]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
[zstd][leak] Avoid memory leak on early return of ZSTD_generateSequence 4115/head
authorAdenilson Cavalcanti <cavalcantii@chromium.org>
Wed, 7 Aug 2024 00:16:28 +0000 (17:16 -0700)
committerAdenilson Cavalcanti <cavalcantii@chromium.org>
Wed, 7 Aug 2024 01:01:20 +0000 (18:01 -0700)
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.

lib/compress/zstd_compress.c

index 18f929bbe9d9046fde41586a12b634135a57ecae..aad25049d7907e47362fa7c42ae00a9f13830194 100644 (file)
@@ -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;