better visual conformance
authorYann Collet <cyan@fb.com>
Sun, 15 Nov 2020 00:24:36 +0000 (16:24 -0800)
committerYann Collet <cyan@fb.com>
Sun, 15 Nov 2020 00:27:57 +0000 (16:27 -0800)
only include <intrin.h> on vs2005+ (#947)
remove some useless #pragma
fix a few minor Visual warnings

lib/lz4.c
programs/util.h
tests/checkFrame.c
tests/frametest.c
tests/fuzzer.c

index 5c6cf4dc14939d3075ba28a2c9ce9f7c56d49863..9f5e9bfa0839f8e1347d2abb3d867b21ff740215 100644 (file)
--- a/lib/lz4.c
+++ b/lib/lz4.c
 /*-************************************
 *  Compiler Options
 **************************************/
-#ifdef _MSC_VER    /* Visual Studio */
-#  include <intrin.h>
-#  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
-#  pragma warning(disable : 4293)        /* disable: C4293: too large shift (32-bits) */
+#if defined(_MSC_VER) && (_MSC_VER >= 1400)  /* Visual Studio 2005+ */
+#  include <intrin.h>               /* only present in VS2005+ */
+#  pragma warning(disable : 4127)   /* disable: C4127: conditional expression is constant */
 #endif  /* _MSC_VER */
 
 #ifndef LZ4_FORCE_INLINE
index 2f3d0a822de1e7c2d443572127373c6384996632..733c1cadb9a2bbc841575284ca51674339fb8b86 100644 (file)
@@ -594,15 +594,15 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb,
 
     for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
         if (!UTIL_isDirectory(inputNames[i])) {
-            size_t const len = strlen(inputNames[i]);
+            size_t const len = strlen(inputNames[i]) + 1;  /* include nul char */
             if (pos + len >= bufSize) {
                 while (pos + len >= bufSize) bufSize += LIST_SIZE_INCREASE;
                 buf = (char*)UTIL_realloc(buf, bufSize);
                 if (!buf) return NULL;
             }
             assert(pos + len < bufSize);
-            strncpy(buf + pos, inputNames[i], bufSize - pos);
-            pos += len + 1;
+            memcpy(buf + pos, inputNames[i], len);
+            pos += len;
             nbFiles++;
         } else {
             char* bufend = buf + bufSize;
index f39d2acffb48769d091b7c4c2175259b19363bb3..f9a1c142188d45eb04c47727f11ab48a8c8629a9 100644 (file)
       - LZ4 source repository : https://github.com/lz4/lz4
   */
 
-  /*-************************************
-  *  Compiler specific
-  **************************************/
-  #ifdef _MSC_VER    /* Visual Studio */
-  #  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
-  #  pragma warning(disable : 4146)        /* disable: C4146: minus unsigned expression */
-  #endif
-
-
   /*-************************************
   *  Includes
   **************************************/
index 2633c901302daa9593a369efe8db408be4699ef8..241fd5fb929da807bd75fa8b20cdcf2c616d5cce 100644 (file)
@@ -27,8 +27,7 @@
 *  Compiler specific
 **************************************/
 #ifdef _MSC_VER    /* Visual Studio */
-#  pragma warning(disable : 4127)     /* disable: C4127: conditional expression is constant */
-#  pragma warning(disable : 4146)     /* disable: C4146: minus unsigned expression */
+#  pragma warning(disable : 26451)     /* disable: Arithmetic overflow */
 #endif
 
 
index ba36621c06b30a2a46efc663178a8fca6a6b57e6..a824813dc335d9c9cec331c75e378b507d269c14 100644 (file)
@@ -1097,7 +1097,7 @@ static void FUZ_unitTests(int compressionLevel)
         {   int const dSize = LZ4_decompress_safe(testCompressed, testVerify, cSize, testInputSize);
             assert(dSize == sampleSize);   /* correct size */
             {   XXH32_hash_t const crcCheck = XXH32(testVerify, (size_t)dSize, 0);
-                assert(crcCheck == crcOrig);
+                FUZ_CHECKTEST(crcCheck != crcOrig, "LZ4_decompress_safe decompression corruption");
     }   }   }
     DISPLAYLEVEL(3, " OK \n");
 
@@ -1121,7 +1121,7 @@ static void FUZ_unitTests(int compressionLevel)
                 {   int const dSize = LZ4_decompress_safe(startInput, testVerify, cSize, sampleSize);
                     assert(dSize == sampleSize);   /* correct size */
                     {   XXH64_hash_t const crcCheck = XXH64(testVerify, (size_t)dSize, 0);
-                        assert(crcCheck == crcOrig);
+                        FUZ_CHECKTEST(crcCheck != crcOrig, "LZ4_decompress_safe decompression corruption");
     }   }   }   }   }
     DISPLAYLEVEL(3, " OK \n");
 
@@ -1173,12 +1173,12 @@ static void FUZ_unitTests(int compressionLevel)
             assert(ctx != NULL);  /* ensure init is successful */
 
             /* Check access violation with asan */
-            FUZ_CHECKTEST( LZ4_saveDict(&streamingState, NULL, 0) != 0,
+            FUZ_CHECKTEST( LZ4_saveDict(ctx, NULL, 0) != 0,
             "LZ4_saveDict() can't save anything into (NULL,0)");
 
             /* Check access violation with asan */
             {   char tmp_buffer[240] = { 0 };
-                FUZ_CHECKTEST( LZ4_saveDict(&streamingState, tmp_buffer, sizeof(tmp_buffer)) != 0,
+                FUZ_CHECKTEST( LZ4_saveDict(ctx, tmp_buffer, sizeof(tmp_buffer)) != 0,
                 "LZ4_saveDict() can't save anything since compression hasn't started");
         }   }
         DISPLAYLEVEL(3, "OK \n");
@@ -1290,12 +1290,12 @@ static void FUZ_unitTests(int compressionLevel)
             assert(ctx != NULL);  /* ensure init is successful */
 
             /* Check access violation with asan */
-            FUZ_CHECKTEST( LZ4_saveDictHC(&sHC, NULL, 0) != 0,
+            FUZ_CHECKTEST( LZ4_saveDictHC(ctx, NULL, 0) != 0,
             "LZ4_saveDictHC() can't save anything into (NULL,0)");
 
             /* Check access violation with asan */
             {   char tmp_buffer[240] = { 0 };
-                FUZ_CHECKTEST( LZ4_saveDictHC(&sHC, tmp_buffer, sizeof(tmp_buffer)) != 0,
+                FUZ_CHECKTEST( LZ4_saveDictHC(ctx, tmp_buffer, sizeof(tmp_buffer)) != 0,
                 "LZ4_saveDictHC() can't save anything since compression hasn't started");
         }   }
         DISPLAYLEVEL(3, "OK \n");
@@ -1596,7 +1596,7 @@ static void FUZ_unitTests(int compressionLevel)
 
     DISPLAYLEVEL(3, "LZ4_compress_HC_destSize : ");
     /* encode congenerical sequence test for HC compressors */
-    {   LZ4_streamHC_t sHC;   /* statically allocated */
+    {   LZ4_streamHC_t* const sHC = LZ4_createStreamHC();
         int const src_buf_size = 3 MB;
         int const dst_buf_size = 6 KB;
         int const payload = 0;
@@ -1609,6 +1609,7 @@ static void FUZ_unitTests(int compressionLevel)
         char* dbuf1 = (char*)malloc(dst_buf_size + 1);
         char* dbuf2 = (char*)malloc(dst_buf_size + 1);
 
+        assert(sHC != NULL);
         assert(dst_buf_size > dst_max_len);
         if (!sbuf1 || !sbuf2 || !dbuf1 || !dbuf2) {
             EXIT_MSG("not enough memory for FUZ_unitTests (destSize)");
@@ -1643,8 +1644,8 @@ static void FUZ_unitTests(int compressionLevel)
                 memset(sbuf2, payload, slen);
                 memset(dbuf2, 0, dlen);
                 dbuf2[dlen] = endchk;
-                LZ4_resetStreamHC(&sHC, compressionLevel);
-                dsz2 = LZ4_compress_HC_destSize(&sHC, sbuf2, dbuf2, &srcsz2, dlen, compressionLevel);
+                LZ4_resetStreamHC(sHC, compressionLevel);
+                dsz2 = LZ4_compress_HC_destSize(sHC, sbuf2, dbuf2, &srcsz2, dlen, compressionLevel);
                 DISPLAYLEVEL(5, "LZ4_compress_HC_destSize: %i bytes compressed into %i bytes, ", srcsz2, dsz2);
                 DISPLAYLEVEL(5, "last token : 0x%0X, ", dbuf2[dsz2 - 6]);
                 DISPLAYLEVEL(5, "last ML extra lenbyte : 0x%0X, \n", dbuf2[dsz2 - 7]);
@@ -1663,6 +1664,7 @@ static void FUZ_unitTests(int compressionLevel)
                 FUZ_CHECKTEST(memcmp(sbuf1, sbuf2, (size_t)res2), "LZ4_compress_HC_destSize() decompression corruption!");
             }
         }
+        LZ4_freeStreamHC(sHC);
         free(sbuf1);
         free(sbuf2);
         free(dbuf1);