Repository navigation
Fix invalid free in pgmoneta_symlink_at_file() - #1348
Closed
Pranav-error wants to merge 1 commit into
Closed
Pranav-error wants to merge 1 commit into
Pranav-error wants to merge 1 commit into
Conversation
dirname() is not required to return the pointer it was given -- on
macOS/BSD libc it can return a pointer into static storage instead of
modifying the strdup'd buffer in place. free(dir_path) was freeing
whatever dirname() returned rather than the actual allocation, which
crashes macOS's hardened malloc ("BUG IN CLIENT OF LIBMALLOC: not an
allocated block") and is undefined behavior elsewhere.
Found while getting PR pgmoneta#1316's test_cli_verify to actually run
end-to-end: any restore (or verify, which restores internally) of a
backup containing a tablespace crashed inside
copy_tablespaces_restore() -> pgmoneta_symlink_at_file() before this
fix.
Uses pgmoneta_append() rather than strdup() per project convention.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
Pranav-error
force-pushed
the
fix-symlink-at-file-free
branch
from
October 4, 2026 10:33
2c84bcb to
952ea48
Compare
This was referenced Oct 4, 2026
Member
|
Rebased and merged. Thanks for your contribution ! |
jesperpedersen
pushed a commit
that referenced
this pull request
Oct 8, 2026
write_incremental_file() did: rel_path = pgmoneta_append(rel_path, relative_filename); rel_path = dirname(rel_path); file_name = pgmoneta_append(file_name, rel_path + strlen(rel_path) + 1); This relies on dirname() modifying its buffer in place and returning the same pointer, so pointer arithmetic past the inserted NUL lands on the original basename bytes. glibc commonly does this for simple inputs; POSIX doesn't require it, and BSD/macOS libc's dirname() can return a pointer into a separate static buffer instead -- verified empirically on macOS/clang, where this produces an EMPTY file_name rather than the real basename. Masked on Linux CI because glibc's dirname() happens to modify common inputs in place there. Fix: split directory and basename with strrchr() on a buffer we fully control, instead of depending on dirname()'s implementation-defined in-place behavior. Verified against a slash path, a no-slash path, and a single-level path via a standalone reproduction, and that the real codebase builds cleanly under -Werror with the change applied. Found by sweeping every dirname() call site in pg-family for the same portability bug class as #1345 after fixing that one in #1348 -- this is a different call site and failure mode (wrong data, not an invalid free), but the same root cause. Co-Authored-By: Claude Sonnet 5 <[email protected]>
jesperpedersen
pushed a commit
to pgvictoria/pgvictoria
that referenced
this pull request
Oct 11, 2026
dirname() is not required to return the pointer it was given -- on
macOS/BSD libc it can return a pointer into static storage instead of
modifying the strdup'd buffer in place. free(dir_path) was freeing
whatever dirname() returned rather than the actual allocation, which
crashes macOS's hardened malloc ("BUG IN CLIENT OF LIBMALLOC: not an
allocated block") and is undefined behavior elsewhere.
Found the identical bug in pgmoneta (pgmoneta/pgmoneta#1345 /
pgmoneta/pgmoneta#1348) while getting that project's restore/verify
test suite running end-to-end on macOS/arm64; pgvictoria's utils.c had
the exact same construct.
Uses pgvictoria_append() rather than strdup() per project convention.
Verified by compiling utils.c standalone with the Darwin-specific
flags this project's CMakeLists.txt adds for macOS -- clean build, no
warnings.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
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.
Fixes #1345.
`dirname(strdup(from))` followed by `free(dir_path)` assumes
`dirname()` returns the exact pointer `strdup()` allocated. POSIX
doesn't guarantee that -- on macOS/BSD libc, `dirname()` can return a
pointer into static/internal storage instead. The fix keeps the
`strdup`'d pointer in its own variable and frees that instead of
whatever `dirname()` happened to return.
Verified by reproducing the crash on macOS/arm64 (restore of a backup
with tablespaces, via `pgmoneta-cli verify`), confirming the exact
invalid-free via a live crash report, applying this fix, and confirming
the same restore/verify now completes without crashing.
This surfaced while getting PR #1316's own `test_cli_verify` test to
run end-to-end for the first time -- it was crashing here, before ever
reaching the page-checksum logic that test is meant to exercise.