diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1448,6 +1448,12 @@ BOOST_CHECK(ParseMoney("0.00000001", ret)); BOOST_CHECK_EQUAL(ret, COIN / 100000000); + // Parsing amount that can not be represented in ret should fail + BOOST_CHECK(!ParseMoney("0.000000001", ret)); + + // Parsing empty string should fail + BOOST_CHECK(!ParseMoney("", ret)); + // Attempted 63 bit overflow should fail BOOST_CHECK(!ParseMoney("92233720368.54775808", ret)); diff --git a/src/util/moneystr.h b/src/util/moneystr.h --- a/src/util/moneystr.h +++ b/src/util/moneystr.h @@ -19,7 +19,10 @@ * JSON but use AmountFromValue and ValueFromAmount for that. */ std::string FormatMoney(const Amount n); +/** + * Parse an amount denoted in full coins. E.g. "0.0034" supplied on the command + * line. + **/ NODISCARD bool ParseMoney(const std::string &str, Amount &nRet); -NODISCARD bool ParseMoney(const char *pszIn, Amount &nRet); #endif // BITCOIN_UTIL_MONEYSTR_H diff --git a/src/util/moneystr.cpp b/src/util/moneystr.cpp --- a/src/util/moneystr.cpp +++ b/src/util/moneystr.cpp @@ -35,13 +35,14 @@ if (!ValidAsCString(str)) { return false; } - return ParseMoney(str.c_str(), nRet); -} -bool ParseMoney(const char *pszIn, Amount &nRet) { + if (str.empty()) { + return false; + } + std::string strWhole; Amount nUnits = Amount::zero(); - const char *p = pszIn; + const char *p = str.c_str(); while (IsSpace(*p)) { p++; }