aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2021-08-24 09:55:35 +0800
committerfanquake <fanquake@gmail.com>2021-08-24 10:43:38 +0800
commit61a843e43bf113df7fa6062c84257c76ee40141f (patch)
treecbf88957727f1baa93a67b5c58630461c71c123f /src/util
parentd3203a99d886177eee9d1f9cd8411e215118a4e6 (diff)
parentf7752adba5dd35fccd3f2144cfcf03538ebf275b (diff)
Merge bitcoin/bitcoin#22220: util: make ParseMoney return a std::optional<CAmount>
f7752adba5dd35fccd3f2144cfcf03538ebf275b util: check MoneyRange() inside ParseMoney() (fanquake) 5ef2738089efd396186775ad23aaec71ea44ebb1 util: make ParseMoney return a std::optional<CAmount> (fanquake) Pull request description: Related discussion in #22193. ACKs for top commit: MarcoFalke: review ACK f7752adba5dd35fccd3f2144cfcf03538ebf275b 📄 Tree-SHA512: 88453f9e28f668deff4290d4bc0b2468cbd54699a3be1bfeac63a512276d309354672e7ea7deefa01466c3d9d826e837cc1ea244d4d74b4fa9c11c56f074e098
Diffstat (limited to 'src/util')
-rw-r--r--src/util/moneystr.cpp29
-rw-r--r--src/util/moneystr.h3
2 files changed, 20 insertions, 12 deletions
diff --git a/src/util/moneystr.cpp b/src/util/moneystr.cpp
index 3f9ce7dce4..d3f4029607 100644
--- a/src/util/moneystr.cpp
+++ b/src/util/moneystr.cpp
@@ -5,10 +5,13 @@
#include <util/moneystr.h>
+#include <amount.h>
#include <tinyformat.h>
#include <util/strencodings.h>
#include <util/string.h>
+#include <optional>
+
std::string FormatMoney(const CAmount n)
{
// Note: not using straight sprintf here because we do NOT want
@@ -35,14 +38,14 @@ std::string FormatMoney(const CAmount n)
}
-bool ParseMoney(const std::string& money_string, CAmount& nRet)
+std::optional<CAmount> ParseMoney(const std::string& money_string)
{
if (!ValidAsCString(money_string)) {
- return false;
+ return std::nullopt;
}
const std::string str = TrimString(money_string);
if (str.empty()) {
- return false;
+ return std::nullopt;
}
std::string strWhole;
@@ -62,21 +65,25 @@ bool ParseMoney(const std::string& money_string, CAmount& nRet)
break;
}
if (IsSpace(*p))
- return false;
+ return std::nullopt;
if (!IsDigit(*p))
- return false;
+ return std::nullopt;
strWhole.insert(strWhole.end(), *p);
}
if (*p) {
- return false;
+ return std::nullopt;
}
if (strWhole.size() > 10) // guard against 63 bit overflow
- return false;
+ return std::nullopt;
if (nUnits < 0 || nUnits > COIN)
- return false;
+ return std::nullopt;
int64_t nWhole = atoi64(strWhole);
- CAmount nValue = nWhole*COIN + nUnits;
- nRet = nValue;
- return true;
+ CAmount value = nWhole * COIN + nUnits;
+
+ if (!MoneyRange(value)) {
+ return std::nullopt;
+ }
+
+ return value;
}
diff --git a/src/util/moneystr.h b/src/util/moneystr.h
index 2aedbee358..b71dffd0db 100644
--- a/src/util/moneystr.h
+++ b/src/util/moneystr.h
@@ -12,6 +12,7 @@
#include <amount.h>
#include <attributes.h>
+#include <optional>
#include <string>
/* Do not use these functions to represent or parse monetary amounts to or from
@@ -19,6 +20,6 @@
*/
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);
+std::optional<CAmount> ParseMoney(const std::string& str);
#endif // BITCOIN_UTIL_MONEYSTR_H