mirror of
https://github.com/facebook/zstd.git
synced 2025-08-05 19:15:58 +03:00
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.
This commit is contained in:
@@ -70,39 +70,38 @@ 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;
|
int _exit_code = 1;
|
||||||
(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");
|
||||||
_exit_code = 1;
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
if (ZSTD_isError(ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 31)))
|
||||||
return 1;
|
goto cleanup;
|
||||||
|
|
||||||
RDG_genBuffer(buffer, bufferSize, 1.0, 0.0, 0xbeefcafe);
|
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) {
|
for (i = 0; i < 10; ++i) {
|
||||||
fprintf(stderr, "Compressing 1 GB\n");
|
fprintf(stderr, "Compressing 1 GB\n");
|
||||||
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue))
|
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue))
|
||||||
return 1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fprintf(stderr, "Compressing 1 GB\n");
|
fprintf(stderr, "Compressing 1 GB\n");
|
||||||
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end))
|
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end))
|
||||||
return 1;
|
goto cleanup;
|
||||||
|
|
||||||
fprintf(stderr, "Success!\n");
|
fprintf(stderr, "Success!\n");
|
||||||
|
|
||||||
goto cleanup;
|
_exit_code = 0;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
free(roundtrip);
|
free(roundtrip);
|
||||||
|
@@ -70,37 +70,38 @@ 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 = 1;
|
||||||
(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;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
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)))
|
if (ZSTD_isError(ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 31)))
|
||||||
return 1;
|
goto cleanup;
|
||||||
|
|
||||||
RDG_genBuffer(buffer, bufferSize, 1.0, 0.0, 0xbeefcafe);
|
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) {
|
for (i = 0; i < 10; ++i) {
|
||||||
fprintf(stderr, "Compressing 1 GB\n");
|
fprintf(stderr, "Compressing 1 GB\n");
|
||||||
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue))
|
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue))
|
||||||
return 1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fprintf(stderr, "Compressing 1 GB\n");
|
fprintf(stderr, "Compressing 1 GB\n");
|
||||||
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end))
|
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end))
|
||||||
return 1;
|
goto cleanup;
|
||||||
|
|
||||||
fprintf(stderr, "Success!\n");
|
fprintf(stderr, "Success!\n");
|
||||||
|
|
||||||
|
cleanup:
|
||||||
free(roundtrip);
|
free(roundtrip);
|
||||||
free(out);
|
free(out);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
ZSTD_freeDCtx(dctx);
|
ZSTD_freeDCtx(dctx);
|
||||||
ZSTD_freeCCtx(cctx);
|
ZSTD_freeCCtx(cctx);
|
||||||
return 0;
|
return _exit_code;
|
||||||
}
|
}
|
||||||
|
@@ -159,15 +159,15 @@ static result_t compress_cctx_compress(
|
|||||||
return result_error(result_error_skip);
|
return result_error(result_error_skip);
|
||||||
|
|
||||||
int const level = config_get_level(config);
|
int const level = config_get_level(config);
|
||||||
|
result_t result;
|
||||||
ZSTD_CCtx* cctx = ZSTD_createCCtx();
|
ZSTD_CCtx* cctx = ZSTD_createCCtx();
|
||||||
ZSTD_DCtx* dctx = ZSTD_createDCtx();
|
ZSTD_DCtx* dctx = ZSTD_createDCtx();
|
||||||
if (cctx == NULL || dctx == NULL) {
|
if (cctx == NULL || dctx == NULL) {
|
||||||
fprintf(stderr, "context creation failed\n");
|
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};
|
result_data_t data = {.total_size = 0};
|
||||||
for (size_t i = 0; i < state->inputs.size; ++i) {
|
for (size_t i = 0; i < state->inputs.size; ++i) {
|
||||||
data_buffer_t const input = state->inputs.buffers[i];
|
data_buffer_t const input = state->inputs.buffers[i];
|
||||||
|
Reference in New Issue
Block a user