From a81ffe11d439d6336bbe9aca938264773beaf43d Mon Sep 17 00:00:00 2001 From: jinyaoguo Date: Wed, 4 Jun 2025 18:08:11 -0400 Subject: [PATCH 1/3] Release resources in error paths via cleanup Replace direct returns in error-handling branches with a unified cleanup block that frees allocated resources before returning, improving code quality and robustness. --- tests/bigdict.c | 31 +++++++++++++++---------------- tests/largeDictionary.c | 32 +++++++++++++++++--------------- tests/regression/method.c | 6 +++--- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/tests/bigdict.c b/tests/bigdict.c index 748b60e79..33c4ab8ea 100644 --- a/tests/bigdict.c +++ b/tests/bigdict.c @@ -70,39 +70,38 @@ int main(int argc, const char** argv) char* buffer = (char*)malloc(bufferSize); void* out = malloc(outSize); void* roundtrip = malloc(dataSize); - int _exit_code = 0; + int _exit_code = 1; (void)argc; (void)argv; if (!buffer || !out || !roundtrip || !cctx || !dctx) { fprintf(stderr, "Allocation failure\n"); - _exit_code = 1; goto cleanup; } if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_overlapLog, 9))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_strategy, ZSTD_btopt))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetLength, 7))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_minMatch, 7))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_searchLog, 1))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 10))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_chainLog, 10))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 31))) - return 1; + goto cleanup; RDG_genBuffer(buffer, bufferSize, 1.0, 0.0, 0xbeefcafe); @@ -112,16 +111,16 @@ int main(int argc, const char** argv) for (i = 0; i < 10; ++i) { fprintf(stderr, "Compressing 1 GB\n"); if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue)) - return 1; + goto cleanup; } } fprintf(stderr, "Compressing 1 GB\n"); if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end)) - return 1; + goto cleanup; fprintf(stderr, "Success!\n"); - goto cleanup; + _exit_code = 0; cleanup: free(roundtrip); diff --git a/tests/largeDictionary.c b/tests/largeDictionary.c index ff2bb2d70..998fd9fa8 100644 --- a/tests/largeDictionary.c +++ b/tests/largeDictionary.c @@ -70,37 +70,38 @@ int main(int argc, const char** argv) char* buffer = (char*)malloc(bufferSize); void* out = malloc(outSize); void* roundtrip = malloc(dataSize); + int _exit_code = 1; (void)argc; (void)argv; if (!buffer || !out || !roundtrip || !cctx || !dctx) { fprintf(stderr, "Allocation failure\n"); - return 1; + goto cleanup; } if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_overlapLog, 9))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_strategy, ZSTD_btopt))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetLength, 7))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_minMatch, 7))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_searchLog, 1))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 10))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_chainLog, 10))) - return 1; + goto cleanup; if (ZSTD_isError(ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 31))) - return 1; + goto cleanup; RDG_genBuffer(buffer, bufferSize, 1.0, 0.0, 0xbeefcafe); @@ -110,19 +111,20 @@ int main(int argc, const char** argv) for (i = 0; i < 10; ++i) { fprintf(stderr, "Compressing 1 GB\n"); if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue)) - return 1; + goto cleanup; } } fprintf(stderr, "Compressing 1 GB\n"); if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end)) - return 1; + goto cleanup; fprintf(stderr, "Success!\n"); +cleanup: free(roundtrip); free(out); free(buffer); ZSTD_freeDCtx(dctx); ZSTD_freeCCtx(cctx); - return 0; + return _exit_code; } diff --git a/tests/regression/method.c b/tests/regression/method.c index f84a15ef3..ac6fdd712 100644 --- a/tests/regression/method.c +++ b/tests/regression/method.c @@ -159,15 +159,15 @@ static result_t compress_cctx_compress( return result_error(result_error_skip); int const level = config_get_level(config); - + result_t result; ZSTD_CCtx* cctx = ZSTD_createCCtx(); ZSTD_DCtx* dctx = ZSTD_createDCtx(); if (cctx == NULL || dctx == NULL) { fprintf(stderr, "context creation failed\n"); - return result_error(result_error_system_error); + result = result_error(result_error_system_error); + goto out; } - result_t result; result_data_t data = {.total_size = 0}; for (size_t i = 0; i < state->inputs.size; ++i) { data_buffer_t const input = state->inputs.buffers[i]; From 16e13ebdeb0cc29c90c4dd8884c366fdce7cd283 Mon Sep 17 00:00:00 2001 From: jinyaoguo Date: Sat, 21 Jun 2025 13:03:13 -0400 Subject: [PATCH 2/3] delete --- tests/bigdict.c | 132 ------------------------------------------------ 1 file changed, 132 deletions(-) delete mode 100644 tests/bigdict.c diff --git a/tests/bigdict.c b/tests/bigdict.c deleted file mode 100644 index 33c4ab8ea..000000000 --- a/tests/bigdict.c +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * All rights reserved. - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - */ - -#include -#include -#include -#include -#include -#include "datagen.h" -#include "mem.h" -#define ZSTD_STATIC_LINKING_ONLY -#include "zstd.h" - -static int -compress(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx, - void* dst, size_t dstCapacity, - void const* src, size_t srcSize, - void* roundtrip, ZSTD_EndDirective end) -{ - ZSTD_inBuffer in = {src, srcSize, 0}; - ZSTD_outBuffer out = {dst, dstCapacity, 0}; - int ended = 0; - - while (!ended && (in.pos < in.size || out.pos > 0)) { - size_t rc; - out.pos = 0; - rc = ZSTD_compressStream2(cctx, &out, &in, end); - if (ZSTD_isError(rc)) - return 1; - if (end == ZSTD_e_end && rc == 0) - ended = 1; - { - ZSTD_inBuffer rtIn = {dst, out.pos, 0}; - ZSTD_outBuffer rtOut = {roundtrip, srcSize, 0}; - rc = 1; - while (rtIn.pos < rtIn.size || rtOut.pos > 0) { - rtOut.pos = 0; - rc = ZSTD_decompressStream(dctx, &rtOut, &rtIn); - if (ZSTD_isError(rc)) { - fprintf(stderr, "Decompression error: %s\n", ZSTD_getErrorName(rc)); - return 1; - } - if (rc == 0) - break; - } - if (ended && rc != 0) { - fprintf(stderr, "Frame not finished!\n"); - return 1; - } - } - } - - return 0; -} - -int main(int argc, const char** argv) -{ - ZSTD_CCtx* cctx = ZSTD_createCCtx(); - ZSTD_DCtx* dctx = ZSTD_createDCtx(); - const size_t dataSize = (size_t)1 << 30; - const size_t outSize = ZSTD_compressBound(dataSize); - const size_t bufferSize = (size_t)1 << 31; - char* buffer = (char*)malloc(bufferSize); - void* out = malloc(outSize); - void* roundtrip = malloc(dataSize); - int _exit_code = 1; - (void)argc; - (void)argv; - - if (!buffer || !out || !roundtrip || !cctx || !dctx) { - fprintf(stderr, "Allocation failure\n"); - goto cleanup; - } - - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_overlapLog, 9))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_strategy, ZSTD_btopt))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetLength, 7))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_minMatch, 7))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_searchLog, 1))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 10))) - goto cleanup; - if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_chainLog, 10))) - goto cleanup; - - if (ZSTD_isError(ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 31))) - goto cleanup; - - RDG_genBuffer(buffer, bufferSize, 1.0, 0.0, 0xbeefcafe); - - /* Compress 30 GB */ - { - int i; - for (i = 0; i < 10; ++i) { - fprintf(stderr, "Compressing 1 GB\n"); - if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue)) - goto cleanup; - } - } - fprintf(stderr, "Compressing 1 GB\n"); - if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end)) - goto cleanup; - - fprintf(stderr, "Success!\n"); - - _exit_code = 0; - -cleanup: - free(roundtrip); - free(out); - free(buffer); - ZSTD_freeCCtx(cctx); - ZSTD_freeDCtx(dctx); - return _exit_code; -} From 878be1c8f0260db5b028694da53bdd97635d8fc7 Mon Sep 17 00:00:00 2001 From: jinyaoguo Date: Sat, 21 Jun 2025 13:43:47 -0400 Subject: [PATCH 3/3] fix --- tests/largeDictionary.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/largeDictionary.c b/tests/largeDictionary.c index 30ff98d98..b824960fc 100644 --- a/tests/largeDictionary.c +++ b/tests/largeDictionary.c @@ -73,7 +73,6 @@ int main(int argc, const char** argv) int _exit_code = 1; (void)argc; (void)argv; - int _exit_code = 0; if (!buffer || !out || !roundtrip || !cctx || !dctx) { fprintf(stderr, "Allocation failure\n"); @@ -119,6 +118,7 @@ int main(int argc, const char** argv) if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end)) goto cleanup; + _exit_code = 0; fprintf(stderr, "Success!\n"); cleanup: @@ -128,5 +128,4 @@ cleanup: ZSTD_freeDCtx(dctx); ZSTD_freeCCtx(cctx); return _exit_code; - return _exit_code; }