Skip to content

seekable_format: return -1 from unreachable default in seek callbacks - #4839

Open
i098 wants to merge 1 commit into
meta:devfrom
i098:fix/zstd-seekable-4756
Open

i098 wants to merge 1 commit into
meta:devfrom
i098:fix/zstd-seekable-4756

Conversation

@i098

@i098 i098 commented Oct 12, 2026

Copy link
Copy Markdown

What and why

Fixes #4756.

In ZSTD_seekable_seek_buff() (contrib/seekable_format/zstdseek_decompress.c), the default: case of switch (origin) is only assert(0). With -DNDEBUG the assert is compiled out, so newOffset is read uninitialized and gcc warns. This PR adds return -1; after the assert. -1 is what the function already returns when the new offset is out of range. assert(0) is kept, so debug builds still catch the case.

seekBuffWithTotal() in contrib/seekable_format/tests/seekable_tests.c is a copy of the same function with the same defect, so it gets the same one-line change.

origin is always SEEK_SET, SEEK_CUR or SEEK_END at the library's call sites, so no runtime input reaches default:. The fix only removes the compiler diagnostic.

The warning is a compile-time diagnostic, so the reproducer is a compiler command (gcc 15.2.0):

gcc -DNDEBUG -O2 -Wall -Wextra -Wmaybe-uninitialized -Werror=maybe-uninitialized \
    -Ilib -Ilib/common -Icontrib/seekable_format \
    -c contrib/seekable_format/zstdseek_decompress.c -o /dev/null

Before:

zstdseek_decompress.c: In function 'ZSTD_seekable_seek_buff':
zstdseek_decompress.c:190:8: error: 'newOffset' may be used uninitialized [-Werror=maybe-uninitialized]
  190 |     if (newOffset > buff->size) {

After: no output, exit 0. The same command with -DXXH_NAMESPACE=ZSTD_ on contrib/seekable_format/tests/seekable_tests.c no longer reports newOffset either.

make -C contrib/seekable_format/tests test passes (all 5 seekable tests), and so do make check and make test.

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 assistant (Claude Code) wrote the change, ran the checks above, and wrote this description.

I am new to open source, and this pull request was made with AI assistance.

With -DNDEBUG, assert(0) in the default case of the buffer seek
callbacks compiles away, so newOffset is read uninitialized and
gcc warns with -Wmaybe-uninitialized. Return -1 (the error value
the callback already uses for an out-of-range offset) after the
assert, in ZSTD_seekable_seek_buff() and the copy in seekable_tests.c.
@meta-cla meta-cla Bot added the CLA Signed label Oct 12, 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.

seekable_format warning with -DNDEBUG: potential uninitialised local variable newOffset used

1 participant