Skip to content

Commit f65c9ad

Browse files
committed
Check for overflow when calculating sum of outputs
1 parent 67de1ee commit f65c9ad

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/primitives/transaction.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <tinyformat.h>
1010
#include <util/strencodings.h>
1111

12+
#include <assert.h>
13+
1214
std::string COutPoint::ToString() const
1315
{
1416
return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
@@ -84,10 +86,11 @@ CAmount CTransaction::GetValueOut() const
8486
{
8587
CAmount nValueOut = 0;
8688
for (const auto& tx_out : vout) {
87-
nValueOut += tx_out.nValue;
88-
if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut))
89+
if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut + tx_out.nValue))
8990
throw std::runtime_error(std::string(__func__) + ": value out of range");
91+
nValueOut += tx_out.nValue;
9092
}
93+
assert(MoneyRange(nValueOut));
9194
return nValueOut;
9295
}
9396

0 commit comments

Comments
 (0)