Skip to content

Fix size_t underflow in COVER_ctx_init for small training sets#4690

Open
rohitsux wants to merge 1 commit into
facebook:devfrom
rohitsux:fix/cover-ctx-init-underflow
Open

Fix size_t underflow in COVER_ctx_init for small training sets#4690
rohitsux wants to merge 1 commit into
facebook:devfrom
rohitsux:fix/cover-ctx-init-underflow

Conversation

@rohitsux
Copy link
Copy Markdown

@rohitsux rohitsux commented Jun 5, 2026

Fixes #4682.

COVER_ctx_init (lib/dictBuilder/cover.c) validates totalSamplesSize against MAX(d, sizeof(U64)), but computes the partial suffix array size from trainingSamplesSize:

ctx->suffixSize = trainingSamplesSize - MAX(d, sizeof(U64)) + 1;

When splitPoint < 1.0 (the optimize path), the training set is only a subset of the corpus, so trainingSamplesSize can be smaller than MAX(d, sizeof(U64)) even when totalSamplesSize passes the check. The size_t subtraction then underflows, and the subsequent malloc(ctx->suffixSize * sizeof(U32)) requests an enormous (wrapped) size rather than failing cleanly.

Repro

Calling ZDICT_optimizeTrainFromBuffer_cover with splitPoint = 0.75 and a corpus whose first (training) samples sum to fewer than 8 bytes triggers the underflow (returns a spurious allocation error / can attempt a huge allocation under overcommit).

Fix

Add a check that trainingSamplesSize >= MAX(d, sizeof(U64)) before the subtraction, returning ERROR(srcSize_wrong) — the same way the existing corpus-size checks behave. This rejects only inputs that were already broken (the suffix array would have been non-positive), so no valid input is affected.

Test

Adds a regression test to tests/fuzzer.c in the COVER section: it calls ZDICT_optimizeTrainFromBuffer_cover with splitPoint = 0.75 and a tiny training split and asserts the call returns a ZDICT_isError result instead of underflowing.

Verified

  • tests/fuzzer passes, including the new ZDICT_optimizeTrainFromBuffer_cover with tiny training set test.
  • make builds cleanly.

Thanks to the original reporter for the precise diagnosis.

When splitPoint < 1.0, the COVER dictionary builder trains on a subset
of the corpus, so trainingSamplesSize can be smaller than the total
even after the existing totalSamplesSize check. The partial suffix
array size is computed as `trainingSamplesSize - MAX(d, sizeof(U64)) + 1`,
which underflows size_t when the training set is too small, producing
an enormous allocation request instead of a clean error.

Guard against an undersized training set before the subtraction and
return srcSize_wrong, and add a regression test in fuzzer.c.

Fixes facebook#4682.
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Jun 5, 2026

Hi @rohitsux!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Jun 5, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented Jun 5, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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.

size_t underflow in COVER_ctx_init when ZDICT_optimizeTrainFromBuffer_cover invoked with params.d=0 leads to multi-EB malloc

1 participant