Skip to content

Commit

Permalink
state: Avoid overflows in blob gas price
Browse files Browse the repository at this point in the history
  • Loading branch information
pdobacz committed Dec 18, 2024
1 parent 935894b commit 8dbd8c6
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion test/state/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ intx::uint256 compute_blob_gas_price(uint64_t excess_blob_gas) noexcept
intx::uint256 i = 1;
intx::uint256 output = 0;
intx::uint256 numerator_accum = factor * denominator;
const intx::uint256 numerator256 = numerator;
while (numerator_accum > 0)
{
output += numerator_accum;
numerator_accum = (numerator_accum * numerator) / (denominator * i);
// Ensure the multiplication won't overflow 256 bits.
if (const auto p = intx::umul(numerator_accum, numerator256);
p <= std::numeric_limits<intx::uint256>::max())
numerator_accum = intx::uint256(p) / (denominator * i);
else
return std::numeric_limits<intx::uint256>::max();

Check warning on line 30 in test/state/block.cpp

View check run for this annotation

Codecov / codecov/patch

test/state/block.cpp#L30

Added line #L30 was not covered by tests
i += 1;
}
return output / denominator;
Expand Down

0 comments on commit 8dbd8c6

Please sign in to comment.