From: Stephan Bosch Date: Fri, 16 Oct 2020 11:35:18 +0000 (+0200) Subject: lib-json: Add fuzz target for parsing JSON tree from istream X-Git-Tag: 2.4.0~2359 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b0e4ff99db47cf663303018e0439f7c02dfa1f69;p=thirdparty%2Fdovecot%2Fcore.git lib-json: Add fuzz target for parsing JSON tree from istream --- diff --git a/src/lib-json/Makefile.am b/src/lib-json/Makefile.am index 2dac5aa022..661a88e74f 100644 --- a/src/lib-json/Makefile.am +++ b/src/lib-json/Makefile.am @@ -36,7 +36,14 @@ test_programs = \ test-json-tree \ test-json-tree-io -noinst_PROGRAMS = json-format $(test_programs) +fuzz_programs = + +if USE_FUZZER +fuzz_programs += \ + fuzz-json-istream +endif + +noinst_PROGRAMS = json-format $(test_programs) $(fuzz_programs) json_format_SOURCE = \ json-format.c @@ -110,6 +117,18 @@ test_json_tree_io_DEPENDENCIES = \ pkginc_libdir=$(pkgincludedir) pkginc_lib_HEADERS = $(headers) +nodist_EXTRA_fuzz_json_istream_SOURCES = force-cxx-linking.cxx +fuzz_json_istream_CPPFLAGS = \ + $(FUZZER_CPPFLAGS) +fuzz_json_istream_LDFLAGS = \ + $(FUZZER_LDFLAGS) +fuzz_json_istream_SOURCES = \ + fuzz-json-istream.c +fuzz_json_istream_LDADD = \ + $(test_libs) +fuzz_json_istream_DEPENDENCIES = \ + $(test_deps) + check: check-am check-test check-test: all-am for bin in $(test_programs); do \ diff --git a/src/lib-json/fuzz-json-istream.c b/src/lib-json/fuzz-json-istream.c new file mode 100644 index 0000000000..a9ec202350 --- /dev/null +++ b/src/lib-json/fuzz-json-istream.c @@ -0,0 +1,36 @@ +/* Copyright (c) 2023 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "istream.h" +#include "test-common.h" +#include "test-common.h" +#include "fuzzer.h" + +#include "json-types.h" +#include "json-parser.h" +#include "json-istream.h" + +FUZZ_BEGIN_DATA(const unsigned char *data, size_t size) +{ + struct istream *input; + struct json_limits limits = { + .max_name_size = 1024U, + .max_string_size = 1024U, + .max_nesting = 10U, + .max_list_items = JSON_DEFAULT_MAX_LIST_ITEMS, + }; + struct json_tree *tree = NULL; + struct json_istream *jinput; + int ret; + + input = test_istream_create_data(data, size); + jinput = json_istream_create(input, JSON_ISTREAM_TYPE_NORMAL, &limits, + JSON_PARSER_FLAG_STRICT); + ret = json_istream_read_tree(jinput, &tree); + i_assert(ret < 1 || tree != NULL); + if (tree != NULL) + json_tree_unref(&tree); + json_istream_unref(&jinput); + i_stream_unref(&input); +} +FUZZ_END