Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix NCountWriteBound #2779

Merged
merged 2 commits into from
Sep 22, 2021
Merged
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
6 changes: 5 additions & 1 deletion lib/compress/fse_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ size_t FSE_buildCTable_wksp(FSE_CTable* ct,
****************************************************************/
size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)
{
size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog) >> 3) + 3;
size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog
+ 4 /* bitCount initialized at 4 */
+ 2 /* first two symbols may use one additional bit each */) / 8)
+ 1 /* round up to whole nb bytes */
+ 2 /* additional two bytes for bitstream flush */;
return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND; /* maxSymbolValue==0 ? use default */
}

Expand Down
17 changes: 17 additions & 0 deletions tests/fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3353,6 +3353,23 @@ static int basicUnitTests(U32 const seed, double compressibility)
FSE_normalizeCount(norm, tableLog, count, nbSeq, maxSymbolValue, /* useLowProbCount */ 1);
}
DISPLAYLEVEL(3, "OK \n");

DISPLAYLEVEL(3, "test%3i : testing FSE_writeNCount() PR#2779: ", testNb++);
{
size_t const outBufSize = 9;
short const count[11] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 9, 18};
unsigned const tableLog = 5;
unsigned const maxSymbolValue = 10;
BYTE* outBuf = (BYTE*)malloc(outBufSize*sizeof(BYTE));

/* Ensure that this write doesn't write out of bounds, and that
* FSE_writeNCount_generic() is *not* called with writeIsSafe == 1.
*/
FSE_writeNCount(outBuf, outBufSize, count, maxSymbolValue, tableLog);
free(outBuf);
}
DISPLAYLEVEL(3, "OK \n");

#ifdef ZSTD_MULTITHREAD
DISPLAYLEVEL(3, "test%3i : passing wrong full dict should fail on compressStream2 refPrefix ", testNb++);
{ ZSTD_CCtx* cctx = ZSTD_createCCtx();
Expand Down