Skip to content

dictBuilder: validate NULL buffer inputs and arithmetic bounds - #4804

Closed
filtede98 wants to merge 1 commit into
meta:devfrom
filtede98:fix-zdict-and-cover-null-and-overflow-guards
Closed

filtede98 wants to merge 1 commit into
meta:devfrom
filtede98:fix-zdict-and-cover-null-and-overflow-guards

Conversation

@filtede98

Copy link
Copy Markdown

Summary

In lib/dictBuilder/ interfaces, callers providing NULL buffers or sample size arrays could cause NULL pointer dereferences before capacity validation. Additionally, sample size accumulation lacked overflow protection against SIZE_MAX.

This change introduces defensive NULL pointer validation and arithmetic bounds guards across zdict.c, cover.c, and astcover.c.

Details

  1. ZDICT_getDictID() and ZDICT_getDictHeaderSize() (lib/dictBuilder/zdict.c):
    Verify that dictBuffer is non-NULL prior to reading the dictionary magic header via MEM_readLE32.

  2. ZDICT_totalSampleSize() (lib/dictBuilder/zdict.c) and COVER_sum() (lib/dictBuilder/cover.c):
    Return early if ileSizes or samplesSizes is NULL. Guard the running total against size_t overflow during sample size accumulation.

  3. ZDICT_trainFromBuffer_legacy() (lib/dictBuilder/zdict.c):
    Validate that dictBuffer, samplesBuffer, and samplesSizes are non-NULL before processing. Ensure sBuffSize + NOISELENGTH does not overflow size_t before memory allocation.

  4. COVER_ctx_init() and FASTCOVER_ctx_init() (lib/dictBuilder/cover.c, lib/dictBuilder/fastcover.c):
    Validate samplesBuffer and samplesSizes at the function entry point before calculating sample size sums or advancing array pointers.

  5. ZDICT_trainFromBuffer_cover(), ZDICT_optimizeTrainFromBuffer_cover(), ZDICT_trainFromBuffer_fastCover(), ZDICT_optimizeTrainFromBuffer_fastCover():
    Ensure dictBuffer is non-NULL alongside existing capacity validation.

  6. Regression Tests ( ests/fuzzer.c):
    Add test coverage verifying that ZDICT_getDictID() and ZDICT_getDictHeaderSize() safely handle NULL buffer inputs.

Verification

All modifications pass git diff --check cleanly with zero whitespace defects.

In dictBuilder interfaces, callers providing NULL buffers or sample size
arrays could cause NULL pointer dereferences prior to capacity validation:

1. ZDICT_getDictID() and ZDICT_getDictHeaderSize() did not verify whether
   dictBuffer was non-NULL before reading magic headers with MEM_readLE32.
2. ZDICT_totalSampleSize() and COVER_sum() did not guard against NULL
   pointers in the sample size array, and lacked overflow protection when
   accumulating total sample sizes.
3. ZDICT_trainFromBuffer_legacy() did not validate dictBuffer, samplesBuffer,
   or samplesSizes before reading sizes, and did not check for integer
   overflow before allocating the noisy guard band.
4. COVER_ctx_init() and FASTCOVER_ctx_init() calculated sample sums and
   advanced size pointers before validating samplesBuffer or samplesSizes.
5. ZDICT_trainFromBuffer_cover(), ZDICT_optimizeTrainFromBuffer_cover(),
   ZDICT_trainFromBuffer_fastCover(), and
   ZDICT_optimizeTrainFromBuffer_fastCover() did not validate that
   dictBuffer was non-NULL before proceeding with dictionary generation.

Add defensive checks for NULL input pointers, add arithmetic overflow
guards during sample size aggregation, and add regression tests in
tests/fuzzer.c.

Signed-off-by: Filippo Tedeschi <filippotedeschi98@gmail.com>
@meta-cla meta-cla Bot added the CLA Signed label Sep 29, 2026
@filtede98 filtede98 closed this Oct 5, 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.

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