diff --git a/tests/fuzz/fuzz-amount.c b/tests/fuzz/fuzz-amount.c index 644c9b0b7..ed2e2ad06 100644 --- a/tests/fuzz/fuzz-amount.c +++ b/tests/fuzz/fuzz-amount.c @@ -21,10 +21,7 @@ void run(const uint8_t *data, size_t size) /* We should not crash when parsing any string. */ - string = tal_arr(NULL, char, size); - for (size_t i = 0; i < size; i++) - string[i] = (char) data[i] % (CHAR_MAX + 1); - + string = to_string(NULL, data, size); parse_amount_msat(&msat, string, tal_count(string)); parse_amount_sat(&sat, string, tal_count(string)); tal_free(string); diff --git a/tests/fuzz/libfuzz.c b/tests/fuzz/libfuzz.c index 273de5523..ca9c4cfdc 100644 --- a/tests/fuzz/libfuzz.c +++ b/tests/fuzz/libfuzz.c @@ -31,3 +31,13 @@ const uint8_t **get_chunks(const void *ctx, const uint8_t *data, return chunks; } + +char *to_string(const tal_t *ctx, const u8 *data, size_t data_size) +{ + char *string = tal_arr(ctx, char, data_size); + + for (size_t i = 0; i < data_size; i++) + string[i] = (char) data[i] % (CHAR_MAX + 1); + + return string; +} diff --git a/tests/fuzz/libfuzz.h b/tests/fuzz/libfuzz.h index b359e7a44..3d28f74d3 100644 --- a/tests/fuzz/libfuzz.h +++ b/tests/fuzz/libfuzz.h @@ -1,6 +1,8 @@ #ifndef LIGHTNING_TESTS_FUZZ_LIBFUZZ_H #define LIGHTNING_TESTS_FUZZ_LIBFUZZ_H +#include +#include #include #include @@ -15,4 +17,7 @@ void run(const uint8_t *data, size_t size); const uint8_t **get_chunks(const void *ctx, const uint8_t *data, size_t data_size, size_t chunk_size); +/* Copy the data as a string. */ +char *to_string(const tal_t *ctx, const u8 *data, size_t data_size); + #endif /* LIGHTNING_TESTS_FUZZ_LIBFUZZ_H */