Closed EngineersBox closed 9 months ago
Thank you for finding this bug. realloc()
is indeed allocating slightly more memory than it should; it's not a particularly big deal as the overhead is minimal and the extra memory is going to be freed properly once the block is deallocated, but I've fixed it anyway as part of the latest commit. I have reported the bug to @nicolasnoble as well, as it's also present in the original implementation of this allocator.
During the invocation of
realloc
, the last case for resizing a block to a new givensize
is done via a call tomalloc(...)
and then a subsequentmemcpy(...)
between the old and new blocks.However, the invocation of
malloc
is passed the_size
parameter instead ofsize
which already has the header bytes added:The issue is that
malloc(...)
also adds thesizeof(BlockHeader)
padding to a requested size:So an initial request size of 92 bytes plus the 16 bytes for the header, aligned to 8 bytes is 112 bytes total. Passing that to malloc, 112 bytes plus the 16 bytes for the header, aligned to 8 bytes is 128 bytes.
So we have over allocated a total of 16 bytes:
Result:
108 => 112, 128 => 128
.Assuming that everything I've stated is correct (happy to be corrected if not), then this would be good for a PR to fix, a simple one liner at that.