From fab30b61eb51538a4db62e34f7133c44575b3fe9 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Sat, 29 Feb 2020 00:27:48 +0700 Subject: util: Remove unused ParseMoney that takes a c_str --- src/util/moneystr.cpp | 6 +----- src/util/moneystr.h | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/util/moneystr.cpp b/src/util/moneystr.cpp index 2797f450ca..981cb1b267 100644 --- a/src/util/moneystr.cpp +++ b/src/util/moneystr.cpp @@ -36,14 +36,10 @@ bool ParseMoney(const std::string& str, CAmount& nRet) if (!ValidAsCString(str)) { return false; } - return ParseMoney(str.c_str(), nRet); -} -bool ParseMoney(const char* pszIn, CAmount& nRet) -{ std::string strWhole; int64_t nUnits = 0; - const char* p = pszIn; + const char* p = str.c_str(); while (IsSpace(*p)) p++; for (; *p; p++) diff --git a/src/util/moneystr.h b/src/util/moneystr.h index 027c7e2e53..d8b08adc24 100644 --- a/src/util/moneystr.h +++ b/src/util/moneystr.h @@ -18,7 +18,7 @@ * JSON but use AmountFromValue and ValueFromAmount for that. */ std::string FormatMoney(const CAmount& n); +/** Parse an amount denoted in full coins. E.g. "0.0034" supplied on the command line. **/ NODISCARD bool ParseMoney(const std::string& str, CAmount& nRet); -NODISCARD bool ParseMoney(const char* pszIn, CAmount& nRet); #endif // BITCOIN_UTIL_MONEYSTR_H -- cgit v1.2.3 From 8888461f6814ae8b6221b02049fb9e1f69a5ff70 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Sat, 29 Feb 2020 00:29:04 +0700 Subject: util: Fail to parse empty string in ParseMoney --- src/test/util_tests.cpp | 6 ++++++ src/util/moneystr.cpp | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index f86e713676..536ff3ba25 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1199,6 +1199,12 @@ BOOST_AUTO_TEST_CASE(util_ParseMoney) 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.cpp b/src/util/moneystr.cpp index 981cb1b267..40d8918dfc 100644 --- a/src/util/moneystr.cpp +++ b/src/util/moneystr.cpp @@ -37,6 +37,10 @@ bool ParseMoney(const std::string& str, CAmount& nRet) return false; } + if (str.empty()) { + return false; + } + std::string strWhole; int64_t nUnits = 0; const char* p = str.c_str(); -- cgit v1.2.3