api: ZSTD_CCtx_reset() and ZSTD_DCtx_reset() silently return 0 (no-op) on invalid ZSTD_ResetDirective values
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 83/100
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:
dev539a64c423(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
- 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 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/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