Skip to content

Fix empty incremental-backup filename on non-glibc dirname() - #1352

Closed
Pranav-error wants to merge 1 commit into
pgmoneta:mainfrom
Pranav-error:fix-incremental-basename-dirname
Closed

Pranav-error wants to merge 1 commit into
pgmoneta:mainfrom
Pranav-error:fix-incremental-basename-dirname

Conversation

@Pranav-error

Copy link
Copy Markdown
Collaborator

Fixes #1351.

`write_incremental_file()` split a relative path into directory and
basename by calling `dirname()` and then doing pointer arithmetic
(`rel_path + strlen(rel_path) + 1`) to reach past it to the basename.
That only works if `dirname()` modifies its argument buffer in place
and returns the same pointer -- glibc commonly does this for simple
inputs, but 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 with the exact pattern from this
function: `dirname()` returns a different pointer, and the basename
pointer arithmetic produces an empty string rather than the real
filename. This would be masked on Linux CI, since glibc's `dirname()`
happens to modify common inputs in place there.

Fix: split on `strrchr(rel_path, '/')` against a buffer we fully
control, rather than depending on `dirname()`'s implementation-defined
behavior. Verified the new logic against a slash path, a no-slash
path, and a single-level path via a standalone reproduction (output in
the issue), and that the real codebase still builds cleanly under
`-Werror`.

There's no existing test for incremental backups in this repo to build
on, so I haven't exercised this through a real backup run end-to-end --
said so plainly in the issue rather than overclaiming. Happy to add a
test case here if that's wanted, per the "write test cases that expose
corner-case bugs" guidance.

…rname()

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 pgmoneta#1345 after fixing that one in pgmoneta#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]>
Copilot AI balanced review requested due to automatic review settings October 5, 2026 07:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@jesperpedersen

Copy link
Copy Markdown
Member

Rebased and merged.

Thanks for your contribution !

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.

write_incremental_file() produces an empty filename on platforms where dirname() doesn't modify its buffer in place

3 participants