Skip to content

Commit

Permalink
fix memory leak in start find (#184)
Browse files Browse the repository at this point in the history
* Fix memory leak when encountering empty folders
* Do bigger allocation steps when iterating over folders
* Don't make assumption that block data is allocated together with stored block structure
* release notes
  • Loading branch information
DanEngelbrecht authored Apr 11, 2022
1 parent de810e0 commit 8b4f7c4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ jobs:
release_name: Release ${{ env.RELEASE_VERSION }}
body: |
# Changes in this Release
- **FIX** Properly handle UNC paths
- **FIX** Fix memory and handle leak when encountering empty folders
- **FIX** Fix reallocation size when scanning folders
draft: false
prerelease: false
files: "*-x64.zip"
2 changes: 1 addition & 1 deletion lib/compressblockstore/longtail_compressblockstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ static int DecompressBlock(
LONGTAIL_FATAL_ASSERT(ctx, uncompressed_stored_block->m_BlockIndex, return EINVAL; )
uncompressed_stored_block->m_BlockData = &((uint8_t*)(&uncompressed_stored_block->m_BlockIndex[1]))[block_index_data_size];
uncompressed_stored_block->m_BlockChunksDataSize = uncompressed_size;
memmove(&uncompressed_stored_block->m_BlockIndex[1], &compressed_stored_block->m_BlockIndex[1], block_index_data_size);
memmove(&uncompressed_stored_block->m_BlockIndex[1], ((const uint8_t*)(compressed_stored_block->m_BlockData))-block_index_data_size, block_index_data_size);

size_t real_uncompressed_size = 0;
err = compression_api->Decompress(
Expand Down
8 changes: 7 additions & 1 deletion lib/longtail_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,13 @@ int Longtail_StartFind(HLongtail_FSIterator fs_iterator, const char* path)
Longtail_Free(fs_iterator->m_Path);
return Win32ErrorToErrno(GetLastError());
}
return Skip(fs_iterator);
int err = Skip(fs_iterator);
if (err != 0)
{
FindClose(fs_iterator->m_Handle);
Longtail_Free(fs_iterator->m_Path);
}
return err;
}

int Longtail_FindNext(HLongtail_FSIterator fs_iterator)
Expand Down
7 changes: 4 additions & 3 deletions src/longtail.c
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,7 @@ static int AddFile(void* context, const char* root_path, const char* asset_path,
full_path[asset_path_length + 1] = 0;
}

int err = AppendPath(&paths_context->m_FileInfos, full_path, properties->m_Size, properties->m_Permissions, &paths_context->m_ReservedPathCount, &paths_context->m_ReservedPathSize, 512, 128);
int err = AppendPath(&paths_context->m_FileInfos, full_path, properties->m_Size, properties->m_Permissions, &paths_context->m_ReservedPathCount, &paths_context->m_ReservedPathSize, 1024, 1024 * 32);
if (err)
{
LONGTAIL_LOG(ctx, LONGTAIL_LOG_LEVEL_ERROR, "AppendPath() failed with %d", err)
Expand Down Expand Up @@ -3293,8 +3293,9 @@ int Longtail_InitStoredBlockFromData(
LONGTAIL_LOG(ctx, LONGTAIL_LOG_LEVEL_ERROR, "Longtail_InitBlockIndexFromData() failed with %d", err)
return err;
}
stored_block->m_BlockData = &((uint8_t*)stored_block->m_BlockIndex)[Longtail_GetBlockIndexSize(*stored_block->m_BlockIndex->m_ChunkCount)];
stored_block->m_BlockChunksDataSize = (uint32_t)(block_data_size - Longtail_GetBlockIndexDataSize(*stored_block->m_BlockIndex->m_ChunkCount));
size_t BlockIndexDataSize = Longtail_GetBlockIndexDataSize(*stored_block->m_BlockIndex->m_ChunkCount);
stored_block->m_BlockData = &((uint8_t*)block_data)[BlockIndexDataSize];
stored_block->m_BlockChunksDataSize = (uint32_t)(block_data_size - BlockIndexDataSize);
stored_block->Dispose = 0;
return 0;
}
Expand Down

0 comments on commit 8b4f7c4

Please sign in to comment.