From 79827a179f2b659928487aeb9cf802d675aa7840 Mon Sep 17 00:00:00 2001 From: shakeelrao Date: Wed, 13 Mar 2019 01:23:07 -0700 Subject: [PATCH 1/4] Fix incorrectly assigned value in ZSTD_errorFrameSizeInfo As documented in `zstd.h`, ZSTD_decompressBound returns `ZSTD_CONTENTSIZE_ERROR` if an error occurs (not `ZSTD_CONTENTSIZE_UNKNOWN`). This is consistent with the error checking made in ZSTD_decompressBound, particularly line 545. --- lib/decompress/zstd_decompress.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/decompress/zstd_decompress.c b/lib/decompress/zstd_decompress.c index 268ba386..9974dbe3 100644 --- a/lib/decompress/zstd_decompress.c +++ b/lib/decompress/zstd_decompress.c @@ -437,7 +437,7 @@ 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` you must check for errors using ZSTD_isError(). * similarly, before using `decompressedBound`, you must check for errors using: - * `decompressedBound` != ZSTD_CONTENTSIZE_UNKNOWN + * `decompressedBound` != ZSTD_CONTENTSIZE_ERROR */ typedef struct { size_t compressedSize; @@ -448,7 +448,7 @@ static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret) { ZSTD_frameSizeInfo frameSizeInfo; frameSizeInfo.compressedSize = ret; - frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_UNKNOWN; + frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; return frameSizeInfo; } From 18d3a97d434a49b40a0462ce9c14b7fde9aea3d8 Mon Sep 17 00:00:00 2001 From: shakeelrao Date: Wed, 13 Mar 2019 01:43:40 -0700 Subject: [PATCH 2/4] Add unit test to validate the error case --- tests/fuzzer.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/fuzzer.c b/tests/fuzzer.c index 37531379..b96fb4ac 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -383,6 +383,13 @@ static int basicUnitTests(U32 seed, double compressibility) } DISPLAYLEVEL(3, "OK \n"); + DISPLAYLEVEL(3, "test%3i : ZSTD_decompressBound test with invalid srcSize : ", testNb++); + { + unsigned long long bound = ZSTD_decompressBound(compressedBuffer, cSize - 1); + if (bound != ZSTD_CONTENTSIZE_ERROR) goto _output_error; + } + DISPLAYLEVEL(3, "OK \n"); + DISPLAYLEVEL(3, "test%3i : decompress %u bytes : ", testNb++, (unsigned)CNBuffSize); { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, cSize); if (r != CNBuffSize) goto _output_error; } From 91ffc8d256b4a21f29b82057868ccb1fcf9ebb12 Mon Sep 17 00:00:00 2001 From: shakeelrao Date: Fri, 15 Mar 2019 03:59:03 -0700 Subject: [PATCH 3/4] Add test to validate patch --- tests/legacy.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/legacy.c b/tests/legacy.c index b749567f..6da71227 100644 --- a/tests/legacy.c +++ b/tests/legacy.c @@ -130,12 +130,28 @@ static int testStreamingAPI(void) return error_code; } +static int testFrameDecoding(void) +{ + if (ZSTD_decompressBound(COMPRESSED, COMPRESSED_SIZE) != ZSTD_CONTENTSIZE_ERROR) { + DISPLAY("ERROR: ZSTD_decompressBound: Expected to receive ZSTD_CONTENTSIZE_ERROR\n"); + return 1; + } + if (ZSTD_findFrameCompressedSize(COMPRESSED) != COMPRESSED_SIZE) { + DISPLAY("ERROR: ZSTD_findFrameCompressedSize: Expected to receive %d\n", COMPRESSED_SIZE); + return 1; + } + DISPLAY("Frame Decoding OK\n"); + return 0; +} + int main(void) { { int const ret = testSimpleAPI(); if (ret) return ret; } { int const ret = testStreamingAPI(); if (ret) return ret; } + { int const ret = testFrameDecoding(); + if (ret) return ret; } DISPLAY("OK\n"); return 0; From 4c0540da1c77dd1710ee1c492f6c8bd562d417cd Mon Sep 17 00:00:00 2001 From: shakeelrao Date: Fri, 15 Mar 2019 05:13:55 -0700 Subject: [PATCH 4/4] Add static linking to legacy tests --- tests/legacy.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/legacy.c b/tests/legacy.c index 6da71227..e660b9cc 100644 --- a/tests/legacy.c +++ b/tests/legacy.c @@ -16,10 +16,11 @@ /*=========================================== * Dependencies *==========================================*/ -#include /* size_t */ -#include /* malloc, free */ -#include /* fprintf */ -#include /* strlen */ +#include /* size_t */ +#include /* malloc, free */ +#include /* fprintf */ +#include /* strlen */ +#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_decompressBound */ #include "zstd.h" #include "zstd_errors.h" @@ -136,10 +137,6 @@ static int testFrameDecoding(void) DISPLAY("ERROR: ZSTD_decompressBound: Expected to receive ZSTD_CONTENTSIZE_ERROR\n"); return 1; } - if (ZSTD_findFrameCompressedSize(COMPRESSED) != COMPRESSED_SIZE) { - DISPLAY("ERROR: ZSTD_findFrameCompressedSize: Expected to receive %d\n", COMPRESSED_SIZE); - return 1; - } DISPLAY("Frame Decoding OK\n"); return 0; }