Skip to content

Commit fa327c7

Browse files
committed
util: Add ConsumeArithUInt256InRange fuzzing helper
1 parent f1bcf3e commit fa327c7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/test/fuzz/util.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@ template <typename WeakEnumType, size_t size>
180180
return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
181181
}
182182

183+
[[nodiscard]] inline arith_uint256 ConsumeArithUInt256InRange(FuzzedDataProvider& fuzzed_data_provider, const arith_uint256& min, const arith_uint256& max) noexcept
184+
{
185+
assert(min <= max);
186+
const arith_uint256 range = max - min;
187+
const arith_uint256 value = ConsumeArithUInt256(fuzzed_data_provider);
188+
arith_uint256 result = value;
189+
// Avoid division by 0, in case range + 1 results in overflow.
190+
if (range != ~arith_uint256(0)) {
191+
const arith_uint256 quotient = value / (range + 1);
192+
result = value - (quotient * (range + 1));
193+
}
194+
result += min;
195+
assert(result >= min && result <= max);
196+
return result;
197+
}
198+
183199
[[nodiscard]] std::map<COutPoint, Coin> ConsumeCoins(FuzzedDataProvider& fuzzed_data_provider) noexcept;
184200

185201
[[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept;

0 commit comments

Comments
 (0)