Skip to content

Commit e980214

Browse files
author
pierrenn
committed
serialization: prevent int overflow for big Coin::nHeight
1 parent 97b0687 commit e980214

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/coins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Coin
5959
template<typename Stream>
6060
void Serialize(Stream &s) const {
6161
assert(!IsSpent());
62-
uint32_t code = nHeight * 2 + fCoinBase;
62+
uint32_t code = nHeight * uint32_t{2} + fCoinBase;
6363
::Serialize(s, VARINT(code));
6464
::Serialize(s, Using<TxOutCompression>(out));
6565
}

src/undo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct TxInUndoFormatter
2424
{
2525
template<typename Stream>
2626
void Ser(Stream &s, const Coin& txout) {
27-
::Serialize(s, VARINT(txout.nHeight * 2 + (txout.fCoinBase ? 1u : 0u)));
27+
::Serialize(s, VARINT(txout.nHeight * uint32_t{2} + txout.fCoinBase ));
2828
if (txout.nHeight > 0) {
2929
// Required to maintain compatibility with older undo format.
3030
::Serialize(s, (unsigned char)0);
@@ -34,9 +34,9 @@ struct TxInUndoFormatter
3434

3535
template<typename Stream>
3636
void Unser(Stream &s, Coin& txout) {
37-
unsigned int nCode = 0;
37+
uint32_t nCode = 0;
3838
::Unserialize(s, VARINT(nCode));
39-
txout.nHeight = nCode / 2;
39+
txout.nHeight = nCode >> 1;
4040
txout.fCoinBase = nCode & 1;
4141
if (txout.nHeight > 0) {
4242
// Old versions stored the version number for the last spend of

0 commit comments

Comments
 (0)