diff --git a/lib/dictBuilder/zdict.c b/lib/dictBuilder/zdict.c index befa616b87f..3640995a92c 100644 --- a/lib/dictBuilder/zdict.c +++ b/lib/dictBuilder/zdict.c @@ -950,6 +950,10 @@ static size_t ZDICT_addEntropyTablesFromBuffer_advanced( U32 const notificationLevel = params.notificationLevel; size_t hSize = 8; + /* check conditions */ + if ((dictBuffer == NULL) || (dictBufferCapacity < dictContentSize) || (dictBufferCapacity < hSize)) + return ERROR(dstSize_tooSmall); + /* calculate entropy tables */ DISPLAYLEVEL(2, "\r%70s\r", ""); /* clean display line */ DISPLAYLEVEL(2, "statistics ... \n"); diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 32360b9a58d..b6aa56f2d6f 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -34,6 +34,7 @@ #define ZSTD_DISABLE_DEPRECATE_WARNINGS /* No deprecation warnings, we still test some deprecated functions */ #include "zstd.h" /* ZSTD_VERSION_STRING */ #include "zstd_errors.h" /* ZSTD_getErrorCode */ +#define ZDICT_DISABLE_DEPRECATE_WARNINGS /* No deprecation warnings, we still test deprecated functions */ #define ZDICT_STATIC_LINKING_ONLY #include "zdict.h" /* ZDICT_trainFromBuffer */ #include "mem.h" @@ -3136,6 +3137,72 @@ static int basicUnitTests(U32 const seed, double compressibility) } DISPLAYLEVEL(3, "OK : %u \n", (unsigned)dictHeaderSize); + DISPLAYLEVEL(3, "test%3i : ZDICT_addEntropyTablesFromBuffer capacity bounds : ", testNb++); + { size_t const validCapacity = 1024; + size_t const validContentSize = 128; + BYTE* const smallBuf = (BYTE*)malloc(7); + BYTE* const contentOverflowBuf = (BYTE*)malloc(16); + BYTE* const validBuf = (BYTE*)malloc(validCapacity); + size_t res; + if (smallBuf == NULL || contentOverflowBuf == NULL || validBuf == NULL) { + free(smallBuf); + free(contentOverflowBuf); + free(validBuf); + goto _output_error; + } + memset(smallBuf, 0, 7); + memset(contentOverflowBuf, 0, 16); + { U32 u; for (u=0; u validCapacity || ZDICT_getDictID(validBuf, res) == 0) { + free(smallBuf); + free(contentOverflowBuf); + free(validBuf); + goto _output_error; + } + assert(ZDICT_getDictID(validBuf, res) != 0); + + free(smallBuf); + free(contentOverflowBuf); + free(validBuf); + } + DISPLAYLEVEL(3, "OK \n"); + DISPLAYLEVEL(3, "test%3i : compress with dictionary : ", testNb++); cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize,