]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
fixed memory leaks and almost all undefined behaviour
authorAlex Murkoff <413x1nkp@gmail.com>
Fri, 12 Apr 2024 19:24:14 +0000 (02:24 +0700)
committerAlex Murkoff <413x1nkp@gmail.com>
Fri, 12 Apr 2024 19:24:14 +0000 (02:24 +0700)
doc/educational_decoder/harness.c
lib/compress/zstd_compress.c
lib/decompress/zstd_decompress.c
programs/benchfn.c
tests/bigdict.c
tests/paramgrill.c
tests/regression/result.c
tests/seqgen.c
zlibWrapper/examples/minigzip.c
zlibWrapper/gzwrite.c

index 12c5a801bebdadfca919080a1f797112eb5784a7..eb79ceacd0f898350f50eb193548743803591e82 100644 (file)
@@ -50,6 +50,7 @@ static buffer_s read_file(const char *path)
 
     fclose(f);
     buffer_s const b = { ptr, size };
+    free(ptr);
     return b;
 }
 
index 9284e2a480aeadebbd9ca322c3eeb4e4dc844b32..d17182a46a7db3e42b1ac04e8f1c17fac06250d7 100644 (file)
@@ -2764,7 +2764,7 @@ ZSTD_buildSequencesStatistics(
     const BYTE* const ofCodeTable = seqStorePtr->ofCode;
     const BYTE* const llCodeTable = seqStorePtr->llCode;
     const BYTE* const mlCodeTable = seqStorePtr->mlCode;
-    ZSTD_symbolEncodingTypeStats_t stats;
+    ZSTD_symbolEncodingTypeStats_t stats = {};
 
     stats.lastCountSize = 0;
     /* convert length/distances into codes */
index 2f03cf7b0c77b0fcc449e226ab68d0580e6c2cc4..658cb281828ba30a04123f03e631f76b389d7645 100644 (file)
@@ -723,7 +723,7 @@ static size_t ZSTD_decodeFrameHeader(ZSTD_DCtx* dctx, const void* src, size_t he
 
 static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)
 {
-    ZSTD_frameSizeInfo frameSizeInfo;
+    ZSTD_frameSizeInfo frameSizeInfo = {};
     frameSizeInfo.compressedSize = ret;
     frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
     return frameSizeInfo;
index 3e042cf38f8246fea506890821541a0cc8483ac9..74cd21b20e34fd6ae2e7fc87ad9835438873343b 100644 (file)
@@ -91,7 +91,7 @@ static BMK_runOutcome_t BMK_runOutcome_error(size_t errorResult)
 
 static BMK_runOutcome_t BMK_setValid_runTime(BMK_runTime_t runTime)
 {
-    BMK_runOutcome_t outcome;
+    BMK_runOutcome_t outcome = {};
     outcome.error_tag_never_ever_use_directly = 0;
     outcome.internal_never_ever_use_directly = runTime;
     return outcome;
index ff2bb2d7032c6c91f264f8f98dc651df5e6575c0..748b60e79b1466e768feea6e6946b25cdbdcf603 100644 (file)
@@ -70,12 +70,14 @@ int main(int argc, const char** argv)
     char* buffer = (char*)malloc(bufferSize);
     void* out = malloc(outSize);
     void* roundtrip = malloc(dataSize);
+    int _exit_code = 0;
     (void)argc;
     (void)argv;
 
     if (!buffer || !out || !roundtrip || !cctx || !dctx) {
         fprintf(stderr, "Allocation failure\n");
-        return 1;
+        _exit_code = 1;
+        goto cleanup;
     }
 
     if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31)))
@@ -119,10 +121,13 @@ int main(int argc, const char** argv)
 
     fprintf(stderr, "Success!\n");
 
+    goto cleanup;
+
+cleanup:
     free(roundtrip);
     free(out);
     free(buffer);
-    ZSTD_freeDCtx(dctx);
     ZSTD_freeCCtx(cctx);
-    return 0;
+    ZSTD_freeDCtx(dctx);
+    return _exit_code;
 }
index 8971c65d627451d3bac53c653c0c39619517722a..869e966e05c17facd29c2da0e9746d5660f914fe 100644 (file)
@@ -1273,7 +1273,6 @@ static int createBuffers(buffers_t* buff, const char* const * const fileNamesTab
         f = fopen(fileNamesTable[n], "rb");
         if (f==NULL) {
             DISPLAY("impossible to open file %s\n", fileNamesTable[n]);
-            fclose(f);
             ret = 10;
             goto _cleanUp;
         }
index 8ccb8751e67009887b6e1da7e7de0c6e92e48a75..84a8a044dc45bf8cd199b53394a5dad9a2fc72f7 100644 (file)
@@ -24,5 +24,7 @@ char const* result_get_error_string(result_t result) {
             return "decompression error";
         case result_error_round_trip_error:
             return "round trip error";
+        default:
+            return "unknown error - " + result_get_error(result);
     }
 }
index 0d8a766c82e3bc74cd31d01416768d78898674b5..efd528baa61f79bba998488d514a86ae6a5b3443 100644 (file)
@@ -31,7 +31,7 @@ static BYTE SEQ_randByte(unsigned* src)
 
 SEQ_stream SEQ_initStream(unsigned seed)
 {
-    SEQ_stream stream;
+    SEQ_stream stream = {};
     stream.state = 0;
     XXH64_reset(&stream.xxh, 0);
     stream.seed = seed;
index 1af815207397a6293f25865c99364972b6292863..a065542125d568e75b1339186049922101597259 100644 (file)
@@ -234,7 +234,7 @@ int gzwrite _Z_OF((gzFile, const void *, unsigned));
 
 int gzwrite(gzFile gz, const void *buf, unsigned len) {
     z_stream *strm;
-    unsigned char out[BUFLEN];
+    unsigned char out[BUFLEN] = 0;
 
     if (gz == NULL || !gz->write)
         return 0;
@@ -287,7 +287,7 @@ int gzclose _Z_OF((gzFile));
 
 int gzclose(gzFile gz) {
     z_stream *strm;
-    unsigned char out[BUFLEN];
+    unsigned char out[BUFLEN] = 0;
 
     if (gz == NULL)
         return Z_STREAM_ERROR;
index 67f5eda6c3443ef8db17f79774cac56431bfe4fc..0a89c8d75caa9272018bae4d10e3c5c7bbd348f1 100644 (file)
@@ -64,6 +64,8 @@ local int gz_init(gz_statep state) {
         strm->next_out = state.state->out;
         state.state->x.next = strm->next_out;
     }
+
+    free(state.state);
     return 0;
 }