]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Added : fuzzer test, checking contentLength value after copyCCtx() usage
authorYann Collet <yann.collet.73@gmail.com>
Tue, 12 Apr 2016 16:00:20 +0000 (18:00 +0200)
committerYann Collet <yann.collet.73@gmail.com>
Tue, 12 Apr 2016 16:00:20 +0000 (18:00 +0200)
lib/zstd_static.h
programs/fuzzer.c

index 52dfae9d651aedb928a19d05a57a35e136e7b0a4..960808bc5c0fbd022dde5d5f4903021ac46a1c44 100644 (file)
@@ -200,7 +200,7 @@ ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t ds
   Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
   A ZSTD_DCtx object can be re-used multiple times.
 
-  First optional operation is to retrieve frame parameters, using ZSTD_getFrameParams().
+  First optional operation is to retrieve frame parameters, using ZSTD_getFrameParams(), which doesn't consume the input.
   It can provide the minimum size of rolling buffer required to properly decompress data,
   and optionally the final size of uncompressed content.
   (Note : content size is an optional info that may not be present. 0 means : content size unknown)
index c4b3118e8d8cfbd745a02f82f1df80ace9fc5c6d..74fe1cac12424f3c7d5439de5332c6249dddfbb1 100644 (file)
@@ -173,7 +173,7 @@ static int basicUnitTests(U32 seed, double compressibility)
     {   ZSTD_CCtx* ctxOrig = ZSTD_createCCtx();
         ZSTD_CCtx* ctxDuplicated = ZSTD_createCCtx();
         ZSTD_DCtx* dctx = ZSTD_createDCtx();
-        const size_t dictSize = 500;
+        size_t const dictSize = 500;
         size_t cSizeOrig;
 
         DISPLAYLEVEL(4, "test%3i : copy context too soon : ", testNb++);
@@ -206,7 +206,6 @@ static int basicUnitTests(U32 seed, double compressibility)
                                            CNBuffer, dictSize);
         if (ZSTD_isError(result)) goto _output_error;
         if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error;
-        ZSTD_freeCCtx(ctxOrig);   /* if ctxOrig is read, will produce segfault */
         DISPLAYLEVEL(4, "OK \n");
 
         DISPLAYLEVEL(4, "test%3i : compress with duplicated context : ", testNb++);
@@ -219,7 +218,6 @@ static int basicUnitTests(U32 seed, double compressibility)
         if (ZSTD_isError(result)) goto _output_error;
         cSize += result;
         if (cSize != cSizeOrig) goto _output_error;   /* should be identical == have same size */
-        ZSTD_freeCCtx(ctxDuplicated);
         DISPLAYLEVEL(4, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/COMPRESSIBLE_NOISE_LENGTH*100);
 
         DISPLAYLEVEL(4, "test%3i : frame built with duplicated context should be decompressible : ", testNb++);
@@ -229,8 +227,30 @@ static int basicUnitTests(U32 seed, double compressibility)
                                            CNBuffer, dictSize);
         if (ZSTD_isError(result)) goto _output_error;
         if (result != COMPRESSIBLE_NOISE_LENGTH - dictSize) goto _output_error;
-        ZSTD_freeDCtx(dctx);
         DISPLAYLEVEL(4, "OK \n");
+
+        DISPLAYLEVEL(4, "test%3i : check content size on duplicated context : ", testNb++);
+        {   size_t const testSize = COMPRESSIBLE_NOISE_LENGTH / 3;
+            {   ZSTD_parameters p;
+                p.cParams = ZSTD_getCParams(2, testSize, dictSize);
+                p.fParams.contentSizeFlag = 1;
+                {   size_t const initResult = ZSTD_compressBegin_advanced(ctxOrig, CNBuffer, dictSize, p, testSize-1);
+                    if (ZSTD_isError(initResult)) goto _output_error;
+            }   }
+            { size_t const copyResult = ZSTD_copyCCtx(ctxDuplicated, ctxOrig);
+              if (ZSTD_isError(copyResult)) goto _output_error;  }
+            cSize = ZSTD_compressContinue(ctxDuplicated, compressedBuffer, ZSTD_compressBound(testSize), (const char*)CNBuffer + dictSize, COMPRESSIBLE_NOISE_LENGTH - dictSize);
+            if (ZSTD_isError(cSize)) goto _output_error;
+            {   ZSTD_frameParams fp;
+                size_t const gfpResult = ZSTD_getFrameParams(&fp, compressedBuffer, cSize);
+                if (gfpResult!=0) goto _output_error;
+                if ((fp.frameContentSize != testSize) && (fp.frameContentSize != 0)) goto _output_error;
+        }   }
+        DISPLAYLEVEL(4, "OK \n");
+
+        ZSTD_freeCCtx(ctxOrig);
+        ZSTD_freeCCtx(ctxDuplicated);
+        ZSTD_freeDCtx(dctx);
     }
 
     /* Decompression defense tests */