Fix size_t underflow in COVER_ctx_init for small training sets#4690
Fix size_t underflow in COVER_ctx_init for small training sets#4690rohitsux wants to merge 1 commit into
Conversation
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.
|
Hi @rohitsux! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Fixes #4682.
COVER_ctx_init(lib/dictBuilder/cover.c) validatestotalSamplesSizeagainstMAX(d, sizeof(U64)), but computes the partial suffix array size fromtrainingSamplesSize:When
splitPoint < 1.0(the optimize path), the training set is only a subset of the corpus, sotrainingSamplesSizecan be smaller thanMAX(d, sizeof(U64))even whentotalSamplesSizepasses the check. Thesize_tsubtraction then underflows, and the subsequentmalloc(ctx->suffixSize * sizeof(U32))requests an enormous (wrapped) size rather than failing cleanly.Repro
Calling
ZDICT_optimizeTrainFromBuffer_coverwithsplitPoint = 0.75and 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, returningERROR(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.cin the COVER section: it callsZDICT_optimizeTrainFromBuffer_coverwithsplitPoint = 0.75and a tiny training split and asserts the call returns aZDICT_isErrorresult instead of underflowing.Verified
tests/fuzzerpasses, including the newZDICT_optimizeTrainFromBuffer_cover with tiny training settest.makebuilds cleanly.Thanks to the original reporter for the precise diagnosis.