1
0
mirror of https://github.com/facebook/zstd.git synced 2025-07-29 11:21:22 +03:00

fixed memory leaks and almost all undefined behaviour

This commit is contained in:
Alex Murkoff
2024-04-13 02:24:14 +07:00
parent 592de19843
commit 1d5e9705db
10 changed files with 19 additions and 10 deletions

View File

@ -50,6 +50,7 @@ static buffer_s read_file(const char *path)
fclose(f); fclose(f);
buffer_s const b = { ptr, size }; buffer_s const b = { ptr, size };
free(ptr);
return b; return b;
} }

View File

@ -2764,7 +2764,7 @@ ZSTD_buildSequencesStatistics(
const BYTE* const ofCodeTable = seqStorePtr->ofCode; const BYTE* const ofCodeTable = seqStorePtr->ofCode;
const BYTE* const llCodeTable = seqStorePtr->llCode; const BYTE* const llCodeTable = seqStorePtr->llCode;
const BYTE* const mlCodeTable = seqStorePtr->mlCode; const BYTE* const mlCodeTable = seqStorePtr->mlCode;
ZSTD_symbolEncodingTypeStats_t stats; ZSTD_symbolEncodingTypeStats_t stats = {};
stats.lastCountSize = 0; stats.lastCountSize = 0;
/* convert length/distances into codes */ /* convert length/distances into codes */

View 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) static ZSTD_frameSizeInfo ZSTD_errorFrameSizeInfo(size_t ret)
{ {
ZSTD_frameSizeInfo frameSizeInfo; ZSTD_frameSizeInfo frameSizeInfo = {};
frameSizeInfo.compressedSize = ret; frameSizeInfo.compressedSize = ret;
frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR; frameSizeInfo.decompressedBound = ZSTD_CONTENTSIZE_ERROR;
return frameSizeInfo; return frameSizeInfo;

View 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) 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.error_tag_never_ever_use_directly = 0;
outcome.internal_never_ever_use_directly = runTime; outcome.internal_never_ever_use_directly = runTime;
return outcome; return outcome;

View File

@ -70,12 +70,14 @@ int main(int argc, const char** argv)
char* buffer = (char*)malloc(bufferSize); char* buffer = (char*)malloc(bufferSize);
void* out = malloc(outSize); void* out = malloc(outSize);
void* roundtrip = malloc(dataSize); void* roundtrip = malloc(dataSize);
int _exit_code = 0;
(void)argc; (void)argc;
(void)argv; (void)argv;
if (!buffer || !out || !roundtrip || !cctx || !dctx) { if (!buffer || !out || !roundtrip || !cctx || !dctx) {
fprintf(stderr, "Allocation failure\n"); fprintf(stderr, "Allocation failure\n");
return 1; _exit_code = 1;
goto cleanup;
} }
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31))) 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"); fprintf(stderr, "Success!\n");
goto cleanup;
cleanup:
free(roundtrip); free(roundtrip);
free(out); free(out);
free(buffer); free(buffer);
ZSTD_freeDCtx(dctx);
ZSTD_freeCCtx(cctx); ZSTD_freeCCtx(cctx);
return 0; ZSTD_freeDCtx(dctx);
return _exit_code;
} }

View File

@ -1273,7 +1273,6 @@ static int createBuffers(buffers_t* buff, const char* const * const fileNamesTab
f = fopen(fileNamesTable[n], "rb"); f = fopen(fileNamesTable[n], "rb");
if (f==NULL) { if (f==NULL) {
DISPLAY("impossible to open file %s\n", fileNamesTable[n]); DISPLAY("impossible to open file %s\n", fileNamesTable[n]);
fclose(f);
ret = 10; ret = 10;
goto _cleanUp; goto _cleanUp;
} }

View File

@ -24,5 +24,7 @@ char const* result_get_error_string(result_t result) {
return "decompression error"; return "decompression error";
case result_error_round_trip_error: case result_error_round_trip_error:
return "round trip error"; return "round trip error";
default:
return "unknown error - " + result_get_error(result);
} }
} }

View File

@ -31,7 +31,7 @@ static BYTE SEQ_randByte(unsigned* src)
SEQ_stream SEQ_initStream(unsigned seed) SEQ_stream SEQ_initStream(unsigned seed)
{ {
SEQ_stream stream; SEQ_stream stream = {};
stream.state = 0; stream.state = 0;
XXH64_reset(&stream.xxh, 0); XXH64_reset(&stream.xxh, 0);
stream.seed = seed; stream.seed = seed;

View File

@ -234,7 +234,7 @@ int gzwrite _Z_OF((gzFile, const void *, unsigned));
int gzwrite(gzFile gz, const void *buf, unsigned len) { int gzwrite(gzFile gz, const void *buf, unsigned len) {
z_stream *strm; z_stream *strm;
unsigned char out[BUFLEN]; unsigned char out[BUFLEN] = 0;
if (gz == NULL || !gz->write) if (gz == NULL || !gz->write)
return 0; return 0;
@ -287,7 +287,7 @@ int gzclose _Z_OF((gzFile));
int gzclose(gzFile gz) { int gzclose(gzFile gz) {
z_stream *strm; z_stream *strm;
unsigned char out[BUFLEN]; unsigned char out[BUFLEN] = 0;
if (gz == NULL) if (gz == NULL)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;

View File

@ -64,6 +64,8 @@ local int gz_init(gz_statep state) {
strm->next_out = state.state->out; strm->next_out = state.state->out;
state.state->x.next = strm->next_out; state.state->x.next = strm->next_out;
} }
free(state.state);
return 0; return 0;
} }