Skip to content

Commit 9027960

Browse files
committed
Merge #18225: util: Fail to parse empty string in ParseMoney
8888461 util: Fail to parse empty string in ParseMoney (MarcoFalke) fab30b6 util: Remove unused ParseMoney that takes a c_str (MarcoFalke) Pull request description: Supplying a fee rate or an amount on the command line as an empty string, which currently parses as `0` seems fragile and confusing. See for example the confusion in #18214. Fixes #18214 ACKs for top commit: Empact: Code Review ACK bitcoin/bitcoin@8888461 achow101: ACK 8888461 instagibbs: utACK bitcoin/bitcoin@8888461 Tree-SHA512: ac2d6b7fa89fe5809c34d5f49831042032591c34fb3c76908d72fed51e8bced41bf2b41dc1b3be34ee691a40463355649857a7a8f378709d38ae89503feb11c2
2 parents eca4d8e + 8888461 commit 9027960

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/test/util_tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,12 @@ BOOST_AUTO_TEST_CASE(util_ParseMoney)
11991199
BOOST_CHECK(ParseMoney("0.00000001", ret));
12001200
BOOST_CHECK_EQUAL(ret, COIN/100000000);
12011201

1202+
// Parsing amount that can not be represented in ret should fail
1203+
BOOST_CHECK(!ParseMoney("0.000000001", ret));
1204+
1205+
// Parsing empty string should fail
1206+
BOOST_CHECK(!ParseMoney("", ret));
1207+
12021208
// Attempted 63 bit overflow should fail
12031209
BOOST_CHECK(!ParseMoney("92233720368.54775808", ret));
12041210

src/util/moneystr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ bool ParseMoney(const std::string& str, CAmount& nRet)
3636
if (!ValidAsCString(str)) {
3737
return false;
3838
}
39-
return ParseMoney(str.c_str(), nRet);
40-
}
4139

42-
bool ParseMoney(const char* pszIn, CAmount& nRet)
43-
{
40+
if (str.empty()) {
41+
return false;
42+
}
43+
4444
std::string strWhole;
4545
int64_t nUnits = 0;
46-
const char* p = pszIn;
46+
const char* p = str.c_str();
4747
while (IsSpace(*p))
4848
p++;
4949
for (; *p; p++)

src/util/moneystr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* JSON but use AmountFromValue and ValueFromAmount for that.
1919
*/
2020
std::string FormatMoney(const CAmount& n);
21+
/** Parse an amount denoted in full coins. E.g. "0.0034" supplied on the command line. **/
2122
NODISCARD bool ParseMoney(const std::string& str, CAmount& nRet);
22-
NODISCARD bool ParseMoney(const char* pszIn, CAmount& nRet);
2323

2424
#endif // BITCOIN_UTIL_MONEYSTR_H

0 commit comments

Comments
 (0)