Skip to content

Commit 528132e

Browse files
authored
Merge pull request #4402 from mugitya03/tests
Release resources in error paths via cleanup
2 parents 7eefc22 + 878be1c commit 528132e

2 files changed

Lines changed: 33 additions & 59 deletions

File tree

tests/largeDictionary.c

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -70,61 +70,38 @@ int main(int argc, const char** argv)
7070
char* buffer = (char*)malloc(bufferSize);
7171
void* out = malloc(outSize);
7272
void* roundtrip = malloc(dataSize);
73+
int _exit_code = 1;
7374
(void)argc;
7475
(void)argv;
75-
int _exit_code = 0;
7676

7777
if (!buffer || !out || !roundtrip || !cctx || !dctx) {
7878
fprintf(stderr, "Allocation failure\n");
79-
_exit_code = 1;
80-
goto _cleanup;
79+
goto cleanup;
8180
}
8281

83-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31))) {
84-
_exit_code = 1;
85-
goto _cleanup;
86-
}
87-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1))) {
88-
_exit_code = 1;
89-
goto _cleanup;
90-
}
91-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_overlapLog, 9))) {
92-
_exit_code = 1;
93-
goto _cleanup;
94-
}
95-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1))) {
96-
_exit_code = 1;
97-
goto _cleanup;
98-
}
99-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_strategy, ZSTD_btopt))) {
100-
_exit_code = 1;
101-
goto _cleanup;
102-
}
103-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetLength, 7))) {
104-
_exit_code = 1;
105-
goto _cleanup;
106-
}
107-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_minMatch, 7))) {
108-
_exit_code = 1;
109-
goto _cleanup;
110-
}
111-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_searchLog, 1))) {
112-
_exit_code = 1;
113-
goto _cleanup;
114-
}
115-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 10))) {
116-
_exit_code = 1;
117-
goto _cleanup;
118-
}
119-
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_chainLog, 10))) {
120-
_exit_code = 1;
121-
goto _cleanup;
122-
}
82+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, 31)))
83+
goto cleanup;
84+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 1)))
85+
goto cleanup;
86+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_overlapLog, 9)))
87+
goto cleanup;
88+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1)))
89+
goto cleanup;
90+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_strategy, ZSTD_btopt)))
91+
goto cleanup;
92+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetLength, 7)))
93+
goto cleanup;
94+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_minMatch, 7)))
95+
goto cleanup;
96+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_searchLog, 1)))
97+
goto cleanup;
98+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_hashLog, 10)))
99+
goto cleanup;
100+
if (ZSTD_isError(ZSTD_CCtx_setParameter(cctx, ZSTD_c_chainLog, 10)))
101+
goto cleanup;
123102

124-
if (ZSTD_isError(ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 31))) {
125-
_exit_code = 1;
126-
goto _cleanup;
127-
}
103+
if (ZSTD_isError(ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, 31)))
104+
goto cleanup;
128105

129106
RDG_genBuffer(buffer, bufferSize, 1.0, 0.0, 0xbeefcafe);
130107

@@ -133,21 +110,18 @@ int main(int argc, const char** argv)
133110
int i;
134111
for (i = 0; i < 10; ++i) {
135112
fprintf(stderr, "Compressing 1 GB\n");
136-
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue)) {
137-
_exit_code = 1;
138-
goto _cleanup;
139-
}
113+
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_continue))
114+
goto cleanup;
140115
}
141116
}
142117
fprintf(stderr, "Compressing 1 GB\n");
143-
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end)) {
144-
_exit_code = 1;
145-
goto _cleanup;
146-
}
118+
if (compress(cctx, dctx, out, outSize, buffer, dataSize, roundtrip, ZSTD_e_end))
119+
goto cleanup;
147120

121+
_exit_code = 0;
148122
fprintf(stderr, "Success!\n");
149123

150-
_cleanup:
124+
cleanup:
151125
free(roundtrip);
152126
free(out);
153127
free(buffer);

tests/regression/method.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ static result_t compress_cctx_compress(
159159
return result_error(result_error_skip);
160160

161161
int const level = config_get_level(config);
162-
162+
result_t result;
163163
ZSTD_CCtx* cctx = ZSTD_createCCtx();
164164
ZSTD_DCtx* dctx = ZSTD_createDCtx();
165165
if (cctx == NULL || dctx == NULL) {
166166
fprintf(stderr, "context creation failed\n");
167-
return result_error(result_error_system_error);
167+
result = result_error(result_error_system_error);
168+
goto out;
168169
}
169170

170-
result_t result;
171171
result_data_t data = {.total_size = 0};
172172
for (size_t i = 0; i < state->inputs.size; ++i) {
173173
data_buffer_t const input = state->inputs.buffers[i];

0 commit comments

Comments
 (0)