Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/dictBuilder/zdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
67 changes: 67 additions & 0 deletions tests/fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<nbSamples; u++) samplesSizes[u] = sampleUnitSize; }

/* negative checks: reject out-of-bounds capacities and null destination */
res = ZDICT_addEntropyTablesFromBuffer(smallBuf, 0, 7, CNBuffer, samplesSizes, nbSamples);
if (!ZDICT_isError(res) || ZSTD_getErrorCode(res) != ZSTD_error_dstSize_tooSmall) {
free(smallBuf);
free(contentOverflowBuf);
free(validBuf);
goto _output_error;
}
res = ZDICT_addEntropyTablesFromBuffer(smallBuf, 0, 0, CNBuffer, samplesSizes, nbSamples);
if (!ZDICT_isError(res) || ZSTD_getErrorCode(res) != ZSTD_error_dstSize_tooSmall) {
free(smallBuf);
free(contentOverflowBuf);
free(validBuf);
goto _output_error;
}
res = ZDICT_addEntropyTablesFromBuffer(contentOverflowBuf, 17, 16, CNBuffer, samplesSizes, nbSamples);
if (!ZDICT_isError(res) || ZSTD_getErrorCode(res) != ZSTD_error_dstSize_tooSmall) {
free(smallBuf);
free(contentOverflowBuf);
free(validBuf);
goto _output_error;
}
res = ZDICT_addEntropyTablesFromBuffer(NULL, 0, 0, CNBuffer, samplesSizes, nbSamples);
if (!ZDICT_isError(res) || ZSTD_getErrorCode(res) != ZSTD_error_dstSize_tooSmall) {
free(smallBuf);
free(contentOverflowBuf);
free(validBuf);
goto _output_error;
}

/* positive control: valid capacity and trailing dictionary content */
memset(validBuf, 0, validCapacity);
memcpy(validBuf + validCapacity - validContentSize, CNBuffer, validContentSize);
res = ZDICT_addEntropyTablesFromBuffer(validBuf, validContentSize, validCapacity,
CNBuffer, samplesSizes, nbSamples);
if (ZDICT_isError(res) || res > 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,
Expand Down