Skip to content

Commit 2454cff

Browse files
pauldreikhorenmar
authored andcommitted
add fuzzer for columns
1 parent 0098a76 commit 2454cff

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

fuzzing/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
add_library(fuzzhelper NullOStream.h NullOStream.cpp)
88
target_link_libraries(fuzzhelper PUBLIC Catch2::Catch2)
99

10+
# use C++17 so we can get string_view
11+
target_compile_features(fuzzhelper PUBLIC cxx_std_17)
12+
1013
# This should be possible to set from the outside to be oss-fuzz compatible,
1114
# fix later. For now, target libFuzzer only.
1215
target_link_options(fuzzhelper PUBLIC "-fsanitize=fuzzer")
1316

14-
foreach(fuzzer TestSpecParser XmlWriter)
17+
foreach(fuzzer TestSpecParser XmlWriter textflow)
1518
add_executable(fuzz_${fuzzer} fuzz_${fuzzer}.cpp)
1619
target_link_libraries(fuzz_${fuzzer} PRIVATE fuzzhelper)
1720
endforeach()

fuzzing/NullOStream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

3-
#include <iostream>
3+
#include <ostream>
4+
#include <streambuf>
45

56
// from https://stackoverflow.com/a/8244052
67
class NullStreambuf : public std::streambuf {
@@ -17,4 +18,3 @@ class NullOStream final : private NullStreambuf, public std::ostream {
1718
virtual void avoidOutOfLineVirtualCompilerWarning();
1819
};
1920

20-

fuzzing/fuzz_textflow.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//License: Boost 1.0
2+
//By Paul Dreik 2020
3+
4+
#include <catch2/internal/catch_textflow.hpp>
5+
6+
#include "NullOStream.h"
7+
8+
#include <string>
9+
#include <string_view>
10+
11+
12+
template<class Callback>
13+
void split(const char *Data, size_t Size, Callback callback) {
14+
15+
using namespace std::literals;
16+
constexpr auto sep="\n~~~\n"sv;
17+
18+
std::string_view remainder(Data,Size);
19+
for (;;) {
20+
auto pos=remainder.find(sep);
21+
if(pos==std::string_view::npos) {
22+
//not found. use the remainder and exit
23+
callback(remainder);
24+
return;
25+
} else {
26+
//found. invoke callback on the first part, then proceed with the rest.
27+
callback(remainder.substr(0,pos));
28+
remainder=remainder.substr(pos+sep.size());
29+
}
30+
}
31+
}
32+
33+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
34+
35+
Catch::TextFlow::Columns columns;
36+
37+
// break the input on separator
38+
split((const char*)Data,Size,[&](std::string_view word) {
39+
columns+=Catch::TextFlow::Column(std::string(word));
40+
});
41+
42+
NullOStream nul;
43+
nul << columns;
44+
45+
return 0;
46+
}
47+

0 commit comments

Comments
 (0)