Skip to content

Commit 76d4018

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22180: fuzz: Increase branch coverage of the float fuzz target
fa13f34 fuzz: Increase branch coverage of the float fuzz target (MarcoFalke) fad0c58 fuzz: Remove confusing return keyword from CallOneOf (MarcoFalke) Pull request description: Currently the branch coverage for the float fuzz target is only 50% : https://marcofalke.github.io/btc_cov/fuzz.coverage/src/test/fuzz/float.cpp.gcov.html This is caused by the Fuzzed Data Provider only picking "nice" floats. ACKs for top commit: practicalswift: cr ACK fa13f34: patch looks correct Tree-SHA512: 326822515e9a1c77647d41eab9a96185a3b320914d9264730fa72ffb76c2bf3dc5bf72cf6cd9beef14f4f032358d76a976860bf3e2418ae61943cf926c0ea086
2 parents e638acf + fa13f34 commit 76d4018

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/test/fuzz/float.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <memusage.h>
66
#include <test/fuzz/FuzzedDataProvider.h>
77
#include <test/fuzz/fuzz.h>
8+
#include <test/fuzz/util.h>
89
#include <util/serfloat.h>
910
#include <version.h>
1011

@@ -17,7 +18,33 @@ FUZZ_TARGET(float)
1718
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
1819

1920
{
20-
const double d = fuzzed_data_provider.ConsumeFloatingPoint<double>();
21+
const double d{[&] {
22+
double tmp;
23+
CallOneOf(
24+
fuzzed_data_provider,
25+
// an actual number
26+
[&] { tmp = fuzzed_data_provider.ConsumeFloatingPoint<double>(); },
27+
// special numbers and NANs
28+
[&] { tmp = fuzzed_data_provider.PickValueInArray({
29+
std::numeric_limits<double>::infinity(),
30+
-std::numeric_limits<double>::infinity(),
31+
std::numeric_limits<double>::min(),
32+
-std::numeric_limits<double>::min(),
33+
std::numeric_limits<double>::max(),
34+
-std::numeric_limits<double>::max(),
35+
std::numeric_limits<double>::lowest(),
36+
-std::numeric_limits<double>::lowest(),
37+
std::numeric_limits<double>::quiet_NaN(),
38+
-std::numeric_limits<double>::quiet_NaN(),
39+
std::numeric_limits<double>::signaling_NaN(),
40+
-std::numeric_limits<double>::signaling_NaN(),
41+
std::numeric_limits<double>::denorm_min(),
42+
-std::numeric_limits<double>::denorm_min(),
43+
}); },
44+
// Anything from raw memory (also checks that DecodeDouble doesn't crash on any input)
45+
[&] { tmp = DecodeDouble(fuzzed_data_provider.ConsumeIntegral<uint64_t>()); });
46+
return tmp;
47+
}()};
2148
(void)memusage::DynamicUsage(d);
2249

2350
uint64_t encoded = EncodeDouble(d);

src/test/fuzz/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void CallOneOf(FuzzedDataProvider& fuzzed_data_provider, Callables... callables)
4444
const size_t call_index{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, call_size - 1)};
4545

4646
size_t i{0};
47-
return ((i++ == call_index ? callables() : void()), ...);
47+
((i++ == call_index ? callables() : void()), ...);
4848
}
4949

5050
template <typename Collection>

0 commit comments

Comments
 (0)