Fix unintended high memory usage

This commit is contained in:
Kevin Zhang
2026-03-17 13:11:22 -07:00
parent e56865641e
commit cef5a5611a
2 changed files with 36 additions and 1 deletions
+31
View File
@@ -1414,6 +1414,37 @@ static int basicUnitTests(U32 const seed, double compressibility)
}
DISPLAYLEVEL(3, "OK \n");
DISPLAYLEVEL(3, "test%3i : ldm hashRateLog > windowLog underflow check : ", testNb++);
{
/* Test that when windowLog < hashRateLog, we don't get excessive memory usage
* due to underflow in hashLog calculation (windowLog - hashRateLog). */
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
size_t const size = (1U << 10); // 1 KB
size_t const dstCapacity = ZSTD_compressBound(size);
void* src = (void*)malloc(size);
void* dst = (void*)malloc(dstCapacity);
RDG_genBuffer(src, size, 0.5, 0.5, seed);
CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_enableLongDistanceMatching, ZSTD_ps_enable));
CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmHashLog, 0));
CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 12));
CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_c_ldmHashRateLog, 13));
CHECK_Z(ZSTD_compress2(cctx, dst, dstCapacity, src, size));
{ size_t const cctxSize = ZSTD_sizeof_CCtx(cctx);
DISPLAYLEVEL(3, "CCtx size: %u bytes ", (unsigned)cctxSize);
CHECK_LT(cctxSize, 50 MB);
}
ZSTD_freeCCtx(cctx);
free(src);
free(dst);
}
DISPLAYLEVEL(3, "OK \n");
DISPLAYLEVEL(3, "test%3i : testing dict compression with enableLdm and forceMaxWindow : ", testNb++);
{
ZSTD_CCtx* const cctx = ZSTD_createCCtx();