Skip to content

Commit

Permalink
util: Add ConsumeArithUInt256InRange fuzzing helper
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofleon committed Nov 5, 2024
1 parent f1bcf3e commit fa327c7
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/test/fuzz/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ template <typename WeakEnumType, size_t size>
return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
}

[[nodiscard]] inline arith_uint256 ConsumeArithUInt256InRange(FuzzedDataProvider& fuzzed_data_provider, const arith_uint256& min, const arith_uint256& max) noexcept
{
assert(min <= max);
const arith_uint256 range = max - min;
const arith_uint256 value = ConsumeArithUInt256(fuzzed_data_provider);
arith_uint256 result = value;
// Avoid division by 0, in case range + 1 results in overflow.
if (range != ~arith_uint256(0)) {
const arith_uint256 quotient = value / (range + 1);
result = value - (quotient * (range + 1));
}
result += min;
assert(result >= min && result <= max);
return result;
}

[[nodiscard]] std::map<COutPoint, Coin> ConsumeCoins(FuzzedDataProvider& fuzzed_data_provider) noexcept;

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

0 comments on commit fa327c7

Please sign in to comment.