Skip to content

Commit 2bdc47b

Browse files
committed
add fuzzer for jsonptr from the examples page
1 parent 6318098 commit 2bdc47b

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎fuzz/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(SOURCES
3232
fuzz_print_json.cpp
3333
fuzz_dump_raw_tape.cpp
3434
fuzz_ndjson.cpp
35+
fuzz_jsonptr.cpp
3536
)
3637

3738
add_custom_target(print_all_fuzz_targets

‎fuzz/fuzz_jsonptr.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "simdjson.h"
2+
#include <cstddef>
3+
#include <cstdint>
4+
#include <string>
5+
6+
#include "NullBuffer.h"
7+
8+
// why is this not in <algorithm>?
9+
template<class Iterator, class Element>
10+
Iterator find_last(Iterator first, Iterator last, const Element& e) {
11+
Iterator ret=last;
12+
while(first!=ret) {
13+
--ret;
14+
if(*ret==e) {
15+
return ret;
16+
}
17+
}
18+
return last;
19+
}
20+
21+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
22+
23+
// split the input in two, the first part will be used as json and
24+
// the second part as index to use for the json pointer
25+
26+
const uint8_t sep='\t';
27+
const uint8_t* end=Data+Size;
28+
const uint8_t* mid = find_last(Data,end,sep);
29+
const size_t Size1=std::distance(Data,mid);
30+
if(Size1<Size) {
31+
++mid;
32+
}
33+
const size_t Size2=std::distance(mid,end);
34+
35+
// inspired by doc/basics.md#json-pointer
36+
try {
37+
simdjson::dom::parser pj;
38+
simdjson::dom::element root=pj.parse(Data, Size1);
39+
40+
auto key=std::string_view((const char*)mid,Size2);
41+
NulOStream os;
42+
//std::ostream& os(std::cout);
43+
os<< root[key]<<'\n';
44+
} catch (...) {
45+
}
46+
return 0;
47+
}

0 commit comments

Comments
 (0)