Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
scriptnum_ops.cpp
Go to the documentation of this file.
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 <script/script.h>
7#include <test/fuzz/fuzz.h>
8#include <test/fuzz/util.h>
9
10#include <cassert>
11#include <cstdint>
12#include <limits>
13#include <vector>
14
15namespace {
16bool IsValidAddition(const CScriptNum& lhs, const CScriptNum& rhs)
17{
18 return rhs == 0 || (rhs > 0 && lhs <= CScriptNum{std::numeric_limits<int64_t>::max()} - rhs) || (rhs < 0 && lhs >= CScriptNum{std::numeric_limits<int64_t>::min()} - rhs);
19}
20
22{
23 return rhs == 0 || (rhs > 0 && lhs >= CScriptNum{std::numeric_limits<int64_t>::min()} + rhs) || (rhs < 0 && lhs <= CScriptNum{std::numeric_limits<int64_t>::max()} + rhs);
24}
25} // namespace
26
28{
29 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
34 [&] {
36 assert((script_num == i) != (script_num != i));
37 assert((script_num <= i) != (script_num > i));
38 assert((script_num >= i) != (script_num < i));
39 // Avoid signed integer overflow:
40 // script/script.h:264:93: runtime error: signed integer overflow: -2261405121394637306 + -9223372036854775802 cannot be represented in type 'long'
42 assert((script_num + i) - i == script_num);
43 }
44 // Avoid signed integer overflow:
45 // script/script.h:265:93: runtime error: signed integer overflow: 9223371895120855039 - -9223372036854710486 cannot be represented in type 'long'
47 assert((script_num - i) + i == script_num);
48 }
49 },
50 [&] {
55 // Avoid signed integer overflow:
56 // script/script.h:264:93: runtime error: signed integer overflow: -9223126527765971126 + -9223372036854756825 cannot be represented in type 'long'
59 }
60 // Avoid signed integer overflow:
61 // script/script.h:265:93: runtime error: signed integer overflow: 6052837899185946624 - -9223372036854775808 cannot be represented in type 'long'
64 }
65 },
66 [&] {
69 // Avoid assertion failure:
70 // ./script/script.h:292: CScriptNum &CScriptNum::operator+=(const int64_t &): Assertion `rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) || (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs)' failed.
71 return;
72 }
74 },
75 [&] {
78 // Avoid assertion failure:
79 // ./script/script.h:300: CScriptNum &CScriptNum::operator-=(const int64_t &): Assertion `rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) || (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs)' failed.
80 return;
81 }
83 },
84 [&] {
86 },
87 [&] {
89 },
90 [&] {
92 },
93 [&] {
94 if (script_num == CScriptNum{std::numeric_limits<int64_t>::min()}) {
95 // Avoid assertion failure:
96 // ./script/script.h:279: CScriptNum CScriptNum::operator-() const: Assertion `m_value != std::numeric_limits<int64_t>::min()' failed.
97 return;
98 }
100 },
101 [&] {
103 },
104 [&] {
107 // Avoid assertion failure:
108 // ./script/script.h:292: CScriptNum &CScriptNum::operator+=(const int64_t &): Assertion `rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) || (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs)' failed.
109 return;
110 }
112 },
113 [&] {
116 // Avoid assertion failure:
117 // ./script/script.h:300: CScriptNum &CScriptNum::operator-=(const int64_t &): Assertion `rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) || (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs)' failed.
118 return;
119 }
121 },
122 [&] {
124 });
125 (void)script_num.getint();
126 (void)script_num.getvch();
127 }
128}
#define FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
CScriptNum ConsumeScriptNum(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition util.h:153
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition util.h:35
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:38