Skip to content

compress, decompress: validate NULL pointers in CCtx, DCtx, and streaming entry points - #4809

Closed
filtede98 wants to merge 1 commit into
meta:devfrom
filtede98:fix-cctx-dctx-null-pointer-guards
Closed

filtede98 wants to merge 1 commit into
meta:devfrom
filtede98:fix-cctx-dctx-null-pointer-guards

Conversation

@filtede98

Copy link
Copy Markdown

Summary

This patch hardens core public compression and decompression APIs across lib/compress/zstd_compress.c and lib/decompress/zstd_decompress.c against crashes caused by NULL pointer dereferences:

  1. lib/compress/zstd_compress.c:

    • ZSTD_CCtx_reset(): Enforce RETURN_ERROR_IF(!cctx, GENERIC, "NULL pointer!"); before accessing cctx->streamStage.
    • ZSTD_CCtx_setParameter(): Validate cctx != NULL before accessing cctx->streamStage.
    • ZSTD_CCtxParams_setParameter(): Validate CCtxParams != NULL before switching on parameters and writing struct fields.
    • ZSTD_CCtx_getParameter() & ZSTD_CCtxParams_getParameter(): Validate cctx != NULL, CCtxParams != NULL, and value != NULL out-pointer.
    • ZSTD_CCtx_setPledgedSrcSize(): Validate cctx != NULL before reading streamStage.
    • ZSTD_compressStream2(): Enforce defensive NULL checks on cctx, output, and input pointers prior to checking pos and size offsets.
    • ZSTD_compressStream2_simpleArgs(): Enforce defensive NULL checks on out-pointers dstPos and srcPos before dereferencing them.
    • ZSTD_compress2(): Validate cctx != NULL before reading buffer modes.
    • ZSTD_getBlockSize_deprecated(): Safely return 0 when cctx == NULL.
    • ZSTD_flushStream() & ZSTD_endStream(): Enforce RETURN_ERROR_IF(!zcs || !output, GENERIC, "NULL pointer!"); before invoking inBuffer_forEndFlush(zcs).
  2. lib/decompress/zstd_decompress.c:

    • ZSTD_DCtx_reset(): Enforce RETURN_ERROR_IF(!dctx, GENERIC, "NULL pointer!"); before accessing dctx->streamStage.
    • ZSTD_DCtx_setParameter(): Enforce RETURN_ERROR_IF(!dctx, GENERIC, "NULL pointer!"); before accessing dctx->streamStage.
    • ZSTD_DCtx_getParameter(): Validate dctx != NULL and value != NULL.
    • ZSTD_decompressBegin() & ZSTD_decompressBegin_usingDDict(): Validate dctx != NULL.
    • ZSTD_nextSrcSizeToDecompress() & ZSTD_nextSrcSizeToDecompressWithInputSize(): Gracefully return 0 when dctx == NULL.
    • ZSTD_nextInputType(): Gracefully return ZSTDnit_frameHeader when dctx == NULL.
    • ZSTD_decompressStream(): Enforce RETURN_ERROR_IF(!zds || !output || !input, GENERIC, "NULL pointer!"); before reading input->src, input->pos, output->dst, output->pos.
    • ZSTD_decompressStream_simpleArgs(): Enforce RETURN_ERROR_IF(!dstPos || !srcPos, GENERIC, "NULL pointer!"); before dereferencing.
  3. tests/zstreamtest.c:

    • Added unit test coverage to basicUnitTests asserting that passing NULL arguments safely returns error codes without segmentation faults.

…ming entry points

Add defensive parameter validation against NULL pointers across public CCtx,
DCtx, and streaming APIs:

1. In lib/compress/zstd_compress.c:
   - Validate cctx != NULL in ZSTD_CCtx_reset, ZSTD_CCtx_setParameter,
     ZSTD_CCtx_getParameter, and ZSTD_CCtx_setPledgedSrcSize before accessing
     streamStage or struct members.
   - Validate CCtxParams != NULL and value != NULL in ZSTD_CCtxParams_setParameter
     and ZSTD_CCtxParams_getParameter.
   - Validate cctx, output, and input pointers in ZSTD_compressStream2 before
     checking pos and size bounds.
   - Validate dstPos != NULL and srcPos != NULL in ZSTD_compressStream2_simpleArgs.
   - Validate cctx != NULL in ZSTD_compress2 and return 0 for NULL cctx in
     ZSTD_getBlockSize_deprecated.
   - Validate zcs != NULL and output != NULL in ZSTD_flushStream and ZSTD_endStream.

2. In lib/decompress/zstd_decompress.c:
   - Validate dctx != NULL in ZSTD_DCtx_reset, ZSTD_DCtx_setParameter,
     ZSTD_DCtx_getParameter, ZSTD_decompressBegin, and ZSTD_decompressBegin_usingDDict.
   - Validate zds, output, and input pointers in ZSTD_decompressStream prior
     to reading buffer fields.
   - Validate dstPos != NULL and srcPos != NULL in ZSTD_decompressStream_simpleArgs.
   - Return 0 on NULL dctx in ZSTD_nextSrcSizeToDecompress and
     ZSTD_nextSrcSizeToDecompressWithInputSize; return ZSTDnit_frameHeader on NULL
     dctx in ZSTD_nextInputType.

3. In tests/zstreamtest.c:
   - Add unit test coverage in basicUnitTests verifying that NULL pointer arguments
     are rejected with appropriate error codes rather than crashing.

Signed-off-by: Filippo Tedeschi <filippotedeschi98@gmail.com>
@meta-cla meta-cla Bot added the CLA Signed label Oct 1, 2026
@filtede98

Copy link
Copy Markdown
Author

Closing. This adds NULL / overflow checks for arguments the callers are not
meant to pass, and I do not have a reproducer where a correct caller hits a
crash on current dev. Sorry for the noise.

@filtede98 filtede98 closed this Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant