ZSTD_decompressStream() rejects a legacy (v0.5–v0.7) frame when the first call supplies fewer than 5 bytes
Describe the bug
zstd.h says the value returned by ZSTD_decompressStream() "is a suggested next input size (just a hint for better latency)", and lib/README.md describes legacy-format decoding as "a transparent capability triggered within decompression functions". With legacy support compiled in, a valid v0.5/v0.6/v0.7 frame decodes with ZSTD_decompress() and with ZSTD_decompressStream() when the first call carries at least 5 bytes, but ZSTD_decompressStream() returns Unknown frame descriptor when the first call carries only 1–4 bytes. Reproduced on v1.5.7 (f8745da6) and on dev 01b7154f (2026-09-18) built with ZSTD_LEGACY_SUPPORT=5.
To Reproduce repro.c:
/* Feeds one zstd frame to ZSTD_decompressStream() in two pieces (first k bytes, then the rest)
* and compares the outcome with feeding it whole. Public API only.
* zstd.h: the return value of ZSTD_decompressStream() is "a suggested next input size (just a
* hint for better latency)", so how the caller chunks the input must not change the result.
* build: cc -I<zstd>/lib repro.c <zstd>/lib/libzstd.a -o repro
* usage: ./repro frame.zst exit 0 = every split agrees with the whole-input result */
#include <stdio.h>
#include <stdlib.h>
#include <zstd.h>
/* returns 1 and sets *total on success, 0 and sets *err on failure */
static int decode(const unsigned char* src, size_t n, size_t k, size_t* total, const char** err, size_t* fed_out)
{
ZSTD_DStream* zds = ZSTD_createDStream();
size_t cap = ZSTD_DStreamOutSize(), fed = 0;
unsigned char* dst = malloc(cap);
int calls = 0, ok = 0;
*total = 0; *err = NULL;
ZSTD_initDStream(zds);
for (;;) {
ZSTD_inBuffer in = { src + fed, (calls == 0 && k < n) ? k : n - fed, 0 };
ZSTD_outBuffer out = { dst, cap, 0 };
size_t ret = ZSTD_decompressStream(zds, &out, &in);
calls++; fed += in.pos; *total += out.pos;
if (ZSTD_isError(ret)) { *err = ZSTD_getErrorName(ret); break; }
if (ret == 0) { ok = 1; break; } /* frame complete and flushed */
if (in.pos == 0 && out.pos == 0) { *err = "stalled: no progress (truncated input?)"; break; }
}
*fed_out = fed;
free(dst); ZSTD_freeDStream(zds);
return ok;
}
int main(int argc, char** argv)
{
unsigned char* buf = malloc(1 << 20);
FILE* f = argc > 1 ? fopen(argv[1], "rb") : NULL;
size_t n, total, fed, whole_total; const char* err; int bad = 0;
if (!f) { fprintf(stderr, "usage: %s frame.zst\n", argv[0]); return 2; }
n = fread(buf, 1, 1 << 20, f); fclose(f);
printf("zstd %s, %zu-byte input, magic %02x %02x %02x %02x\n", ZSTD_versionString(), n, buf[0], buf[1], buf[2], buf[3]);
int whole = decode(buf, n, n, &whole_total, &err, &fed);
if (whole) printf(" whole input : OK, %zu bytes out\n", whole_total);
else printf(" whole input : ERROR %s\n", err);
for (size_t k = 1; k <= 8 && k < n; k++) {
int ok = decode(buf, n, k, &total, &err, &fed);
if (ok) printf(" first call %zu byte%s: OK, %zu bytes out\n", k, k == 1 ? " " : "s", total);
else printf(" first call %zu byte%s: ERROR %s (consumed %zu/%zu)\n", k, k == 1 ? " " : "s", err, fed, n);
if (ok != whole || (ok && total != whole_total)) bad = 1;
}
printf(bad ? " => MISMATCH: outcome depends on how the input was split\n" : " => consistent\n");
return bad;
}legacy-v07.hex:
27b52ffd20ef0000a612e4841fb001100000003559a6e7a1ef7cfcbd3fff
9fefeeef61c3aa311d343822220444218032ad28f3d6280c0a0ed65cac19
8d205f45022e1750666dac8b9c6e077346bb4414e798c3b917326e337c0e
21b1dbcb89512334ab9dbc6d20f503a9914c2e1f59dbd935674b0c957910
0085a696952edf787b4a5c097697d15c96127535a3554ad40b00350b71b5
c02a5ce60845f13943f11c4b54109d3150854b540e014b3d01c00000build and run:
git clone https://github.com/facebook/zstd && cd zstd && git checkout v1.5.7
make -C lib libzstd.a # dev: make -C lib libzstd.a ZSTD_LEGACY_SUPPORT=5
cc -Wall -Wextra -O1 -Ilib repro.c lib/libzstd.a -o repro
xxd -r -p legacy-v07.hex > legacy-v07.zst # legacy-v07.hex = the hex block above
./repro legacy-v07.zstOutput:
zstd 1.5.7, 178-byte input, magic 27 b5 2f fd
whole input : OK, 239 bytes out
first call 1 byte : ERROR Unknown frame descriptor (consumed 1/178)
first call 2 bytes: ERROR Unknown frame descriptor (consumed 2/178)
first call 3 bytes: ERROR Unknown frame descriptor (consumed 3/178)
first call 4 bytes: ERROR Unknown frame descriptor (consumed 4/178)
first call 5 bytes: OK, 239 bytes out
first call 6 bytes: OK, 239 bytes out
first call 7 bytes: OK, 239 bytes out
first call 8 bytes: OK, 239 bytes out
=> MISMATCH: outcome depends on how the input was splitExpected behavior How the input is split should not determine the outcome. All lines should finish without errors.
Desktop (please complete the following information):
- OS: Linux x86-64 (Ubuntu 24.04)
- Version: zstd v1.5.7 (f8745da6); dev 01b7154f with ZSTD_LEGACY_SUPPORT=5
- Compiler: gcc 13.3.0
- Flags: make -C lib libzstd.a (dev: ZSTD_LEGACY_SUPPORT=5); program -Wall -Wextra -O1
- Build system: Makefile
Additional context Found using Claude at Anthropic, I manually verified the PoC.
Source: facebook/zstd