decompress: ZSTD_decompressFrame() returns checksum_wrong instead of srcSize_wrong on truncated checksum (even with ZSTD_d_forceIgnoreChecksum=1)
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 83/100
Research direction
Start with ZSTD_decompressFrame() in lib/decompress/zstd_decompress.c, especially the checksum handling around lines 1069–1079 and the nearby truncation checks. Compare the short-checksum error with ZSTD_findFrameSizeInfo() and the reproducer in the issue. Done when truncated checksums return ZSTD_error_srcSize_wrong, including when checksum ignoring is enabled, and the relevant decompression tests pass.
Written by the indexing model from the issue text.
Description
Describe the bug
When a frame with Content_Checksum_flag = 1 is truncated inside its trailing 4-byte checksum (leaving 0–3 bytes remaining), ZSTD_decompress() / ZSTD_decompressDCtx() returns ZSTD_error_checksum_wrong (22, "Restored data doesn't match checksum") instead of ZSTD_error_srcSize_wrong (72, "Src size is incorrect").
This happens even when ZSTD_d_forceIgnoreChecksum = ZSTD_d_ignoreChecksum (1) is explicitly set on the ZSTD_DCtx, which is self-contradictory and inconsistent with ZSTD_findFrameSizeInfo() (lib/decompress/zstd_decompress.c:799-800) and every other truncation check in ZSTD_decompressFrame() (lines 986, 994, 1014).
To Reproduce
C reproducer (built and run against dev 539a64c423):
#define ZSTD_STATIC_LINKING_ONLY
#include <stdio.h>
#include "zstd.h"
#include "zstd_errors.h"
int main(void) {
const char src[] = "hello world checksum truncation test";
char cbuf[256], dbuf[256];
ZSTD_CCtx* cctx = ZSTD_createCCtx();
ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
size_t cSize = ZSTD_compress2(cctx, cbuf, sizeof(cbuf), src, sizeof(src) - 1);
ZSTD_freeCCtx(cctx);
ZSTD_DCtx* dctx = ZSTD_createDCtx();
size_t r1 = ZSTD_decompressDCtx(dctx, dbuf, sizeof(dbuf), cbuf, cSize - 2);
printf("default: code=%u (%s)\n",
(unsigned)ZSTD_getErrorCode(r1), ZSTD_getErrorName(r1));
ZSTD_DCtx_setParameter(dctx, ZSTD_d_forceIgnoreChecksum, ZSTD_d_ignoreChecksum);
size_t r2 = ZSTD_decompressDCtx(dctx, dbuf, sizeof(dbuf), cbuf, cSize - 2);
printf("forceIgnoreChecksum: code=%u (%s)\n",
(unsigned)ZSTD_getErrorCode(r2), ZSTD_getErrorName(r2));
size_t r3 = ZSTD_findFrameCompressedSize(cbuf, cSize - 2);
printf("findFrameCompSize: code=%u (%s)\n",
(unsigned)ZSTD_getErrorCode(r3), ZSTD_getErrorName(r3));
ZSTD_freeDCtx(dctx);
return 0;
}
Actual output on dev 539a64c423:
default: code=22 (Restored data doesn't match checksum)
forceIgnoreChecksum: code=22 (Restored data doesn't match checksum)
findFrameCompSize: code=72 (Src size is incorrect)
Expected behavior
Both default and forceIgnoreChecksum should return ZSTD_error_srcSize_wrong (72, "Src size is incorrect"), matching ZSTD_findFrameSizeInfo() (lib/decompress/zstd_decompress.c:799-800) and all other truncation checks in ZSTD_decompressFrame().
Desktop (please complete the following information):
- OS: Linux x86_64 (Ubuntu 22.04)
- Version:
dev539a64c423(v1.6.0+) - Compiler: gcc 11.4.0
- Flags:
-O2 - Build system: Makefile
Additional context
Root cause at lib/decompress/zstd_decompress.c:1069-1079 (when commit 44c54a3e3 hoisted the remainingSrcSize < 4 check above if (!dctx->forceIgnoreChecksum)):
if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, "");
if (!dctx->forceIgnoreChecksum) {
tests/fuzzer.c:4038-4039 only checks if (!ZSTD_isError(r)) goto _output_error; /* wrong checksum size should still throw error */, so changing checksum_wrong to srcSize_wrong at line 1070 preserves full compatibility with existing tests.
Suggested fix:
--- a/lib/decompress/zstd_decompress.c
+++ b/lib/decompress/zstd_decompress.c
@@ -1067,7 +1067,7 @@ static size_t ZSTD_decompressFrame(ZSTD_DCtx* dctx,
corruption_detected, "");
}
if (dctx->fParams.checksumFlag) { /* Frame content checksum verification */
- RETURN_ERROR_IF(remainingSrcSize<4, checksum_wrong, "");
+ RETURN_ERROR_IF(remainingSrcSize<4, srcSize_wrong, "");
if (!dctx->forceIgnoreChecksum) {
- Dominant language
- C
- Stars
- 28.1k
- Forks
- 2.7k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from facebook/zstd
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
windows-artifacts: the win-arm64 job uses a Visual Studio generator missing from windows-11-armPossibly taken @Ranjeet2063 claimed this 12 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 86/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NVIDIA/open-gpu-kernel-modules#1635 ·
Maintainers usually reply within 1 day
-
iOS: the "Edit controls" button never hides, even with a controller connected, and covers the gameOpengame: 010065A014024000 ios
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
seedds/nxnative#3714 · 1 comment ·
Maintainers usually reply within 2 days
-
HTTP/3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
haskell/bytestring#730 · 1 comment ·
-
bug ux/behavior
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
DeusData/codebase-memory-mcp#3191 ·
Maintainers usually reply within 1 day