Skip to content

fileio: don't abort on --patch-from with an empty source file - #4896

Open
b84330808 wants to merge 1 commit into
meta:devfrom
b84330808:fix-patch-from-empty-input
Open

b84330808 wants to merge 1 commit into
meta:devfrom
b84330808:fix-patch-from-empty-input

Conversation

@b84330808

Copy link
Copy Markdown

What and why

zstd --patch-from=REF empty aborts on an assert when the file to compress is empty:

zstd: fileio.c:1233: FIO_highbit64: Assertion `v != 0' failed.

FIO_adjustParamsForPatchFromMode() computes the window log with FIO_highbit64(maxSrcFileSize), which needs a non-zero value, and maxSrcFileSize is 0 here. The CLI keeps assert() on in its default build, so this crashes the regular binary (exit 134).

An empty source only needs the smallest window, so this uses ZSTD_WINDOWLOG_MIN when the size is 0. That is the value the clamp right below already picks for any tiny source, so non-empty inputs don't change.

The new cli-test compression/patch-from.sh round-trips an empty file with --patch-from. It fails with exit code 134 without the fix.

Checklist

  • I searched open pull requests and issues: this problem is not already being addressed.
  • Bug fix: it comes with a reproducer or a test that fails without the fix.
  • Performance change: numbers from repeated runs are included, with CPU, compiler, flags and data.
    A gain in one configuration is not a general gain: see the Performance section of CONTRIBUTING.md.
  • I stated below whether AI or other automated tools were used.

Tools used

An AI coding agent (Claude Code) found the problem and wrote the change and the test.

FIO_adjustParamsForPatchFromMode() computes the window log of the source
with FIO_highbit64(maxSrcFileSize), which asserts v != 0. When the file
to compress is empty, maxSrcFileSize is 0, so

    zstd --patch-from=ref empty

aborts on the assertion. The CLI is built with assert() enabled by
default, so this crashes the regular `zstd` binary.

An empty source only needs the smallest window, so use
ZSTD_WINDOWLOG_MIN in that case. This is the window log the clamp below
already selected for any small source.

Add a cli-test that compresses and decompresses an empty file with
--patch-from.
@meta-cla meta-cla Bot added the CLA Signed label Nov 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant