From 92c34f7f387e7a228fec6bba364fc91b6b42cfb0 Mon Sep 17 00:00:00 2001 From: Paul Dreik Date: Tue, 14 Apr 2020 07:45:30 +0200 Subject: [PATCH] do not use deprecated apis in the fuzzers (#705) * move from deprecated interface in fuzz dump raw tape * update fuzz_dump to the non deprecated replacement * replace use of deprecated api * hopefully fix windows build --- fuzz/fuzz_dump.cpp | 95 +++++++++++++++++++------------------ fuzz/fuzz_dump_raw_tape.cpp | 16 ++++--- fuzz/fuzz_parser.cpp | 10 ++-- fuzz/fuzz_print_json.cpp | 16 +++---- 4 files changed, 73 insertions(+), 64 deletions(-) diff --git a/fuzz/fuzz_dump.cpp b/fuzz/fuzz_dump.cpp index 8def05c5..b6f30992 100644 --- a/fuzz/fuzz_dump.cpp +++ b/fuzz/fuzz_dump.cpp @@ -3,58 +3,61 @@ #include #include #include +#include #include "NullBuffer.h" -// from the README on the front page -void compute_dump(simdjson::ParsedJson::Iterator &pjh) { - NulOStream os; - - if (pjh.is_object()) { - os << "{"; - if (pjh.down()) { - pjh.print(os); // must be a string - os << ":"; - pjh.next(); - compute_dump(pjh); // let us recurse - while (pjh.next()) { - os << ","; - pjh.print(os); - os << ":"; - pjh.next(); - compute_dump(pjh); // let us recurse - } - pjh.up(); +// example from doc/basics.md#tree-walking-and-json-element-types +static void print_json(std::ostream& os, simdjson::dom::element element) { + const char endl='\n'; + switch (element.type()) { + case simdjson::dom::element_type::ARRAY: + os << "["; + for (simdjson::dom::element child : simdjson::dom::array(element)) { + print_json(os, child); + os << ","; + } + os << "]"; + break; + case simdjson::dom::element_type::OBJECT: + os << "{"; + for (simdjson::dom::key_value_pair field : simdjson::dom::object(element)) { + os << "\"" << field.key << "\": "; + print_json(os, field.value); + } + os << "}"; + break; + case simdjson::dom::element_type::INT64: + os << int64_t(element) << endl; + break; + case simdjson::dom::element_type::UINT64: + os << uint64_t(element) << endl; + break; + case simdjson::dom::element_type::DOUBLE: + os << double(element) << endl; + break; + case simdjson::dom::element_type::STRING: + os << std::string_view(element) << endl; + break; + case simdjson::dom::element_type::BOOL: + os << bool(element) << endl; + break; + case simdjson::dom::element_type::NULL_VALUE: + os << "null" << endl; + break; } - os << "}"; - } else if (pjh.is_array()) { - os << "["; - if (pjh.down()) { - compute_dump(pjh); // let us recurse - while (pjh.next()) { - os << ","; - compute_dump(pjh); // let us recurse - } - pjh.up(); - } - os << "]"; - } else { - pjh.print(os); // just print the lone value - } } - extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { - try { - auto pj = simdjson::build_parsed_json(Data, Size); - if (!pj.is_valid()) { - throw 1; + try { + simdjson::dom::parser pj; + auto elem=pj.parse(Data, Size); + auto v=elem.value(); + NulOStream os; + //std::ostream& os(std::cout); + print_json(os,v); + } catch (...) { } - simdjson::ParsedJson::Iterator pjh(pj); - if (pjh.is_ok()) { - compute_dump(pjh); - } - } catch (...) { - } - return 0; + return 0; + } diff --git a/fuzz/fuzz_dump_raw_tape.cpp b/fuzz/fuzz_dump_raw_tape.cpp index 5c146b8c..5b150a26 100644 --- a/fuzz/fuzz_dump_raw_tape.cpp +++ b/fuzz/fuzz_dump_raw_tape.cpp @@ -8,11 +8,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { - try { - auto pj = simdjson::build_parsed_json(Data, Size); - NulOStream os; - UNUSED bool ignored=pj.dump_raw_tape(os); - } catch (...) { - } - return 0; + try { + simdjson::dom::parser pj; + auto elem=pj.parse(Data, Size); + auto v=elem.value(); + NulOStream os; + UNUSED auto dumpstatus=v.dump_raw_tape(os); + } catch (...) { + } + return 0; } diff --git a/fuzz/fuzz_parser.cpp b/fuzz/fuzz_parser.cpp index dd3a5912..e45e08b2 100644 --- a/fuzz/fuzz_parser.cpp +++ b/fuzz/fuzz_parser.cpp @@ -4,7 +4,11 @@ #include extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { - auto ignored = simdjson::build_parsed_json(Data, Size); - - return 0; + try { + simdjson::dom::parser pj; + auto result=pj.parse(Data, Size); + UNUSED auto v=result.value(); + }catch(...) { + } + return 0; } diff --git a/fuzz/fuzz_print_json.cpp b/fuzz/fuzz_print_json.cpp index 57a2a220..6925dd8a 100644 --- a/fuzz/fuzz_print_json.cpp +++ b/fuzz/fuzz_print_json.cpp @@ -8,12 +8,12 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { - try { - auto pj = simdjson::build_parsed_json(Data, Size); - NulOStream os; - bool ignored=pj.print_json(os); - (void)ignored; - } catch (...) { - } - return 0; + try { + simdjson::dom::parser pj; + auto elem=pj.parse(Data, Size); + NulOStream os; + os<