Coverage Report

Created: 2026-09-01 13:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/test/fuzz/float.cpp
Line
Count
Source
1
// Copyright (c) 2020-present 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 <memusage.h>
6
#include <test/fuzz/FuzzedDataProvider.h>
7
#include <test/fuzz/fuzz.h>
8
#include <test/fuzz/util.h>
9
#include <util/serfloat.h>
10
11
#include <cassert>
12
#include <cmath>
13
#include <limits>
14
#include <optional>
15
16
FUZZ_TARGET(float)
17
17
{
18
17
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19
20
17
    {
21
17
        const double d{[&] {
22
17
            std::optional<double> tmp;
23
17
            CallOneOf(
24
17
                fuzzed_data_provider,
25
                // an actual number
26
17
                [&] { tmp = fuzzed_data_provider.ConsumeFloatingPoint<double>(); },
27
                // special numbers and NANs
28
17
                [&] { tmp = fuzzed_data_provider.PickValueInArray({
29
4
                          std::numeric_limits<double>::infinity(),
30
4
                          -std::numeric_limits<double>::infinity(),
31
4
                          std::numeric_limits<double>::min(),
32
4
                          -std::numeric_limits<double>::min(),
33
4
                          std::numeric_limits<double>::max(),
34
4
                          -std::numeric_limits<double>::max(),
35
4
                          std::numeric_limits<double>::lowest(),
36
4
                          -std::numeric_limits<double>::lowest(),
37
4
                          std::numeric_limits<double>::quiet_NaN(),
38
4
                          -std::numeric_limits<double>::quiet_NaN(),
39
4
                          std::numeric_limits<double>::signaling_NaN(),
40
4
                          -std::numeric_limits<double>::signaling_NaN(),
41
4
                          std::numeric_limits<double>::denorm_min(),
42
4
                          -std::numeric_limits<double>::denorm_min(),
43
4
                      }); },
44
                // Anything from raw memory (also checks that DecodeDouble doesn't crash on any input)
45
17
                [&] { tmp = DecodeDouble(fuzzed_data_provider.ConsumeIntegral<uint64_t>()); });
46
17
            return *tmp;
47
17
        }()};
48
17
        (void)memusage::DynamicUsage(d);
49
50
17
        uint64_t encoded = EncodeDouble(d);
51
17
        if constexpr (std::numeric_limits<double>::is_iec559) {
52
17
            if (!std::isnan(d)) {
  Branch (52:17): [True: 15, False: 2]
53
15
                uint64_t encoded_in_memory;
54
15
                std::copy((const unsigned char*)&d, (const unsigned char*)(&d + 1), (unsigned char*)&encoded_in_memory);
55
15
                assert(encoded_in_memory == encoded);
  Branch (55:17): [True: 15, False: 0]
56
15
            }
57
17
        }
58
17
        double d_deserialized = DecodeDouble(encoded);
59
17
        assert(std::isnan(d) == std::isnan(d_deserialized));
  Branch (59:9): [True: 17, False: 0]
60
17
        assert(std::isnan(d) || d == d_deserialized);
  Branch (60:9): [True: 2, False: 15]
  Branch (60:9): [True: 15, False: 0]
  Branch (60:9): [True: 17, False: 0]
61
17
    }
62
17
}