api: ZSTD_CCtx_reset() and ZSTD_DCtx_reset() silently return 0 (no-op) on invalid ZSTD_ResetDirective values

Open Beginner friendly
#5,126 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

Read lib/compress/zstd_compress.c around ZSTD_CCtx_reset() and lib/decompress/zstd_decompress.c around ZSTD_DCtx_reset(), then compare their directive handling with ZSTD_compressStream2() around line 6492. Add coverage for invalid reset values in the relevant tests. Done means both reset functions return ZSTD_error_parameter_outOfBound for values outside the defined range, while valid directives continue to work.

Written by the indexing model from the issue text.

Description

Describe the bug
ZSTD_ResetDirective (lib/zstd.h:737-741) only defines values 1 (ZSTD_reset_session_only), 2 (ZSTD_reset_parameters), and 3 (ZSTD_reset_session_and_parameters). Notice 0 is not a valid directive.

However, both ZSTD_CCtx_reset() (lib/compress/zstd_compress.c:1404-1424) and ZSTD_DCtx_reset() (lib/decompress/zstd_decompress.c:2032-2046) only check if (reset == ...) and fall through to return 0; for any out-of-range value (such as 0 — the default zero-initialized enum value in C/FFI — or 99). Even when a context is mid-frame or in an error state, passing an invalid directive silently does nothing and reports success (0), leaving the context unreset — unlike ZSTD_compressStream2() (lib/compress/zstd_compress.c:6492), which validates ZSTD_EndDirective and returns ZSTD_error_parameter_outOfBound.

To Reproduce
C reproducer (built and run against dev 539a64c423):

#include <stdio.h>
#include "zstd.h"

int main(void) {
    ZSTD_CCtx* cctx = ZSTD_createCCtx();
    ZSTD_DCtx* dctx = ZSTD_createDCtx();

    size_t rc0  = ZSTD_CCtx_reset(cctx, (ZSTD_ResetDirective)0);
    size_t rc99 = ZSTD_CCtx_reset(cctx, (ZSTD_ResetDirective)99);
    size_t rd0  = ZSTD_DCtx_reset(dctx, (ZSTD_ResetDirective)0);
    size_t rd99 = ZSTD_DCtx_reset(dctx, (ZSTD_ResetDirective)99);

    printf("ZSTD_CCtx_reset(0)  = %zu (isError=%d)\n", rc0,  ZSTD_isError(rc0));
    printf("ZSTD_CCtx_reset(99) = %zu (isError=%d)\n", rc99, ZSTD_isError(rc99));
    printf("ZSTD_DCtx_reset(0)  = %zu (isError=%d)\n", rd0,  ZSTD_isError(rd0));
    printf("ZSTD_DCtx_reset(99) = %zu (isError=%d)\n", rd99, ZSTD_isError(rd99));

    ZSTD_freeCCtx(cctx);
    ZSTD_freeDCtx(dctx);
    return 0;
}

Actual output on dev 539a64c423:

ZSTD_CCtx_reset(0)  = 0 (isError=0)
ZSTD_CCtx_reset(99) = 0 (isError=0)
ZSTD_DCtx_reset(0)  = 0 (isError=0)
ZSTD_DCtx_reset(99) = 0 (isError=0)

Expected behavior
Both ZSTD_CCtx_reset() and ZSTD_DCtx_reset() should reject invalid ZSTD_ResetDirective values (anything outside [ZSTD_reset_session_only, ZSTD_reset_session_and_parameters]) with ZSTD_error_parameter_outOfBound, matching ZSTD_compressStream2()'s validation of ZSTD_EndDirective (lib/compress/zstd_compress.c:6492).

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
Suggested fix at lib/compress/zstd_compress.c:1404 and lib/decompress/zstd_decompress.c:2032:

RETURN_ERROR_IF(reset < ZSTD_reset_session_only || reset > ZSTD_reset_session_and_parameters,
                parameter_outOfBound, "invalid reset directive");
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.