Repository navigation
jcl: JCL_realloc(): do not free the original pointer on failure - #83
Merged
Merged
Conversation
Unlike realloc(), JCL_realloc() frees the original pointer when
the allocation fails.
JCL_realloc() has three callers. Two of them (maybeGrowBuf()
and VMFile.toCanonicalForm()) rely on the original buffer being
freed on failure. The third, VMFile.list(), assumes realloc()
semantics: it frees the list entries and then the list when
reallocation fails. Since JCL_realloc() has already freed the
list itself, this causes a use after free and a double free.
The problem is that fixing only VMFile.list() isn't easy. The
current behaviour of JCL_realloc() makes it difficult to use it
correctly with arrays of pointers: once the array is freed, the
pointers it held are not reachable anymore.
So, make JCL_realloc() behave like realloc() and do not free the
memory on failure. This makes VMFile.list() correct. Update the
other two callers:
- maybeGrowBuf() now frees the buffer itself when it cannot grow
it, as its callers expect.
- The readlink() loop in VMFile.toCanonicalForm() did not check
the result. When the allocation failed, readlink() was called
with a NULL buffer and failed, and the pending OutOfMemoryError
was replaced with an IOException("readlink failed"). Now it
frees its buffers and returns with OutOfMemoryError pending.
Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
phvega
approved these changes
Nov 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unlike realloc(), JCL_realloc() frees the original pointer when the allocation fails.
JCL_realloc() has three callers. Two of them (maybeGrowBuf() and VMFile.toCanonicalForm()) rely on the original buffer being freed on failure. The third, VMFile.list(), assumes realloc() semantics: it frees the list entries and then the list when reallocation fails. Since JCL_realloc() has already freed the list itself, this causes a use after free and a double free.
The problem is that fixing only VMFile.list() isn't easy. The current behaviour of JCL_realloc() makes it difficult to use it correctly with arrays of pointers: once the array is freed, the pointers it held are not reachable anymore.
So, make JCL_realloc() behave like realloc() and do not free the memory on failure. This makes VMFile.list() correct. Update the other two callers: