Many APIs expect NUL terminated strings, for example the JSON Web Token decoding functions.
Add a helper to make it easy to fuzz such functions. As we are allocating anew anyway, we pass along the buffer mutably as well as the original size, so the harness is useful for a wider range of API. Signed-off-by: Ahmad Fatoum <a.fat...@pengutronix.de> --- include/fuzz.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/fuzz.h b/include/fuzz.h index bd95ae6203b0..caebc284d5db 100644 --- a/include/fuzz.h +++ b/include/fuzz.h @@ -9,6 +9,8 @@ #include <linux/types.h> #include <linux/compiler_types.h> +#include <linux/bug.h> +#include <linux/string.h> #include <ramdisk.h> /** @@ -65,6 +67,18 @@ extern const struct fuzz_test __barebox_fuzz_tests_end; } \ fuzz_test(_name, _func##_ramdisk) +#define fuzz_test_str(_name, _func) \ + static int _func##_str(const u8 *_data, size_t size) \ + { \ + int ret; \ + char *data = memdup_nul(_data, size); \ + BUG_ON(!data); \ + ret = _func(data, size); \ + free(data); \ + return ret; \ + } \ + fuzz_test(_name, _func##_str) + static inline int fuzz_test_once(const struct fuzz_test *test, const u8 *data, size_t len) { return test->func(data, len); -- 2.39.5