Skip to content

Commit 522c916

Browse files
committed
Merge pull request #2002 from pguyot/w48/cleanup-nif_zlib_compress_1-to-reduce-gcc-confusion
Clean-up `nif_zlib_compress_1` to reduce gcc confusion These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 263bb40 + 2358569 commit 522c916

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/libAtomVM/nifs.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6344,9 +6344,6 @@ static term nif_zlib_compress_1(Context *ctx, int argc, term argv[])
63446344
{
63456345
UNUSED(argc)
63466346

6347-
term error = OK_ATOM;
6348-
6349-
Bytef *output_buf = NULL;
63506347
char *input_buf = NULL;
63516348
size_t input_size = 0;
63526349
char *alloc_ptr = NULL;
@@ -6355,8 +6352,7 @@ static term nif_zlib_compress_1(Context *ctx, int argc, term argv[])
63556352
if (LIKELY(term_is_list(input))) {
63566353
term status = iolist_to_buffer(input, &input_buf, &input_size);
63576354
if (UNLIKELY(status != OK_ATOM)) {
6358-
error = status;
6359-
goto cleanup;
6355+
RAISE_ERROR(status);
63606356
}
63616357

63626358
bool allocated = input_size > 0;
@@ -6370,39 +6366,34 @@ static term nif_zlib_compress_1(Context *ctx, int argc, term argv[])
63706366
input_buf = (char *) term_binary_data(input);
63716367
input_size = term_binary_size(input);
63726368
} else {
6373-
error = BADARG_ATOM;
6374-
goto cleanup;
6369+
RAISE_ERROR(BADARG_ATOM);
63756370
}
63766371

63776372
// to_allocate is an upper bound for compression size
63786373
// changes to actual size after calling compress
63796374
uLong to_allocate = compressBound(input_size);
63806375
// TODO: use `to_allocate` binary directly instead of allocating a buffer
63816376
// Currently not possible, there's no way to shrink backing buffer for the binary
6382-
output_buf = malloc(to_allocate);
6377+
Bytef *output_buf = malloc(to_allocate);
63836378
if (IS_NULL_PTR(output_buf)) {
6384-
error = OUT_OF_MEMORY_ATOM;
6385-
goto cleanup;
6379+
free(alloc_ptr);
6380+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
63866381
}
63876382

63886383
int z_ret = compress(output_buf, &to_allocate, (const Bytef *) input_buf, input_size);
6384+
free(alloc_ptr);
63896385
if (UNLIKELY(z_ret != Z_OK)) {
6390-
error = OUT_OF_MEMORY_ATOM;
6391-
goto cleanup;
6386+
free(output_buf);
6387+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
63926388
}
63936389

63946390
if (UNLIKELY(memory_ensure_free(ctx, term_binary_data_size_in_terms(to_allocate)) != MEMORY_GC_OK)) {
6395-
error = OUT_OF_MEMORY_ATOM;
6396-
goto cleanup;
6391+
free(output_buf);
6392+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
63976393
}
63986394
term bin_res = term_from_literal_binary(output_buf, to_allocate, &ctx->heap, ctx->global);
63996395

6400-
cleanup:
6401-
free(alloc_ptr);
64026396
free(output_buf);
6403-
if (UNLIKELY(error != OK_ATOM)) {
6404-
RAISE_ERROR(error);
6405-
}
64066397
return bin_res;
64076398
}
64086399
#endif

0 commit comments

Comments
 (0)