Browse Source

fuzz: add a 'to_string' function to libfuzz

We want to use it outside of fuzz-amount

Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
ppa-prep
Antoine Poinsot 4 years ago
committed by neil saitug
parent
commit
bf0a2f2b85
  1. 5
      tests/fuzz/fuzz-amount.c
  2. 10
      tests/fuzz/libfuzz.c
  3. 5
      tests/fuzz/libfuzz.h

5
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);

10
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;
}

5
tests/fuzz/libfuzz.h

@ -1,6 +1,8 @@
#ifndef LIGHTNING_TESTS_FUZZ_LIBFUZZ_H
#define LIGHTNING_TESTS_FUZZ_LIBFUZZ_H
#include <ccan/ccan/short_types/short_types.h>
#include <ccan/ccan/tal/tal.h>
#include <stddef.h>
#include <stdint.h>
@ -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 */

Loading…
Cancel
Save