]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
change compressedBound to ULL
authorshakeelrao <shakeelrao79@gmail.com>
Fri, 1 Mar 2019 08:03:50 +0000 (00:03 -0800)
committershakeelrao <shakeelrao79@gmail.com>
Fri, 1 Mar 2019 08:03:50 +0000 (00:03 -0800)
lib/decompress/zstd_decompress.c
lib/zstd.h
tests/fuzzer.c

index 4b9402d695733b8989610d67bc04f0e71f38d765..630291f08ae44bc12a29e62c7236781d8061852c 100644 (file)
@@ -435,12 +435,13 @@ static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t he
 
 /**
  * Contains the compressed frame size and an upper-bound for the decompressed frame size.
- * Note: before using `compressedSize` or `decompressedBound`,
- *       you must check the field for errors using ZSTD_isError().
+ * Note: before using `compressedSize` you must check for errors using ZSTD_isError().
+ *       similarly, before using `decompressedBound`, you must check for errors using:
+ *          `decompressedBound` != ZSTD_CONTENTSIZE_UNKNOWN
  */
 typedef struct {
     size_t compressedSize;
-    size_t decompressedBound;
+    unsigned long long decompressedBound;
 } ZSTD_frameSizeInfo;
 
 static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize)
@@ -451,7 +452,7 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize
 #if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
     if (ZSTD_isLegacy(src, srcSize)) {
         frameSizeInfo.compressedSize = ZSTD_findFrameCompressedSizeLegacy(src, srcSize);
-        frameSizeInfo.decompressedBound = ERROR(version_unsupported);
+        frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
         return frameSizeInfo;
     }
 #endif
@@ -477,7 +478,7 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize
             }
             if (ret > 0) {
                 frameSizeInfo.compressedSize = ERROR(srcSize_wrong);
-                frameSizeInfo.decompressedBound = ERROR(srcSize_wrong);
+                frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
                 return frameSizeInfo;
             }
         }
@@ -491,13 +492,13 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize
             size_t const cBlockSize = ZSTD_getcBlockSize(ip, remainingSize, &blockProperties);
             if (ZSTD_isError(cBlockSize)) {
                 frameSizeInfo.compressedSize = cBlockSize;
-                frameSizeInfo.decompressedBound = cBlockSize;
+                frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
                 return frameSizeInfo;
             }
 
             if (ZSTD_blockHeaderSize + cBlockSize > remainingSize) {
                 frameSizeInfo.compressedSize = ERROR(srcSize_wrong);
-                frameSizeInfo.decompressedBound = ERROR(srcSize_wrong);
+                frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
                 return frameSizeInfo;
             }
 
@@ -512,7 +513,7 @@ static ZSTD_frameSizeInfo ZSTD_findFrameSizeInfo(const void* src, size_t srcSize
         if (zfh.checksumFlag) {
             if (remainingSize < 4) {
                 frameSizeInfo.compressedSize = ERROR(srcSize_wrong);
-                frameSizeInfo.decompressedBound = ERROR(srcSize_wrong);
+                frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
                 return frameSizeInfo;
             }
             ip += 4;
@@ -542,19 +543,19 @@ size_t ZSTD_findFrameCompressedSize(const void *src, size_t srcSize)
  *  currently incompatible with legacy mode
  *  `src` must point to the start of a ZSTD frame or a skippeable frame
  *  `srcSize` must be at least as large as the frame contained
- *  @return : maximum decompressed size of the compressed source
- *            or an error code which can be tested with ZSTD_isError()
+ *  @return : the maximum decompressed size of the compressed source
  */
-size_t ZSTD_decompressBound(const void* src, size_t srcSize)
+unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize)
 {
-    size_t bound = 0;
+    unsigned long long bound = 0;
     /* Iterate over each frame */
     while (srcSize > 0) {
         ZSTD_frameSizeInfo frameSizeInfo = ZSTD_findFrameSizeInfo(src, srcSize);
         size_t compressedSize = frameSizeInfo.compressedSize;
-        size_t decompressedBound = frameSizeInfo.decompressedBound;
-        FORWARD_IF_ERROR(compressedSize);
-        FORWARD_IF_ERROR(decompressedBound);
+        unsigned long long decompressedBound = frameSizeInfo.decompressedBound;
+        if (ZSTD_isError(compressedSize) || decompressedBound == ZSTD_CONTENTSIZE_ERROR) {
+            return ZSTD_CONTENTSIZE_ERROR;
+        }
         src = (const BYTE*)src + compressedSize;
         srcSize -= compressedSize;
         bound += decompressedBound;
index 6d457cfdc6ce385b6a24ecd76d8e0e705b199027..e49a4048a0645efc9fe538cb4ab40e0478f55328 100644 (file)
@@ -1107,16 +1107,16 @@ ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t
  *  currently incompatible with legacy mode
  *  `src` must point to the start of a ZSTD frame or a skippeable frame
  *  `srcSize` must be at least as large as the frame contained
- *  @return : maximum decompressed size of the compressed source
- *            or an error code which can be tested with ZSTD_isError()
+ *  @return : - the maximum decompressed size of the compressed source
+ *            - if an error occured: ZSTD_CONTENTSIZE_ERROR
  *
- *  note 1  : the bound is exact when Frame_Content_Size field is available in EVERY frame of `src`.
- *  note 2  : when Frame_Content_Size isn't provided, the upper-bound for that frame is calculated by:
- *                             upper-bound = min(128 KB, Window_Size)
- *  note 3  : we always use Frame_Content_Size to bound the decompressed frame size if it's present.
- **           the above formula is only used when Frame_Content_Size is missing.
+ *  note 1  : an error can occur if `src` points to a legacy frame or an invalid/incorrectly formatted frame.
+ *  note 2  : the bound is exact when Frame_Content_Size field is available in EVERY frame of `src`.
+ *  note 3  : when Frame_Content_Size isn't provided, the upper-bound for that frame is calculated by:
+ *              upper-bound = min(128 KB, Window_Size)
+ *  note 4  : we always use Frame_Content_Size to bound the decompressed frame size if it's present.
  */
-ZSTDLIB_API size_t ZSTD_decompressBound(const void* src, size_t srcSice);
+ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSice);
 
 /*! ZSTD_frameHeaderSize() :
  *  srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX.
index 2ac1f0f47325d5de82d8fe9d2988bdb590a856d9..9efa232a90de178b2276740cce8d4efe674a25fa 100644 (file)
@@ -378,7 +378,7 @@ static int basicUnitTests(U32 seed, double compressibility)
 
     DISPLAYLEVEL(3, "test%3i : tight ZSTD_decompressBound test : ", testNb++);
     {
-        size_t rSize = ZSTD_decompressBound(compressedBuffer, cSize);
+        unsigned long long rSize = ZSTD_decompressBound(compressedBuffer, cSize);
         if (rSize != CNBuffSize) goto _output_error;
     }
     DISPLAYLEVEL(3, "OK \n");
@@ -909,8 +909,8 @@ static int basicUnitTests(U32 seed, double compressibility)
     DISPLAYLEVEL(3, "OK \n");
 
     DISPLAYLEVEL(3, "test%3i : get tight decompressed bound of multiple frames : ", testNb++);
-    {   size_t const rSize = ZSTD_decompressBound(compressedBuffer, cSize);
-        if (rSize != CNBuffSize / 2) goto _output_error; }
+    {   unsigned long long const r = ZSTD_decompressBound(compressedBuffer, cSize);
+        if (r != CNBuffSize / 2) goto _output_error; }
     DISPLAYLEVEL(3, "OK \n");
 
     DISPLAYLEVEL(3, "test%3i : decompress multiple frames : ", testNb++);