From f8b760503445610c43cad813fb3eda29bb3fc763 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 16:49:01 -0700 Subject: fixed minor Visual warnings since Visual 2017, worries about potential overflow, which are actually impossible. Replaced (c * a) by (c ? a : 0). Will likely replaced a * by a cmov. Probably harmless for performance. --- lib/lz4.c | 4 ++-- lib/lz4frame.c | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/lz4.c b/lib/lz4.c index ca3684f..031f8c1 100644 --- a/lib/lz4.c +++ b/lib/lz4.c @@ -983,7 +983,7 @@ _next_match: assert(dictEnd > match); if (limit > matchlimit) limit = matchlimit; matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); - ip += MINMATCH + matchCode; + ip += (size_t)matchCode + MINMATCH; if (ip==limit) { unsigned const more = LZ4_count(limit, (const BYTE*)source, matchlimit); matchCode += more; @@ -992,7 +992,7 @@ _next_match: DEBUGLOG(6, " with matchLength=%u starting in extDict", matchCode+MINMATCH); } else { matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); - ip += MINMATCH + matchCode; + ip += (size_t)matchCode + MINMATCH; DEBUGLOG(6, " with matchLength=%u", matchCode+MINMATCH); } diff --git a/lib/lz4frame.c b/lib/lz4frame.c index 2b31a0d..b6bc218 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -194,10 +194,10 @@ static void LZ4F_writeLE64 (void* dst, U64 value64) /*-************************************ * Constants **************************************/ -#ifndef LZ4_SRC_INCLUDED -#define KB *(1<<10) -#define MB *(1<<20) -#define GB *(1<<30) +#ifndef LZ4_SRC_INCLUDED /* avoid double definition */ +# define KB *(1<<10) +# define MB *(1<<20) +# define GB *(1<<30) #endif #define _1BIT 0x01 @@ -644,8 +644,8 @@ size_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); { size_t const requiredBuffSize = preferencesPtr->autoFlush ? - (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) * 64 KB : /* only needs windows size */ - cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) * 128 KB); + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 64 KB : 0) : /* only needs past data up to window size */ + cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 128 KB : 0); if (cctxPtr->maxBufferSize < requiredBuffSize) { cctxPtr->maxBufferSize = 0; @@ -1146,7 +1146,7 @@ static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize } /* Frame Header Size */ - frameHeaderSize = minFHSize + (contentSizeFlag*8) + (dictIDFlag*4); + frameHeaderSize = minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0); if (srcSize < frameHeaderSize) { /* not enough input to fully decode frame header */ @@ -1215,7 +1215,7 @@ size_t LZ4F_headerSize(const void* src, size_t srcSize) { BYTE const FLG = ((const BYTE*)src)[4]; U32 const contentSizeFlag = (FLG>>3) & _1BIT; U32 const dictIDFlag = FLG & _1BIT; - return minFHSize + (contentSizeFlag*8) + (dictIDFlag*4); + return minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0); } } @@ -1421,11 +1421,11 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0); /* internal buffers allocation */ { size_t const bufferNeeded = dctx->maxBlockSize - + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB); + + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) ? 128 KB : 0); if (bufferNeeded > dctx->maxBufferSize) { /* tmp buffers too small */ dctx->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/ FREEMEM(dctx->tmpIn); - dctx->tmpIn = (BYTE*)ALLOC(dctx->maxBlockSize + 4 /* block checksum */); + dctx->tmpIn = (BYTE*)ALLOC(dctx->maxBlockSize + BFSize /* block checksum */); if (dctx->tmpIn == NULL) return err0r(LZ4F_ERROR_allocation_failed); FREEMEM(dctx->tmpOutBuffer); @@ -1526,7 +1526,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, } dctx->tmpInTarget -= sizeToCopy; /* need to copy more */ nextSrcSizeHint = dctx->tmpInTarget + - + dctx->frameInfo.blockChecksumFlag * 4 /* size for block checksum */ + +(dctx->frameInfo.blockChecksumFlag ? BFSize : 0) + BHSize /* next header size */; doAnotherStage = 0; break; @@ -1578,7 +1578,7 @@ size_t LZ4F_decompress(LZ4F_dctx* dctx, srcPtr += sizeToCopy; if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */ nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) - + dctx->frameInfo.blockChecksumFlag * 4 /* size for block checksum */ + + (dctx->frameInfo.blockChecksumFlag ? BFSize : 0) + BHSize /* next header size */; doAnotherStage = 0; break; -- cgit v0.12 From dd43b913a292e5404aa8bc981ad11223f216d898 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 16:56:22 -0700 Subject: fix minor visual warning yet some overly cautious overflow risk flag, while it's actually impossible, due to previous test just one line above. Changing the cast position, just to be please the thing. --- lib/lz4hc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lz4hc.c b/lib/lz4hc.c index a6dc7a2..4e3573a 100644 --- a/lib/lz4hc.c +++ b/lib/lz4hc.c @@ -458,7 +458,7 @@ LZ4_FORCE_INLINE int LZ4HC_encodeSequence ( /* Encode MatchLength */ assert(matchLength >= MINMATCH); - length = (size_t)(matchLength - MINMATCH); + length = (size_t)matchLength - MINMATCH; if ((limit) && (*op + (length / 255) + (1 + LASTLITERALS) > oend)) return 1; /* Check output limit */ if (length >= ML_MASK) { *token += ML_MASK; @@ -979,7 +979,7 @@ int LZ4_loadDictHC (LZ4_streamHC_t* LZ4_streamHCPtr, DEBUGLOG(4, "LZ4_loadDictHC(%p, %p, %d)", LZ4_streamHCPtr, dictionary, dictSize); assert(LZ4_streamHCPtr != NULL); if (dictSize > 64 KB) { - dictionary += dictSize - 64 KB; + dictionary += (size_t)dictSize - 64 KB; dictSize = 64 KB; } /* need a full initialization, there are bad side-effects when using resetFast() */ -- cgit v0.12 From c7554c3004e11b3d706143917f33783b1a2e9a29 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 17:03:28 -0700 Subject: fixed minor Visual conversion warnings --- programs/util.h | 12 ++++++------ tests/fullbench.c | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/programs/util.h b/programs/util.h index 4aba6a3..1385620 100644 --- a/programs/util.h +++ b/programs/util.h @@ -391,11 +391,11 @@ UTIL_STATIC void* UTIL_realloc(void* ptr, size_t size) UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd) { char* path; - int dirLength, nbFiles = 0; + size_t dirLength, nbFiles = 0; WIN32_FIND_DATAA cFile; HANDLE hFile; - dirLength = (int)strlen(dirName); + dirLength = strlen(dirName); path = (char*) malloc(dirLength + 3); if (!path) return 0; @@ -412,7 +412,7 @@ UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_ free(path); do { - int pathLength; + size_t pathLength; int const fnameLength = (int)strlen(cFile.cFileName); path = (char*) malloc(dirLength + fnameLength + 2); if (!path) { FindClose(hFile); return 0; } @@ -553,15 +553,15 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** alloc } } else { char* bufend = buf + bufSize; - nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); + nbFiles += (unsigned)UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); if (buf == NULL) return NULL; assert(bufend > buf); - bufSize = bufend - buf; + bufSize = (size_t)(bufend - buf); } } if (nbFiles == 0) { free(buf); return NULL; } - fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*)); + fileTable = (const char**)malloc(((size_t)nbFiles+1) * sizeof(const char*)); if (!fileTable) { free(buf); return NULL; } for (i=0, pos=0; i Date: Fri, 12 Apr 2019 17:06:53 -0700 Subject: some more Visual conversion warning --- programs/util.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/programs/util.h b/programs/util.h index 1385620..85ac87f 100644 --- a/programs/util.h +++ b/programs/util.h @@ -45,6 +45,7 @@ extern "C" { # include /* utime */ #endif #include /* time */ +#include /* INT_MAX */ #include @@ -445,7 +446,8 @@ UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_ } while (FindNextFileA(hFile, &cFile)); FindClose(hFile); - return nbFiles; + assert(nbFiles < INT_MAX); + return (int)nbFiles; } #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ -- cgit v0.12 From 63bfb64b80a6b9b310961c04cadd5242c81df0c8 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 12 Apr 2019 17:40:23 -0700 Subject: and even more visual conversion warnings --- programs/util.h | 6 +++++- tests/fuzzer.c | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/programs/util.h b/programs/util.h index 85ac87f..6a35481 100644 --- a/programs/util.h +++ b/programs/util.h @@ -571,7 +571,11 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** alloc pos += strlen(fileTable[i]) + 1; } - if (pos > bufSize) { free(buf); free((void*)fileTable); return NULL; } /* can this happen ? */ + if (pos > bufSize) { + free(buf); + free((void*)fileTable); + return NULL; + } /* can this happen ? */ *allocatedBuffer = buf; *allocatedNamesNb = nbFiles; diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 27d8d9c..8e25615 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -145,10 +145,10 @@ static void FUZ_fillCompressibleNoiseBuffer(void* buffer, size_t bufferSize, dou /* Select : Literal (noise) or copy (within 64K) */ if (FUZ_RAND15BITS < P32) { /* Copy (within 64K) */ - size_t const length = FUZ_RANDLENGTH + 4; + size_t const length = (size_t)FUZ_RANDLENGTH + 4; size_t const d = MIN(pos+length, bufferSize); size_t match; - size_t offset = FUZ_RAND15BITS + 1; + size_t offset = (size_t)FUZ_RAND15BITS + 1; while (offset > pos) offset >>= 1; match = pos - offset; while (pos < d) BBuffer[pos++] = BBuffer[match++]; @@ -667,7 +667,7 @@ static int FUZ_test(U32 seed, U32 nbCycles, const U32 startCycle, const double c /* Compress using External dictionary */ FUZ_DISPLAYTEST("test LZ4_compress_fast_continue(), with non-contiguous dictionary"); - dict -= (FUZ_rand(&randState) & 0xF) + 1; /* create space, so now dictionary is an ExtDict */ + dict -= (size_t)(FUZ_rand(&randState) & 0xF) + 1; /* create space, so now dictionary is an ExtDict */ if (dict < (char*)CNBuffer) dict = (char*)CNBuffer; LZ4_loadDict(&LZ4dict, dict, dictSize); blockContinueCompressedSize = LZ4_compress_fast_continue(&LZ4dict, block, compressedBuffer, blockSize, (int)compressedBufferSize, 1); @@ -1145,7 +1145,7 @@ static void FUZ_unitTests(int compressionLevel) result1 = LZ4_compress_HC_continue(&sHC, testInput + segSize, testCompressed, segSize, segSize -1); FUZ_CHECKTEST(result1==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result1); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); - result2 = LZ4_compress_HC_continue(&sHC, testInput + 2*segSize, testCompressed+result1, segSize, segSize-1); + result2 = LZ4_compress_HC_continue(&sHC, testInput + 2*(size_t)segSize, testCompressed+result1, segSize, segSize-1); FUZ_CHECKTEST(result2==0, "LZ4_compressHC_limitedOutput_continue() dictionary compression failed : result = %i", result2); FUZ_CHECKTEST(sHC.internal_donotuse.dirty, "Context should be clean"); @@ -1178,7 +1178,7 @@ static void FUZ_unitTests(int compressionLevel) int dictSize = (FUZ_rand(&randState) & 8191); char* dst = testVerify; - size_t segStart = dictSize + 7; + size_t segStart = (size_t)dictSize + 7; int segSize = (FUZ_rand(&randState) & 8191); int segNb = 1; @@ -1203,13 +1203,14 @@ static void FUZ_unitTests(int compressionLevel) FUZ_CHECKTEST(crcOrig!=crcNew, "LZ4_decompress_safe_usingDict() part %i corruption", segNb); } + assert(segSize >= 0); dict = dst; dictSize = segSize; - dst += segSize + 1; + dst += (size_t)segSize + 1; segNb ++; - segStart += segSize + (FUZ_rand(&randState) & 0xF) + 1; + segStart += (size_t)segSize + (FUZ_rand(&randState) & 0xF) + 1; segSize = (FUZ_rand(&randState) & 8191); } } @@ -1288,7 +1289,7 @@ static void FUZ_unitTests(int compressionLevel) int dNext = 0; int compressedSize; - assert((size_t)(dBufferSize + 1 + dBufferSize) < testVerifySize); /* space used by ringBufferSafe and ringBufferFast */ + assert((size_t)dBufferSize * 2 + 1 < testVerifySize); /* space used by ringBufferSafe and ringBufferFast */ XXH64_reset(&xxhOrig, 0); XXH64_reset(&xxhNewSafe, 0); XXH64_reset(&xxhNewFast, 0); @@ -1323,7 +1324,8 @@ static void FUZ_unitTests(int compressionLevel) /* prepare second message */ dNext += messageSize; - totalMessageSize += messageSize; + assert(messageSize >= 0); + totalMessageSize += (unsigned)messageSize; messageSize = maxMessageSize; iNext = BSIZE1+1; assert(BSIZE1 >= 65535); -- cgit v0.12