aboutsummaryrefslogtreecommitdiff
path: root/src/util/strencodings.cpp
diff options
context:
space:
mode:
authorJon Atack <jon@atack.com>2021-03-02 22:37:50 +0100
committerJon Atack <jon@atack.com>2021-03-16 19:52:33 +0100
commit2875a764f7d8b1503c7bdb2f262964f7a0cb5fc3 (patch)
tree56880cb31aa5896e256f08b29285e3e774819d65 /src/util/strencodings.cpp
parent6423c8175fad3163c10ffdb49e0df48e4e4931f1 (diff)
downloadbitcoin-2875a764f7d8b1503c7bdb2f262964f7a0cb5fc3.tar.xz
util: add ParseUInt16(), use it in SplitHostPort()
Diffstat (limited to 'src/util/strencodings.cpp')
-rw-r--r--src/util/strencodings.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp
index e9d85c4c30..8ccb71280d 100644
--- a/src/util/strencodings.cpp
+++ b/src/util/strencodings.cpp
@@ -115,8 +115,8 @@ void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut)
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
- int32_t n;
- if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
+ uint16_t n;
+ if (ParseUInt16(in.substr(colon + 1), &n)) {
in = in.substr(0, colon);
portOut = n;
}
@@ -335,6 +335,18 @@ bool ParseUInt8(const std::string& str, uint8_t *out)
return true;
}
+bool ParseUInt16(const std::string& str, uint16_t* out)
+{
+ uint32_t u32;
+ if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint16_t>::max()) {
+ return false;
+ }
+ if (out != nullptr) {
+ *out = static_cast<uint16_t>(u32);
+ }
+ return true;
+}
+
bool ParseUInt32(const std::string& str, uint32_t *out)
{
if (!ParsePrechecks(str))