Skip to content

Commit fa103db

Browse files
author
MarcoFalke
committed
scripted-diff: Rename SetHex to SetHexDeprecated
SetHex is fragile, because it accepts any non-hex input or any length of input, without error feedback. This can lead to issues when the input is truncated or otherwise corrupted. Document the problem by renaming the method. In the future, the fragile method should be removed from the public interface. -BEGIN VERIFY SCRIPT- sed -i 's/SetHex/SetHexDeprecated/g' $( git grep -l SetHex ./src ) -END VERIFY SCRIPT-
1 parent fafe4b8 commit fa103db

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

src/core_read.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ bool ParseHashStr(const std::string& strHex, uint256& result)
239239
if ((strHex.size() != 64) || !IsHex(strHex))
240240
return false;
241241

242-
result.SetHex(strHex);
242+
result.SetHexDeprecated(strHex);
243243
return true;
244244
}
245245

src/qt/transactiontablemodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ void TransactionTableModel::updateAmountColumnTitle()
277277
void TransactionTableModel::updateTransaction(const QString &hash, int status, bool showTransaction)
278278
{
279279
uint256 updated;
280-
updated.SetHex(hash.toStdString());
280+
updated.SetHexDeprecated(hash.toStdString());
281281

282282
priv->updateWallet(walletModel->wallet(), updated, status, showTransaction);
283283
}

src/qt/transactionview.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void TransactionView::contextualMenu(const QPoint &point)
396396

397397
// check if transaction can be abandoned, disable context menu action in case it doesn't
398398
uint256 hash;
399-
hash.SetHex(selection.at(0).data(TransactionTableModel::TxHashRole).toString().toStdString());
399+
hash.SetHexDeprecated(selection.at(0).data(TransactionTableModel::TxHashRole).toString().toStdString());
400400
abandonAction->setEnabled(model->wallet().transactionCanBeAbandoned(hash));
401401
bumpFeeAction->setEnabled(model->wallet().transactionCanBeBumped(hash));
402402
copyAddressAction->setEnabled(GUIUtil::hasEntryData(transactionView, 0, TransactionTableModel::AddressRole));
@@ -416,7 +416,7 @@ void TransactionView::abandonTx()
416416
// get the hash from the TxHashRole (QVariant / QString)
417417
uint256 hash;
418418
QString hashQStr = selection.at(0).data(TransactionTableModel::TxHashRole).toString();
419-
hash.SetHex(hashQStr.toStdString());
419+
hash.SetHexDeprecated(hashQStr.toStdString());
420420

421421
// Abandon the wallet transaction over the walletModel
422422
model->wallet().abandonTransaction(hash);
@@ -431,7 +431,7 @@ void TransactionView::bumpFee([[maybe_unused]] bool checked)
431431
// get the hash from the TxHashRole (QVariant / QString)
432432
uint256 hash;
433433
QString hashQStr = selection.at(0).data(TransactionTableModel::TxHashRole).toString();
434-
hash.SetHex(hashQStr.toStdString());
434+
hash.SetHexDeprecated(hashQStr.toStdString());
435435

436436
// Bump tx fee over the walletModel
437437
uint256 newHash;

src/test/uint256_tests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static std::string ArrayToString(const unsigned char A[], unsigned int width)
6262
inline uint160 uint160S(std::string_view str)
6363
{
6464
uint160 rv;
65-
rv.SetHex(str);
65+
rv.SetHexDeprecated(str);
6666
return rv;
6767
}
6868

@@ -157,7 +157,7 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
157157
uint256S("1000000000000000000000000000000000000000000000000000000000000002"));
158158
}
159159

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

174-
TmpL.SetHex(R1L.ToString());
174+
TmpL.SetHexDeprecated(R1L.ToString());
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 SetHex begin() end() size() GetLow64 G
214214
BOOST_CHECK_EQUAL(MaxS.GetHex(), MaxS.ToString());
215215
uint160 TmpS(R1S);
216216
BOOST_CHECK_EQUAL(TmpS, R1S);
217-
TmpS.SetHex(R2S.ToString()); BOOST_CHECK_EQUAL(TmpS, R2S);
218-
TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK_EQUAL(TmpS, uint160());
217+
TmpS.SetHexDeprecated(R2S.ToString()); BOOST_CHECK_EQUAL(TmpS, R2S);
218+
TmpS.SetHexDeprecated(ZeroS.ToString()); BOOST_CHECK_EQUAL(TmpS, uint160());
219219

220-
TmpS.SetHex(R1S.ToString());
220+
TmpS.SetHexDeprecated(R1S.ToString());
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.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ std::string base_blob<BITS>::GetHex() const
1818
}
1919

2020
template <unsigned int BITS>
21-
void base_blob<BITS>::SetHex(const std::string_view str)
21+
void base_blob<BITS>::SetHexDeprecated(const std::string_view str)
2222
{
2323
std::fill(m_data.begin(), m_data.end(), 0);
2424

@@ -52,12 +52,12 @@ std::string base_blob<BITS>::ToString() const
5252
// Explicit instantiations for base_blob<160>
5353
template std::string base_blob<160>::GetHex() const;
5454
template std::string base_blob<160>::ToString() const;
55-
template void base_blob<160>::SetHex(std::string_view);
55+
template void base_blob<160>::SetHexDeprecated(std::string_view);
5656

5757
// Explicit instantiations for base_blob<256>
5858
template std::string base_blob<256>::GetHex() const;
5959
template std::string base_blob<256>::ToString() const;
60-
template void base_blob<256>::SetHex(std::string_view);
60+
template void base_blob<256>::SetHexDeprecated(std::string_view);
6161

6262
const uint256 uint256::ZERO(0);
6363
const uint256 uint256::ONE(1);

src/uint256.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class base_blob
5959

6060
// Hex string representations are little-endian.
6161
std::string GetHex() const;
62-
void SetHex(std::string_view str);
62+
void SetHexDeprecated(std::string_view str);
6363
std::string ToString() const;
6464

6565
constexpr const unsigned char* data() const { return m_data.data(); }
@@ -119,7 +119,7 @@ class uint256 : public base_blob<256> {
119119
inline uint256 uint256S(std::string_view str)
120120
{
121121
uint256 rv;
122-
rv.SetHex(str);
122+
rv.SetHexDeprecated(str);
123123
return rv;
124124
}
125125

0 commit comments

Comments
 (0)