Fix undefined behavior when srcSize==1

This commit is contained in:
Stella Lau
2017-08-22 11:55:42 -07:00
parent 78c3d16bf4
commit e50ed1fa3a
3 changed files with 37 additions and 30 deletions
+3
View File
@@ -168,6 +168,8 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
* Internal functions * Internal functions
****************************************************************/ ****************************************************************/
MEM_STATIC unsigned BIT_highbit32 (register U32 val) MEM_STATIC unsigned BIT_highbit32 (register U32 val)
{
assert(val != 0);
{ {
# if defined(_MSC_VER) /* Visual */ # if defined(_MSC_VER) /* Visual */
unsigned long r=0; unsigned long r=0;
@@ -189,6 +191,7 @@ MEM_STATIC unsigned BIT_highbit32 (register U32 val)
return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27]; return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
# endif # endif
} }
}
/*===== Local Constants =====*/ /*===== Local Constants =====*/
static const unsigned BIT_mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, static const unsigned BIT_mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F,
+3
View File
@@ -267,6 +267,8 @@ void ZSTD_free(void* ptr, ZSTD_customMem customMem);
/*====== common function ======*/ /*====== common function ======*/
MEM_STATIC U32 ZSTD_highbit32(U32 val) MEM_STATIC U32 ZSTD_highbit32(U32 val)
{
assert(val != 0);
{ {
# if defined(_MSC_VER) /* Visual */ # if defined(_MSC_VER) /* Visual */
unsigned long r=0; unsigned long r=0;
@@ -287,6 +289,7 @@ MEM_STATIC U32 ZSTD_highbit32(U32 val)
return r; return r;
# endif # endif
} }
}
/* hidden functions */ /* hidden functions */
+2 -1
View File
@@ -497,7 +497,8 @@ ZSTD_compressionParameters ZSTD_adjustCParams_internal(ZSTD_compressionParameter
{ U32 const minSrcSize = (srcSize==0) ? 500 : 0; { U32 const minSrcSize = (srcSize==0) ? 500 : 0;
U64 const rSize = srcSize + dictSize + minSrcSize; U64 const rSize = srcSize + dictSize + minSrcSize;
if (rSize < ((U64)1<<ZSTD_WINDOWLOG_MAX)) { if (rSize < ((U64)1<<ZSTD_WINDOWLOG_MAX)) {
U32 const srcLog = MAX(ZSTD_HASHLOG_MIN, ZSTD_highbit32((U32)(rSize)-1) + 1); U32 const srcLog =
MAX(ZSTD_HASHLOG_MIN, (rSize==1) ? 1 : ZSTD_highbit32((U32)(rSize)-1) + 1);
if (cPar.windowLog > srcLog) cPar.windowLog = srcLog; if (cPar.windowLog > srcLog) cPar.windowLog = srcLog;
} } } }
if (cPar.hashLog > cPar.windowLog) cPar.hashLog = cPar.windowLog; if (cPar.hashLog > cPar.windowLog) cPar.hashLog = cPar.windowLog;