Skip to content

Return -1 from the unreachable default case in seek_buff - #4763

Open
afonsojanu wants to merge 1 commit into
facebook:devfrom
afonsojanu:fix/seekable-seek-uninitialized-newoffset
Open

Return -1 from the unreachable default case in seek_buff#4763
afonsojanu wants to merge 1 commit into
facebook:devfrom
afonsojanu:fix/seekable-seek-uninitialized-newoffset

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #4756.

ZSTD_seekable_seek_buff in contrib/seekable_format/zstdseek_decompress.c (and its duplicate helper seekBuffWithTotal in contrib/seekable_format/tests/seekable_tests.c) both have a switch (origin) with a default: assert(0); branch that falls through without returning. With NDEBUG defined, assert is a no-op, so the switch falls through and newOffset gets compared/stored uninitialized. Both Clang and GCC flag this as a genuine used-uninitialized warning on -O2 -DNDEBUG builds (reported here with MSVC /W4 /O2 /DNDEBUG).

Fix: add return -1; after the assert(0), as suggested in the issue.

Verified:

  • Compiled zstdseek_decompress.c with clang -Wall -Wextra -O2 -DNDEBUG: the -Wsometimes-uninitialized warning on newOffset is present before this change and gone after.
  • Rebuilt libzstd.a and ran contrib/seekable_format/tests (make CFLAGS="-O3 -DNDEBUG" test): all 5 seekable_tests cases still pass.

One honest caveat: origin is always one of SEEK_SET/SEEK_CUR/SEEK_END at every call site in the library today, so the default branch isn't reachable through any public entry point — there's no runtime input that can exercise this fix, only the compiler's static analysis. The fix is still correct and matches the reporter's own suggested mitigation.

seekable_tests.c's seekBuffWithTotal and zstdseek_decompress.c's
ZSTD_seekable_seek_buff both fall into a default: assert(0) case for
an unsupported seek origin, but never return afterwards. When built
with NDEBUG the assert is a no-op, so the switch falls through and
newOffset gets compared and stored uninitialized. GCC and Clang both
flag this as a real used-uninitialized warning on -O2/-DNDEBUG builds.

The default case isn't reachable through any of the library's own
call sites (origin is always SEEK_SET/SEEK_CUR/SEEK_END), so there's
no runtime input that exercises the fix; verified instead by
compiling zstdseek_decompress.c with clang -Wall -Wextra -DNDEBUG -O2
and confirming the used-uninitialized warning is present before the
fix and gone after, then rebuilding and running the full
contrib/seekable_format/tests suite (all 5 tests pass) with the fix
in place.
@meta-cla meta-cla Bot added the CLA Signed label Sep 4, 2026
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