Skip to content

Commit 83894fa

Browse files
Chandra Prataprustyrussell
authored andcommitted
fuzz-tests: Replace manual allocations with tal_arr()
Changelog-None: Use the common library utilities for temporary allocations instead of manually calling `malloc` and `free`. This makes the code conformant with rest of the codebase and reduces the chances of leaks.
1 parent f3ddc07 commit 83894fa

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

tests/fuzz/fuzz-bech32.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "config.h"
22
#include <assert.h>
3-
3+
#include <common/utils.h>
44
#include <common/bech32.h>
55
#include <stdint.h>
66
#include <string.h>
@@ -25,33 +25,29 @@ void run(const uint8_t *data, size_t size)
2525
/* Buffer size is defined in each function's doc comment. */
2626
benc = data[0] ? BECH32_ENCODING_BECH32 : BECH32_ENCODING_BECH32M;
2727
bech32_str_cap = (size - 1) + strlen(hrp_inv) + 8;
28-
bech32_str = malloc(bech32_str_cap);
28+
bech32_str = tal_arr(tmpctx, char, bech32_str_cap);
2929
if (bech32_encode(bech32_str, hrp_inv, data + 1, size - 1,
3030
bech32_str_cap, benc) == 1) {
31-
hrp_out = malloc(strlen(bech32_str) - 6);
32-
data_out = malloc(strlen(bech32_str) - 8);
31+
hrp_out = tal_arr(tmpctx, char, strlen(bech32_str) - 6);
32+
data_out = tal_arr(tmpctx, uint8_t, strlen(bech32_str) - 8);
3333

3434
benc_decoded = bech32_decode(hrp_out, data_out, &data_out_len,
3535
bech32_str, bech32_str_cap);
3636
assert(benc_decoded == benc);
3737
assert(strcmp(hrp_inv, hrp_out) == 0);
3838
assert(data_out_len == size - 1);
3939
assert(memcmp(data_out, data + 1, data_out_len) == 0);
40-
41-
free(hrp_out);
42-
free(data_out);
4340
}
44-
free(bech32_str);
4541

46-
data_out = malloc(size);
42+
data_out = tal_arr(tmpctx, uint8_t, size);
4743

4844
/* This is also used as part of sign and check message. */
4945
data_out_len = 0;
5046
bech32_convert_bits(data_out, &data_out_len, 8, data, size, 5, 1);
5147
data_out_len = 0;
5248
bech32_convert_bits(data_out, &data_out_len, 8, data, size, 5, 0);
5349

54-
addr = malloc(73 + strlen(hrp_addr));
50+
addr = tal_arr(tmpctx, char, 73 + strlen(hrp_addr));
5551
for (int wit_version = 0; wit_version < 2; ++wit_version) {
5652
if (segwit_addr_encode(addr, hrp_addr, wit_version, data,
5753
size) == 0)
@@ -63,7 +59,6 @@ void run(const uint8_t *data, size_t size)
6359
assert(data_out_len == size);
6460
assert(memcmp(data_out, data, data_out_len) == 0);
6561
}
66-
free(addr);
6762

68-
free(data_out);
63+
clean_tmpctx();
6964
}

0 commit comments

Comments
 (0)