Fix several locations with potential memory leak
This commit is contained in:
+38
-17
@@ -39,24 +39,39 @@ int main(int argc, const char** argv)
|
||||
unsigned windowLog = 18;
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
int _exit_code = 0;
|
||||
/* Create stream */
|
||||
ctx = ZSTD_createCCtx();
|
||||
if (!ctx) { return 1; }
|
||||
/* Set parameters */
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, windowLog)))
|
||||
return 2;
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, 13)))
|
||||
return 2;
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, 14)))
|
||||
return 2;
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, 1)))
|
||||
return 2;
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, 7)))
|
||||
return 2;
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, 16)))
|
||||
return 2;
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, ZSTD_fast)))
|
||||
return 2;
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, windowLog))) {
|
||||
_exit_code = 2;
|
||||
goto _clean_ctx;
|
||||
}
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, 13))) {
|
||||
_exit_code = 2;
|
||||
goto _clean_ctx;
|
||||
}
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, 14))) {
|
||||
_exit_code = 2;
|
||||
goto _clean_ctx;
|
||||
}
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, 1))) {
|
||||
_exit_code = 2;
|
||||
goto _clean_ctx;
|
||||
}
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, 7))) {
|
||||
_exit_code = 2;
|
||||
goto _clean_ctx;
|
||||
}
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, 16))) {
|
||||
_exit_code = 2;
|
||||
goto _clean_ctx;
|
||||
}
|
||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, ZSTD_fast))) {
|
||||
_exit_code = 2;
|
||||
goto _clean_ctx;
|
||||
}
|
||||
{
|
||||
U64 compressed = 0;
|
||||
const U64 toCompress = ((U64)1) << 33;
|
||||
@@ -81,22 +96,28 @@ int main(int argc, const char** argv)
|
||||
}
|
||||
printf("Compressing, trying to generate a segfault \n");
|
||||
if (compress(ctx, out, srcBuffer, size)) {
|
||||
return 1;
|
||||
_exit_code = 1;
|
||||
goto _clean_buffer;
|
||||
}
|
||||
compressed += size;
|
||||
while (compressed < toCompress) {
|
||||
const size_t block = rand() % (size - pos + 1);
|
||||
if (pos == size) { pos = 0; }
|
||||
if (compress(ctx, out, srcBuffer + pos, block)) {
|
||||
return 1;
|
||||
_exit_code = 1;
|
||||
goto _clean_buffer;
|
||||
}
|
||||
pos += block;
|
||||
compressed += block;
|
||||
}
|
||||
printf("Compression completed successfully (no error triggered)\n");
|
||||
|
||||
_clean_buffer:
|
||||
free(srcBuffer);
|
||||
free(dstBuffer);
|
||||
}
|
||||
|
||||
_clean_ctx:
|
||||
ZSTD_freeCCtx(ctx);
|
||||
return 0;
|
||||
return _exit_code;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user