Skip to content

Commit

Permalink
Better underflow safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Auburn committed Jun 19, 2022
1 parent 87279b1 commit 1a60a85
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/FastNoise/SmartNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ namespace FastNoise
continue;
}

void* ptr = pool + freeSlots[idx].pos + sizeof( SlotHeader );
uint8_t* startSlot = pool + freeSlots[idx].pos;
void* ptr = startSlot + sizeof( SlotHeader );
size_t space = freeSlots[idx].size - sizeof( SlotHeader );

if( std::align( align, size, ptr, space ) )
{
uint8_t* startSlot = pool + freeSlots[idx].pos;
uint8_t* endSlot = (uint8_t*)ptr + size;

// Align next slot correctly for SlotHeader
Expand All @@ -171,16 +171,17 @@ namespace FastNoise
assert( freeSlots[idx].size >= slotSize );

usedSlots.emplace_back( Slot{ freeSlots[idx].pos, slotSize } );

freeSlots[idx].pos += slotSize;
freeSlots[idx].size -= slotSize;

// Check if remaining free slot is empty
if( freeSlots[idx].size == 0 )
if( freeSlots[idx].size <= slotSize )
{
assert( freeSlots[idx].size == slotSize );
freeSlots.erase( freeSlots.cbegin() + idx );
}

freeSlots[idx].pos += slotSize;
freeSlots[idx].size -= slotSize;

new( startSlot ) SlotHeader { 0u };

return ptr;
Expand Down

0 comments on commit 1a60a85

Please sign in to comment.