fix compressing into NULL
authorYann Collet <cyan@fb.com>
Sat, 26 Sep 2020 18:31:57 +0000 (11:31 -0700)
committerYann Collet <cyan@fb.com>
Sat, 26 Sep 2020 18:31:57 +0000 (11:31 -0700)
fails properly
bug discovered by oss-fuzz

lib/lz4.c
tests/fuzzer.c

index 2222d53f053742f8974560e6037742750174bcff..d21eef47657dc2692265646c5c9b33e0b7dd8f6d 100644 (file)
--- a/lib/lz4.c
+++ b/lib/lz4.c
@@ -1236,6 +1236,7 @@ LZ4_FORCE_INLINE int LZ4_compress_generic(
 
     if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; }  /* Unsupported srcSize, too large (or negative) */
     if (srcSize == 0) {   /* src == NULL supported if srcSize == 0 */
+        if (outputDirective != notLimited && dstCapacity <= 0) return 0;  /* no output, can't write anything */
         DEBUGLOG(5, "Generating an empty block");
         assert(outputDirective == notLimited || dstCapacity >= 1);
         assert(dst != NULL);
index 1d8b5f617ae21135f2d236edb864fb3909d6d2e7..4dbf6a7cfc03465941e6cb7411a3d67d5acb6259 100644 (file)
@@ -1056,15 +1056,24 @@ static void FUZ_unitTests(int compressionLevel)
     }   }
 
 
-    /* useful to trigger undefined sanitizer */
-    DISPLAYLEVEL(3, "LZ4_compress_default() with NULL input \n");
+    /* to be tested with undefined sanitizer */
+    DISPLAYLEVEL(3, "LZ4_compress_default() with NULL input:");
     {  int const maxCSize = LZ4_compressBound(0);
         int const cSize = LZ4_compress_default(NULL, testCompressed, 0, maxCSize);
         FUZ_CHECKTEST(!(cSize==1 && testCompressed[0]==0),
                     "compressing empty should give byte 0"
-                    " (maxCSize == %u) (cSize == %u)",
-                    (unsigned)maxCSize, (unsigned)cSize);
+                    " (maxCSize == %i) (cSize == %i) (byte == 0x%02X)",
+                    maxCSize, cSize, testCompressed[0]);
     }
+    DISPLAYLEVEL(3, " OK \n");
+
+    DISPLAYLEVEL(3, "LZ4_compress_default() with both NULL input and output:");
+    {  int const cSize = LZ4_compress_default(NULL, NULL, 0, 0);
+        FUZ_CHECKTEST(cSize != 0,
+                    "compressing into NULL must fail"
+                    " (cSize == %i !=  0)", cSize);
+    }
+    DISPLAYLEVEL(3, " OK \n");
 
     /* in-place compression test */
     DISPLAYLEVEL(3, "in-place compression using LZ4_compress_default() :");