Skip to content

Commit fad2991

Browse files
author
MarcoFalke
committed
refactor: Implement strict uint256::FromHex()
This is a safe replacement of the previous SetHex, which now returns an optional to indicate success or failure. The code is similar to the ParseHashStr helper, which will be removed in a later commit.
1 parent fa103db commit fad2991

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/test/uint256_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
157157
uint256S("1000000000000000000000000000000000000000000000000000000000000002"));
158158
}
159159

160-
BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHexDeprecated begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
160+
BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
161161
{
162162
BOOST_CHECK_EQUAL(R1L.GetHex(), R1L.ToString());
163163
BOOST_CHECK_EQUAL(R2L.GetHex(), R2L.ToString());
@@ -168,10 +168,10 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHexDeprecated begin() end() size()
168168
// Verify previous values don't persist when setting to truncated string.
169169
TmpL.SetHexDeprecated("21");
170170
BOOST_CHECK_EQUAL(TmpL.ToString(), "0000000000000000000000000000000000000000000000000000000000000021");
171-
TmpL.SetHexDeprecated(R2L.ToString()); BOOST_CHECK_EQUAL(TmpL, R2L);
172-
TmpL.SetHexDeprecated(ZeroL.ToString()); BOOST_CHECK_EQUAL(TmpL, uint256());
171+
BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
172+
BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), uint256());
173173

174-
TmpL.SetHexDeprecated(R1L.ToString());
174+
TmpL = uint256::FromHex(R1L.ToString()).value();
175175
BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array + R1L.size());
176176
BOOST_CHECK_EQUAL_COLLECTIONS(TmpL.begin(), TmpL.end(), R1Array, R1Array + TmpL.size());
177177
BOOST_CHECK_EQUAL_COLLECTIONS(R2L.begin(), R2L.end(), R2Array, R2Array + R2L.size());
@@ -214,10 +214,10 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHexDeprecated begin() end() size()
214214
BOOST_CHECK_EQUAL(MaxS.GetHex(), MaxS.ToString());
215215
uint160 TmpS(R1S);
216216
BOOST_CHECK_EQUAL(TmpS, R1S);
217-
TmpS.SetHexDeprecated(R2S.ToString()); BOOST_CHECK_EQUAL(TmpS, R2S);
218-
TmpS.SetHexDeprecated(ZeroS.ToString()); BOOST_CHECK_EQUAL(TmpS, uint160());
217+
BOOST_CHECK_EQUAL(uint160::FromHex(R2S.ToString()).value(), R2S);
218+
BOOST_CHECK_EQUAL(uint160::FromHex(ZeroS.ToString()).value(), uint160());
219219

220-
TmpS.SetHexDeprecated(R1S.ToString());
220+
TmpS = uint160::FromHex(R1S.ToString()).value();
221221
BOOST_CHECK_EQUAL_COLLECTIONS(R1S.begin(), R1S.end(), R1Array, R1Array + R1S.size());
222222
BOOST_CHECK_EQUAL_COLLECTIONS(TmpS.begin(), TmpS.end(), R1Array, R1Array + TmpS.size());
223223
BOOST_CHECK_EQUAL_COLLECTIONS(R2S.begin(), R2S.end(), R2Array, R2Array + R2S.size());

src/uint256.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -8,12 +8,14 @@
88

99
#include <crypto/common.h>
1010
#include <span.h>
11+
#include <util/strencodings.h>
1112

1213
#include <algorithm>
1314
#include <array>
1415
#include <cassert>
16+
#include <cstdint>
1517
#include <cstring>
16-
#include <stdint.h>
18+
#include <optional>
1719
#include <string>
1820

1921
/** Template base class for fixed-sized opaque blobs. */
@@ -59,6 +61,7 @@ class base_blob
5961

6062
// Hex string representations are little-endian.
6163
std::string GetHex() const;
64+
/** Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated */
6265
void SetHexDeprecated(std::string_view str);
6366
std::string ToString() const;
6467

@@ -88,12 +91,30 @@ class base_blob
8891
}
8992
};
9093

94+
namespace detail {
95+
/**
96+
* Writes the hex string (treated as little-endian) into a new uintN_t object
97+
* and only returns a value iff all of the checks pass:
98+
* - Input length is uintN_t::size()*2
99+
* - All characters are hex
100+
*/
101+
template <class uintN_t>
102+
std::optional<uintN_t> FromHex(std::string_view str)
103+
{
104+
if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
105+
uintN_t rv;
106+
rv.SetHexDeprecated(str);
107+
return rv;
108+
}
109+
} // namespace detail
110+
91111
/** 160-bit opaque blob.
92112
* @note This type is called uint160 for historical reasons only. It is an opaque
93113
* blob of 160 bits and has no integer operations.
94114
*/
95115
class uint160 : public base_blob<160> {
96116
public:
117+
static std::optional<uint160> FromHex(std::string_view str) { return detail::FromHex<uint160>(str); }
97118
constexpr uint160() = default;
98119
constexpr explicit uint160(Span<const unsigned char> vch) : base_blob<160>(vch) {}
99120
};
@@ -105,6 +126,7 @@ class uint160 : public base_blob<160> {
105126
*/
106127
class uint256 : public base_blob<256> {
107128
public:
129+
static std::optional<uint256> FromHex(std::string_view str) { return detail::FromHex<uint256>(str); }
108130
constexpr uint256() = default;
109131
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
110132
constexpr explicit uint256(Span<const unsigned char> vch) : base_blob<256>(vch) {}
@@ -113,8 +135,7 @@ class uint256 : public base_blob<256> {
113135
};
114136

115137
/* uint256 from std::string_view, treated as little-endian.
116-
* This is not a uint256 constructor because of historical fears of uint256(0)
117-
* resolving to a NULL string and crashing.
138+
* DEPRECATED. Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
118139
*/
119140
inline uint256 uint256S(std::string_view str)
120141
{

0 commit comments

Comments
 (0)