Skip to content

fix: off-by-one heap buffer overread in seekable format - #4758

Open
VirajMishra1 wants to merge 2 commits into
facebook:devfrom
VirajMishra1:fix-seekable-oob-read
Open

fix: off-by-one heap buffer overread in seekable format#4758
VirajMishra1 wants to merge 2 commits into
facebook:devfrom
VirajMishra1:fix-seekable-oob-read

Conversation

@VirajMishra1

Copy link
Copy Markdown

ZSTD_seekTable_getFrameDecompressedSize in contrib/seekable_format/zstdseek_decompress.c uses > instead of >= for the frameIndex bounds check (line 369), unlike its sister function ZSTD_seekTable_getFrameCompressedSize which correctly uses >= (line 357).

When frameIndex == tableLen, the function does not return an error and proceeds to access entries[frameIndex + 1]. The seek table allocates numFrames + 1 entries, so valid indices are 0..numFrames. Index numFrames + 1 is a heap buffer overread of uninitialized memory.

Any application iterating with for (i = 0; i <= numFrames; i++) and calling getFrameDecompressedSize will silently read past the array. The compressed-size variant catches this; the decompressed-size variant does not.

Fix: change > to >= on line 369, matching getFrameCompressedSize.

Test: added Test 6 in contrib/seekable_format/tests/seekable_tests.c asserting both functions return errors for frameIndex == nbFrames. All 6 tests pass.

VirajMishra1 and others added 2 commits September 2, 2026 20:29
ZSTD_seekTable_getFrameDecompressedSize uses > instead of >= for the
frameIndex bounds check, unlike its sister function
ZSTD_seekTable_getFrameCompressedSize which correctly uses >=.

When frameIndex == tableLen, the function accesses entries[tableLen + 1],
reading past the allocated array (numFrames + 1 entries, valid indices
0..numFrames). This is a heap buffer overread.

Fix: change > to >= to match getFrameCompressedSize.
Add --stream-size flag to pzstd, matching the existing flag in the main
zstd CLI. When compressing from stdin/pipe, this tells the compressor
the total input size, enabling it to write the content size in the frame
header and optimize window size for better compression ratios.

Closes facebook#3171

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@meta-cla

meta-cla Bot commented Sep 3, 2026

Copy link
Copy Markdown

Hi @VirajMishra1!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Sep 3, 2026
@meta-cla

meta-cla Bot commented Sep 3, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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