decompress: ZSTD_decompressFrame() returns checksum_wrong instead of srcSize_wrong on truncated checksum (even with ZSTD_d_forceIgnoreChecksum=1)

Open Beginner friendly
#5,118 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
backend

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: dev 539a64c423 (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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from facebook/zstd

All issues in facebook/zstd

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.