]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
[libzstd] Fix parameter selection for empty input 903/head
authorNick Terrell <terrelln@fb.com>
Thu, 26 Oct 2017 00:10:57 +0000 (17:10 -0700)
committerNick Terrell <terrelln@fb.com>
Thu, 26 Oct 2017 00:24:15 +0000 (17:24 -0700)
ZSTD_compress() and friends would treat an empty input as an unknown size
when selecting parameters. Thus, they would drastically overallocate the
context. Tell ZSTD_getParams() that the source size is 1 when it is empty.

lib/compress/zstd_compress.c
tests/fuzzer.c

index 635cc665c85aaf38854b3ea6434d77a2a91587f3..21b84d1046dea2fc1dcfb6817bb49214596649f8 100644 (file)
@@ -2213,7 +2213,7 @@ size_t ZSTD_compress_advanced_internal(
 size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize,
                                const void* dict, size_t dictSize, int compressionLevel)
 {
-    ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, dict ? dictSize : 0);
+    ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize ? srcSize : 1, dict ? dictSize : 0);
     params.fParams.contentSizeFlag = 1;
     return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params);
 }
index 76df77af9015d006634946c3e45e0a3bb88a309b..e40f19978265e795c73a9e098d1b57eff7cc52c0 100644 (file)
@@ -359,6 +359,14 @@ static int basicUnitTests(U32 seed, double compressibility)
       if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
     DISPLAYLEVEL(4, "OK \n");
 
+    DISPLAYLEVEL(4, "test%di : check CCtx size after compressing empty input : ", testNb++);
+    {   ZSTD_CCtx* cctx = ZSTD_createCCtx();
+        size_t const r = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, NULL, 0, 19);
+        if (ZSTD_isError(r)) goto _output_error;
+        if (ZSTD_sizeof_CCtx(cctx) > (1U << 20)) goto _output_error;
+        ZSTD_freeCCtx(cctx);
+    }
+    DISPLAYLEVEL(4, "OK \n");
 
     /* Static CCtx tests */
 #define STATIC_CCTX_LEVEL 3