Skip to content

jcl: JCL_realloc(): do not free the original pointer on failure - #83

Merged
guillerodriguez merged 1 commit into
masterfrom
fix/jcl_realloc
Nov 5, 2026
Merged

guillerodriguez merged 1 commit into
masterfrom
fix/jcl_realloc

Conversation

@guillerodriguez

Copy link
Copy Markdown
Contributor

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.

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>
@guillerodriguez
guillerodriguez merged commit 02e23e6 into master Nov 5, 2026
3 checks passed
@guillerodriguez
guillerodriguez deleted the fix/jcl_realloc branch November 5, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants