serialization: prevent int overflow for big Coin::nHeight

This commit is contained in:
pierrenn 2020-03-26 07:48:48 +09:00
parent 97b0687501
commit e980214bc4
2 changed files with 4 additions and 4 deletions

View file

@ -59,7 +59,7 @@ public:
template<typename Stream> template<typename Stream>
void Serialize(Stream &s) const { void Serialize(Stream &s) const {
assert(!IsSpent()); assert(!IsSpent());
uint32_t code = nHeight * 2 + fCoinBase; uint32_t code = nHeight * uint32_t{2} + fCoinBase;
::Serialize(s, VARINT(code)); ::Serialize(s, VARINT(code));
::Serialize(s, Using<TxOutCompression>(out)); ::Serialize(s, Using<TxOutCompression>(out));
} }

View file

@ -24,7 +24,7 @@ struct TxInUndoFormatter
{ {
template<typename Stream> template<typename Stream>
void Ser(Stream &s, const Coin& txout) { void Ser(Stream &s, const Coin& txout) {
::Serialize(s, VARINT(txout.nHeight * 2 + (txout.fCoinBase ? 1u : 0u))); ::Serialize(s, VARINT(txout.nHeight * uint32_t{2} + txout.fCoinBase ));
if (txout.nHeight > 0) { if (txout.nHeight > 0) {
// Required to maintain compatibility with older undo format. // Required to maintain compatibility with older undo format.
::Serialize(s, (unsigned char)0); ::Serialize(s, (unsigned char)0);
@ -34,9 +34,9 @@ struct TxInUndoFormatter
template<typename Stream> template<typename Stream>
void Unser(Stream &s, Coin& txout) { void Unser(Stream &s, Coin& txout) {
unsigned int nCode = 0; uint32_t nCode = 0;
::Unserialize(s, VARINT(nCode)); ::Unserialize(s, VARINT(nCode));
txout.nHeight = nCode / 2; txout.nHeight = nCode >> 1;
txout.fCoinBase = nCode & 1; txout.fCoinBase = nCode & 1;
if (txout.nHeight > 0) { if (txout.nHeight > 0) {
// Old versions stored the version number for the last spend of // Old versions stored the version number for the last spend of