Skip to content

Commit 35e1539

Browse files
Fix for out-of-bound accessing last free block in EXTENSIVE defragmentation algorithm.
Code by @medranSolus See #232
1 parent 228a1b6 commit 35e1539

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

include/vk_mem_alloc.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13289,7 +13289,7 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo
1328913289
VMA_SWAP(vector->m_Blocks[i], vector->m_Blocks[vector->GetBlockCount() - ++m_ImmovableBlockCount]);
1329013290
if (state.firstFreeBlock != SIZE_MAX)
1329113291
{
13292-
if (i < state.firstFreeBlock - 1)
13292+
if (i + 1 < state.firstFreeBlock)
1329313293
{
1329413294
if (state.firstFreeBlock > 1)
1329513295
VMA_SWAP(vector->m_Blocks[i], vector->m_Blocks[--state.firstFreeBlock]);
@@ -13709,6 +13709,13 @@ bool VmaDefragmentationContext_T::ComputeDefragmentation_Extensive(VmaBlockVecto
1370913709
case StateExtensive::Operation::FindFreeBlockTexture:
1371013710
case StateExtensive::Operation::FindFreeBlockAll:
1371113711
{
13712+
// No more blocks to free, just perform fast realloc and move to cleanup
13713+
if (vectorState.firstFreeBlock == 0)
13714+
{
13715+
vectorState.operation = StateExtensive::Operation::Cleanup;
13716+
return ComputeDefragmentation_Fast(vector);
13717+
}
13718+
1371213719
// No free blocks, have to clear last one
1371313720
size_t last = (vectorState.firstFreeBlock == SIZE_MAX ? vector.GetBlockCount() : vectorState.firstFreeBlock) - 1;
1371413721
VmaBlockMetadata* freeMetadata = vector.GetBlock(last)->m_pMetadata;
@@ -13777,8 +13784,7 @@ bool VmaDefragmentationContext_T::ComputeDefragmentation_Extensive(VmaBlockVecto
1377713784
}
1377813785
vectorState.firstFreeBlock = last;
1377913786
// Nothing done, block found without reallocations, can perform another reallocs in same pass
13780-
if (prevMoveCount == m_Moves.size())
13781-
return ComputeDefragmentation_Extensive(vector, index);
13787+
return ComputeDefragmentation_Extensive(vector, index);
1378213788
}
1378313789
break;
1378413790
}

0 commit comments

Comments
 (0)