Fix try_grow_raw lead memory leak - #450
Conversation
|
AI contributions are not allowed in any @servo repository as indicated on the contributing guidelines https://book.servo.org/contributing/getting-started.html#ai-contributions this PR will subsequently be closed |
|
@alejandro-vaz What signal did you use to decide this is an AI contribution? |
|
I have to admit that this PR did make use of an LLM. But I’m also cautious about using LLMs, so in this PR, the LLM only handled translating my comments, while I personally did all the code understanding, modifications, and writing of the PR content. |
|
@dDostalker sorry for the precipitated decision to close it I'll review your PR, but please don't use LLMs, bad English is better than AI English thanks for contributing |
There is an issue in the current logic where memory is not correctly released before dropping the last reference. In try_grow_raw, new_ptr decides between reallocating (in-place growth) and allocating fresh memory based on the condition len == 0 || !was_on_heap. However, len == 0 is not equivalent to an empty heap, so the pointer of the abandoned memory block gets overwritten by the newly allocated memory before it is freed — leading to a memory leak.
Sample:
output