Skip to content

Commit 127b30c

Browse files
committed
Merge #14838: Use const in COutPoint class
cf4b032 Use std::numeric_limits<UNSIGNED>::max()) instead of (UNSIGNED)-1 (practicalswift) 6b82fc5 Use const in COutPoint class (Hennadii Stepanov) Pull request description: Refactoring: - all cases of using `(uint32_t) -1` in `COutPoint` class are replaced with const; - also all remaining instances of `(UNSIGNED)-1` transformed to `std::numeric_limits<UNSIGNED>::max()` (by @practicalswift). Tree-SHA512: fc7fe9838b6e5136d8b97ea3d6f64c4aaa1215f4369832df432cab017396620bb6e30520a64180ceab6de222562ac11eab243a78dfa5a658ba018835a34caa19
2 parents 1858e6f + cf4b032 commit 127b30c

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

src/arith_uint256.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <assert.h>
1010
#include <cstring>
11+
#include <limits>
1112
#include <stdexcept>
1213
#include <stdint.h>
1314
#include <string>
@@ -189,7 +190,7 @@ class base_uint
189190
{
190191
// prefix operator
191192
int i = 0;
192-
while (i < WIDTH && --pn[i] == (uint32_t)-1)
193+
while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
193194
i++;
194195
return *this;
195196
}

src/primitives/transaction.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class COutPoint
2121
uint256 hash;
2222
uint32_t n;
2323

24-
COutPoint(): n((uint32_t) -1) { }
24+
static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();
25+
26+
COutPoint(): n(NULL_INDEX) { }
2527
COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
2628

2729
ADD_SERIALIZE_METHODS;
@@ -32,8 +34,8 @@ class COutPoint
3234
READWRITE(n);
3335
}
3436

35-
void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
36-
bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
37+
void SetNull() { hash.SetNull(); n = NULL_INDEX; }
38+
bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }
3739

3840
friend bool operator<(const COutPoint& a, const COutPoint& b)
3941
{

src/streams.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ class CBufferedFile
761761

762762
public:
763763
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
764-
nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0)
764+
nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit(std::numeric_limits<uint64_t>::max()), nRewind(nRewindIn), vchBuf(nBufSize, 0)
765765
{
766766
src = fileIn;
767767
}
@@ -846,7 +846,7 @@ class CBufferedFile
846846

847847
// prevent reading beyond a certain position
848848
// no argument removes the limit
849-
bool SetLimit(uint64_t nPos = (uint64_t)(-1)) {
849+
bool SetLimit(uint64_t nPos = std::numeric_limits<uint64_t>::max()) {
850850
if (nPos < nReadPos)
851851
return false;
852852
nReadLimit = nPos;

src/test/serialize_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ BOOST_AUTO_TEST_CASE(varints)
200200
}
201201

202202
for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
203-
uint64_t j = -1;
203+
uint64_t j = std::numeric_limits<uint64_t>::max();
204204
ss >> VARINT(j);
205205
BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
206206
}

src/test/sighash_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {
105105
txin.prevout.hash = InsecureRand256();
106106
txin.prevout.n = InsecureRandBits(2);
107107
RandomScript(txin.scriptSig);
108-
txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : (unsigned int)-1;
108+
txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : std::numeric_limits<uint32_t>::max();
109109
}
110110
for (int out = 0; out < outs; out++) {
111111
tx.vout.push_back(CTxOut());

0 commit comments

Comments
 (0)