This was reported by practicalswift here #18046
The exact order of the if, is important, we first do !MoneyRange(tx_out.nValue)
to make sure the amount is non-negative. and then std::numeric_limits<CAmount>::max() - tx_out.nValue < nValueOut
checks that the addition cannot overflow (if we won’t check that the amount is positive this check can also overflow! (by doing something like max - -max
))
and only then we make sure that the some is also valid !MoneyRange(nValueOut + tx_out.nValue)
if any of these conditions fail we throw.
the overflowing logic:
0a + b > max // we want to fail if a+b is more than the maximum -> will overflow
1b > max - a
2max - a < b
Closes: #18046