From: Adenilson Cavalcanti Date: Wed, 7 Aug 2024 00:16:28 +0000 (-0700) Subject: [zstd][leak] Avoid memory leak on early return of ZSTD_generateSequence X-Git-Tag: v1.5.7^2~94^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F4115%2Fhead;p=thirdparty%2Fzstd.git [zstd][leak] Avoid memory leak on early return of ZSTD_generateSequence Sanity checks on a few of the context parameters (i.e. workers and block size) may prompt an early return on ZSTD_generateSequences. Allocating the destination buffer past those return points avoids a potential memory leak. This patch should fix issue #4112. --- diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 18f929bbe..aad25049d 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -3458,7 +3458,7 @@ size_t ZSTD_generateSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs, size_t outSeqsSize, const void* src, size_t srcSize) { const size_t dstCapacity = ZSTD_compressBound(srcSize); - void* dst = ZSTD_customMalloc(dstCapacity, ZSTD_defaultCMem); + void* dst; /* Make C90 happy. */ SeqCollector seqCollector; { int targetCBlockSize; @@ -3471,6 +3471,7 @@ size_t ZSTD_generateSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs, RETURN_ERROR_IF(nbWorkers != 0, parameter_unsupported, "nbWorkers != 0"); } + dst = ZSTD_customMalloc(dstCapacity, ZSTD_defaultCMem); RETURN_ERROR_IF(dst == NULL, memory_allocation, "NULL pointer!"); seqCollector.collectSequences = 1;