From 21c273da846f5b6ad76e118b8d6d24b8703c6711 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 16 Jul 2020 20:25:15 -0700 Subject: [PATCH 1/2] import some minor fixes from FSE project --- lib/common/entropy_common.c | 3 +-- lib/common/fse.h | 11 +++++++---- lib/common/fse_decompress.c | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/common/entropy_common.c b/lib/common/entropy_common.c index 9d3e4e8e3..6b825afe5 100644 --- a/lib/common/entropy_common.c +++ b/lib/common/entropy_common.c @@ -54,8 +54,7 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t if (hbSize < 4) { /* This function only works when hbSize >= 4 */ - char buffer[4]; - memset(buffer, 0, sizeof(buffer)); + char buffer[4] = {0}; memcpy(buffer, headerBuffer, hbSize); { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr, buffer, sizeof(buffer)); diff --git a/lib/common/fse.h b/lib/common/fse.h index ff54e70ea..55dd8f3ae 100644 --- a/lib/common/fse.h +++ b/lib/common/fse.h @@ -288,12 +288,12 @@ If there is an error, the function will return an error code, which can be teste *******************************************/ /* FSE buffer bounds */ #define FSE_NCOUNTBOUND 512 -#define FSE_BLOCKBOUND(size) (size + (size>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */) +#define FSE_BLOCKBOUND(size) ((size) + ((size)>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */) #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */ /* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */ -#define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2)) -#define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1< FSE_MAX_MEMORY_USAGE) +# error "FSE_DEFAULT_MEMORY_USAGE must be <= FSE_MAX_MEMORY_USAGE" +#endif /*!FSE_MAX_SYMBOL_VALUE : * Maximum symbol value authorized. @@ -677,7 +680,7 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr) # error "FSE_MAX_TABLELOG > FSE_TABLELOG_ABSOLUTE_MAX is not supported" #endif -#define FSE_TABLESTEP(tableSize) ((tableSize>>1) + (tableSize>>3) + 3) +#define FSE_TABLESTEP(tableSize) (((tableSize)>>1) + ((tableSize)>>3) + 3) #endif /* FSE_STATIC_LINKING_ONLY */ diff --git a/lib/common/fse_decompress.c b/lib/common/fse_decompress.c index bcc2223cc..54dab2550 100644 --- a/lib/common/fse_decompress.c +++ b/lib/common/fse_decompress.c @@ -18,6 +18,7 @@ ****************************************************************/ #include /* malloc, free, qsort */ #include /* memcpy, memset */ +#include "debug.h" /* assert */ #include "bitstream.h" #include "compiler.h" #define FSE_STATIC_LINKING_ONLY @@ -262,8 +263,8 @@ size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size /* normal FSE decoding mode */ size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize); if (FSE_isError(NCountLength)) return NCountLength; - /* if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); */ /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */ if (tableLog > maxLog) return ERROR(tableLog_tooLarge); + assert(NCountLength <= cSrcSize); ip += NCountLength; cSrcSize -= NCountLength; From c224367edec9052d22af101034d69bcecb22bcb7 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 16 Jul 2020 20:33:50 -0700 Subject: [PATCH 2/2] ensure workspace is large enough even when MAX_TABLELOG is reduced --- lib/compress/fse_compress.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/compress/fse_compress.c b/lib/compress/fse_compress.c index a42759814..5290a918b 100644 --- a/lib/compress/fse_compress.c +++ b/lib/compress/fse_compress.c @@ -678,7 +678,10 @@ size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t src typedef struct { FSE_CTable CTable_max[FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)]; - BYTE scratchBuffer[1 << FSE_MAX_TABLELOG]; + union { + U32 hist_wksp[HIST_WKSP_SIZE_U32]; + BYTE scratchBuffer[1 << FSE_MAX_TABLELOG]; + } workspace; } fseWkspMax_t; size_t FSE_compress2 (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog)