From a6e709e5ffbc1ab680dd5eb74206de749d829c01 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 12 Nov 2012 15:55:38 -0800 Subject: [PATCH] Some fixes for broken math in this code. --- src/cpp/ripple/uint256.h | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/cpp/ripple/uint256.h b/src/cpp/ripple/uint256.h index 3e2ab10e6..e7e5e449e 100644 --- a/src/cpp/ripple/uint256.h +++ b/src/cpp/ripple/uint256.h @@ -23,7 +23,7 @@ #endif // These classes all store their values internally -// in little-endian form +// in big-endian form inline int Testuint256AdHoc(std::vector vArg); @@ -36,7 +36,7 @@ protected: enum { WIDTH=BITS/32 }; // This is really big-endian in byte order. - // We use unsigned int for speed. + // We sometimes use unsigned int for speed. unsigned int pn[WIDTH]; public: @@ -73,7 +73,7 @@ public: zero(); // Put in least significant bits. - ((uint64_t *) end())[-1] = htobe64(uHost); + ((uint64_t *) end())[-1] = htobe64(uHost); return *this; } @@ -105,10 +105,12 @@ public: base_uint& operator++() { // prefix operator - int i = WIDTH; - - while (i-- && !++pn[i]) - ; + for (int i = WIDTH - 1; i >= 0; --i) + { + pn[i] = htobe32(be32toh(pn[i]) + 1); + if (pn[i] != 0) + break; + } return *this; } @@ -124,10 +126,13 @@ public: base_uint& operator--() { - int i = WIDTH; - - while (i-- && !pn[i]--) - ; + for (int i = WIDTH - 1; i >= 0; --i) + { + uint32 prev = pn[i]; + pn[i] = htobe32(be32toh(pn[i]) - 1); + if (prev != 0) + break; + } return *this; } @@ -169,10 +174,14 @@ public: const unsigned char* pAEnd = a.end(); const unsigned char* pB = b.begin(); - while (pA != pAEnd && *pA == *pB) - pA++, pB++; + while (*pA == *pB) + { + if (++pA == pAEnd) + return 0; + ++pB; + } - return pA == pAEnd ? 0 : *pA < *pB ? -1 : *pA > *pB ? 1 : 0; + return (*pA < *pB) ? -1 : 1; } friend inline bool operator<(const base_uint& a, const base_uint& b)