Skip to content

Commit f205cf7

Browse files
tests: Add fuzzing harness for functions/classes in span.h
1 parent 9718f38 commit f205cf7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ FUZZ_TARGETS = \
100100
test/fuzz/service_deserialize \
101101
test/fuzz/signature_checker \
102102
test/fuzz/snapshotmetadata_deserialize \
103+
test/fuzz/span \
103104
test/fuzz/spanparsing \
104105
test/fuzz/string \
105106
test/fuzz/strprintf \
@@ -872,6 +873,12 @@ test_fuzz_snapshotmetadata_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
872873
test_fuzz_snapshotmetadata_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
873874
test_fuzz_snapshotmetadata_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
874875

876+
test_fuzz_span_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
877+
test_fuzz_span_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
878+
test_fuzz_span_LDADD = $(FUZZ_SUITE_LD_COMMON)
879+
test_fuzz_span_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
880+
test_fuzz_span_SOURCES = $(FUZZ_SUITE) test/fuzz/span.cpp
881+
875882
test_fuzz_spanparsing_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
876883
test_fuzz_spanparsing_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
877884
test_fuzz_spanparsing_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/span.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <span.h>
6+
#include <test/fuzz/FuzzedDataProvider.h>
7+
#include <test/fuzz/fuzz.h>
8+
#include <test/fuzz/util.h>
9+
10+
#include <cassert>
11+
#include <cstddef>
12+
#include <cstdint>
13+
#include <string>
14+
#include <vector>
15+
16+
void test_one_input(const std::vector<uint8_t>& buffer)
17+
{
18+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19+
20+
std::string str = fuzzed_data_provider.ConsumeBytesAsString(32);
21+
const Span<const char> span = MakeSpan(str);
22+
(void)span.data();
23+
(void)span.begin();
24+
(void)span.end();
25+
if (span.size() > 0) {
26+
const std::ptrdiff_t idx = fuzzed_data_provider.ConsumeIntegralInRange<std::ptrdiff_t>(0U, span.size() - 1U);
27+
(void)span.first(idx);
28+
(void)span.last(idx);
29+
(void)span.subspan(idx);
30+
(void)span.subspan(idx, span.size() - idx);
31+
(void)span[idx];
32+
}
33+
34+
std::string another_str = fuzzed_data_provider.ConsumeBytesAsString(32);
35+
const Span<const char> another_span = MakeSpan(another_str);
36+
assert((span <= another_span) != (span > another_span));
37+
assert((span == another_span) != (span != another_span));
38+
assert((span >= another_span) != (span < another_span));
39+
}

0 commit comments

Comments
 (0)