Skip to content

Fix SIGFPE in ZSTD_estimateCCtxSize_usingCCtxParams when LDM is enabled#4591

Open
fresh3nough wants to merge 1 commit intofacebook:devfrom
fresh3nough:fix-ldm-sigfpe-estimate-cctx-size
Open

Fix SIGFPE in ZSTD_estimateCCtxSize_usingCCtxParams when LDM is enabled#4591
fresh3nough wants to merge 1 commit intofacebook:devfrom
fresh3nough:fix-ldm-sigfpe-estimate-cctx-size

Conversation

@fresh3nough
Copy link

Summary

Fixes #4590

ZSTD_estimateCCtxSize_usingCCtxParams and ZSTD_estimateCStreamSize_usingCCtxParams raise a SIGFPE (division by zero) on x86 when long distance matching (LDM) is enabled via ZSTD_CCtxParams_setParameter but the individual LDM sub-parameters (hashLog, bucketSizeLog, minMatchLength, hashRateLog) remain at their default zero values.

The division by zero occurs in ZSTD_ldm_getMaxNbSeq() at lib/compress/zstd_ldm.c:181, where maxChunkSize / params.minMatchLength is computed with minMatchLength == 0.

Root Cause

When the user enables LDM through ZSTD_CCtxParams_setParameter(params, ZSTD_c_enableLongDistanceMatching, 1), only the enableLdm flag is set. The other LDM parameters stay at zero. The existing ZSTD_estimateCCtxSize_usingCParams path avoids this by going through ZSTD_makeCCtxParamsFromCParams, which calls ZSTD_ldm_adjustParameters() to fill in sensible defaults. However, ZSTD_estimateCCtxSize_usingCCtxParams and ZSTD_estimateCStreamSize_usingCCtxParams skip this adjustment step and pass the raw (zeroed) LDM params directly to the internal estimation function.

Fix

Both ZSTD_estimateCCtxSize_usingCCtxParams and ZSTD_estimateCStreamSize_usingCCtxParams now copy the LDM parameters to a local and call ZSTD_ldm_adjustParameters() when LDM is enabled, consistent with the pattern already used in ZSTD_makeCCtxParamsFromCParams.

Changes

  • lib/compress/zstd_compress.c: Added LDM parameter adjustment in both estimation functions
  • tests/fuzzer.c: Added regression test that enables LDM via CCtxParams and calls both estimation functions

Steps to Reproduce (before fix)

#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    ZSTD_CCtx_params* p = ZSTD_createCCtxParams();
    ZSTD_CCtxParams_setParameter(p, ZSTD_c_compressionLevel, 22);
    ZSTD_CCtxParams_setParameter(p, ZSTD_c_enableLongDistanceMatching, 1);
    size_t r = ZSTD_estimateCCtxSize_usingCCtxParams(p);
    /* SIGFPE on x86, incorrect result on ARM */
    printf("estimated CCtx size: %zu\n", r);
    ZSTD_freeCCtxParams(p);
    return 0;
}

Compile and run:

cc -g -I lib repro.c lib/libzstd.a -o repro && ./repro

Testing

  • Verified the reproducer no longer crashes and returns a correct estimation
  • make check passes
  • make -C tests fuzzer && tests/fuzzer -t1 passes including the new regression test

@meta-cla
Copy link

meta-cla bot commented Feb 19, 2026

Hi @fresh3nough!

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
Copy link

meta-cla bot commented Feb 19, 2026

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

@meta-cla meta-cla bot added the CLA Signed label Feb 19, 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.

ZSTD_estimateCCtxSize_usingCCtxParams raises SIGFPE after division-by-zero when enabling LDM

1 participant