diff --git a/lib/decompress/zstd_decompress.c b/lib/decompress/zstd_decompress.c index 325f0b54dfc..300197f1db7 100644 --- a/lib/decompress/zstd_decompress.c +++ b/lib/decompress/zstd_decompress.c @@ -1793,6 +1793,9 @@ size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict) assert(!dctx->staticSize); /* Impossible: ddictSet cannot have been allocated if static dctx */ FORWARD_IF_ERROR(ZSTD_DDictHashSet_addDDict(dctx->ddictSet, ddict, dctx->customMem), ""); } + } else if (dctx->ddictSet) { + ZSTD_freeDDictHashSet(dctx->ddictSet, dctx->customMem); + dctx->ddictSet = NULL; } return 0; } @@ -1955,6 +1958,10 @@ size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset) || (reset == ZSTD_reset_session_and_parameters) ) { RETURN_ERROR_IF(dctx->streamStage != zdss_init, stage_wrong, ""); ZSTD_clearDict(dctx); + if (dctx->ddictSet) { + ZSTD_freeDDictHashSet(dctx->ddictSet, dctx->customMem); + dctx->ddictSet = NULL; + } ZSTD_DCtx_resetParameters(dctx); } return 0; diff --git a/lib/zstd.h b/lib/zstd.h index 97fef316fd6..ccf44d0cb28 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -1167,8 +1167,9 @@ ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, s * If called with ZSTD_d_refMultipleDDicts enabled, repeated calls of this function * will store the DDict references in a table, and the DDict used for decompression * will be determined at decompression time, as per the dict ID in the frame. - * The memory for the table is allocated on the first call to refDDict, and can be - * freed with ZSTD_freeDCtx(). + * The memory for the table is allocated on the first call to refDDict. + * The table is freed by ZSTD_DCtx_refDDict(dctx, NULL), by ZSTD_DCtx_reset() + * with ZSTD_reset_parameters or ZSTD_reset_session_and_parameters, and by ZSTD_freeDCtx(). * * If called with ZSTD_d_refMultipleDDicts disabled (the default), only one dictionary * will be managed, and referencing a dictionary effectively "discards" any previous one. diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 32360b9a58d..0ff1c582778 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -3941,6 +3941,43 @@ static int basicUnitTests(U32 const seed, double compressibility) } DISPLAYLEVEL(3, "OK (ASAN would report OOB write if vulnerable)\n"); + DISPLAYLEVEL(3, "test%3i : DDict hashset is dropped by reset and refDDict(NULL) : ", testNb++); + { /* After a parameter reset or refDDict(NULL), a frame must not select + * a DDict referenced before, which the caller may have freed since */ + char* const dictBufA = (char*)malloc(dictBufferFixedSize); + ZSTD_DDict* const ddictB = ZSTD_createDDict(dictBufferFixed, dictBufferFixedSize); + ZSTD_DCtx* const dctx = ZSTD_createDCtx(); + size_t frameSize; + int n; + if (!dictBufA || !ddictB || !dctx) goto _output_error; + ZSTD_memcpy(dictBufA, dictBufferFixed, dictBufferFixedSize); + MEM_writeLE32(dictBufA + ZSTD_FRAMEIDSIZE, ZSTD_getDictID_fromDDict(ddictB) + 1); + frameSize = ZSTD_compress_usingDict(cctx, compressedBuffer, compressedBufferSize, + CNBuffer, 100, dictBufA, dictBufferFixedSize, 1); + CHECK_Z(frameSize); + for (n = 0; n < 2; n++) { + ZSTD_DDict* const ddictA = ZSTD_createDDict(dictBufA, dictBufferFixedSize); + if (!ddictA) goto _output_error; + CHECK_Z( ZSTD_DCtx_setParameter(dctx, ZSTD_d_refMultipleDDicts, ZSTD_rmd_refMultipleDDicts) ); + CHECK_Z( ZSTD_DCtx_refDDict(dctx, ddictA) ); + if (n == 0) { + CHECK_Z( ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters) ); + } else { + CHECK_Z( ZSTD_DCtx_refDDict(dctx, NULL) ); + } + ZSTD_freeDDict(ddictA); + /* only ddictB is referenced now : the frame needs ddictA, so it must fail */ + CHECK_Z( ZSTD_DCtx_setParameter(dctx, ZSTD_d_refMultipleDDicts, ZSTD_rmd_refMultipleDDicts) ); + CHECK_Z( ZSTD_DCtx_refDDict(dctx, ddictB) ); + if (!ZSTD_isError(ZSTD_decompressDCtx(dctx, decodedBuffer, CNBuffSize, compressedBuffer, frameSize))) + goto _output_error; + } + ZSTD_freeDCtx(dctx); + ZSTD_freeDDict(ddictB); + free(dictBufA); + } + DISPLAYLEVEL(3, "OK \n"); + ZSTD_freeCCtx(cctx); free(dictBuffer); free(samplesSizes);