Skip to content

write: report close callback failures from archive_write_close() - #3749

Merged
DHowett merged 2 commits into
libarchive:masterfrom
MaxFreedomPollard:fix/write-close-callback-error
Oct 27, 2026
Merged

DHowett merged 2 commits into
libarchive:masterfrom
MaxFreedomPollard:fix/write-close-callback-error

Conversation

@MaxFreedomPollard

Copy link
Copy Markdown
Contributor

Fixes #1713.

archive_write_client_close() calls the client close callback but ignores its return value. As a result archive_write_close() returns ARCHIVE_OK even when the callback returns ARCHIVE_FATAL, which archive_write_open(3) tells a close callback to return on failure. archive_write_free() behaves the same way when it closes the archive itself. The error the callback set with archive_set_error() stays in place, so a caller only sees the failure if it checks archive_errno() after a close that reported success. If the close callback does the final flush or upload, a failed write is reported as a finished archive.

The status used to be returned. In 911dc2b, archive_write_client_close() returned the callback's value. 992610f replaced the function body with the output blocking code and dropped the call, and 4739b8d added the call back without its result. On the read side, client_close_proxy() already merges the close callback's status.

This change merges the callback's status into the value archive_write_client_close() returns, so the caller gets the worse of the final block write and the close callback. The callback still runs only once: __archive_write_filters_close() marks the client filter as failed, and a later archive_write_free() does not call it again. The built-in helpers behave as before. The close callback of archive_write_open_filename() ignores the result of close(2) and returns ARCHIVE_OK, and the fd, FILE * and memory helpers do not register one. bsdtar, bsdcpio and the other tools are therefore unaffected.

The first commit adds test_archive_write_close_callback, and the second commit is the fix. The test opens a ustar writer whose close callback returns a chosen status and sets EIO when that status is not ARCHIVE_OK. It checks what archive_write_close() and archive_write_free() return, that the callback's error is kept, and that the callback runs once.

Verification on macOS (Apple clang 21, CMake + Ninja, Debug build without liblzma, zstd and lz4):

  • At the test commit, libarchive_test test_archive_write_close_callback fails 3 assertions:
    • archive_write_close() returns 0 instead of ARCHIVE_FATAL, with errno 5 and the callback's message set.
    • archive_write_free() with no earlier close returns 0 instead of ARCHIVE_FATAL.
    • An ARCHIVE_WARN from the callback is returned as 0.
  • With the fix the test passes. A C version of the reproducer from archive_write_close returns ARCHIVE_OK when archive_close_callback returns ARCHIVE_FATAL #1713 now prints archive_write_close: -30 instead of 0.
  • Full suites with the fix:
    • libarchive_test: 952 tests ran and 3 failed. test_read_format_zip_lzma_alone_large_size fails because liblzma is not enabled. test_write_disk_perms and test_write_disk_secure fail because the sandbox the tests ran in does not allow setuid/setgid bits or writes to /tmp. All three fail the same way without this change.
    • bsdtar_test (80 tests), bsdcpio_test (49), bsdcat_test (18) and bsdunzip_test (30) all pass.

🤖 Generated with Claude Code

MaxFreedomPollard and others added 2 commits October 22, 2026 18:50
archive_write_open(3) says that the close callback should return
ARCHIVE_FATAL on failure. Check that archive_write_close() and
archive_write_free() report that status, keep the error set by the
callback, and do not invoke the callback a second time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
archive_write_client_close() invoked the client close callback but
ignored its return value, so archive_write_close(), and
archive_write_free() when it closes the archive, returned ARCHIVE_OK
even when the callback returned ARCHIVE_FATAL as archive_write_open(3)
asks it to on failure. The error set by the callback was left in
place, so the failure was only visible to a caller that checked
archive_errno() after a close that reported success.

The status was lost in 2010: 992610f moved the output blocking into
archive_write_client_close() and dropped the closer call, and 4739b8d
added the call back without its return value.

Merge the callback's status into the value returned by
archive_write_client_close(), as client_close_proxy() already does
for the close callback on the read side.

Fixes #1713

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@stoeckmann stoeckmann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@DHowett
DHowett merged commit bad49b7 into libarchive:master Oct 27, 2026
24 of 25 checks passed
mmatuska pushed a commit to mmatuska/libarchive that referenced this pull request Oct 29, 2026
…lose-callback-error

write: report close callback failures from archive_write_close()
(cherry picked from commit bad49b7)
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.

archive_write_close returns ARCHIVE_OK when archive_close_callback returns ARCHIVE_FATAL

3 participants