From 6423c8175fad3163c10ffdb49e0df48e4e4931f1 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Mon, 1 Mar 2021 21:35:28 +0100 Subject: p2p, refactor: pass and use uint16_t CService::port as uint16_t --- src/util/strencodings.cpp | 3 ++- src/util/strencodings.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src/util') diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index f3d54a2ac9..e9d85c4c30 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -107,7 +107,8 @@ std::vector ParseHex(const std::string& str) return ParseHex(str.c_str()); } -void SplitHostPort(std::string in, int &portOut, std::string &hostOut) { +void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut) +{ size_t colon = in.find_last_of(':'); // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator bool fHaveColon = colon != in.npos; diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 98379e9138..a450b30ca2 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -65,7 +65,7 @@ std::string EncodeBase32(Span input, bool pad = true); */ std::string EncodeBase32(const std::string& str, bool pad = true); -void SplitHostPort(std::string in, int& portOut, std::string& hostOut); +void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut); int64_t atoi64(const std::string& str); int atoi(const std::string& str); -- cgit v1.2.3 From 2875a764f7d8b1503c7bdb2f262964f7a0cb5fc3 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Tue, 2 Mar 2021 22:37:50 +0100 Subject: util: add ParseUInt16(), use it in SplitHostPort() --- src/util/strencodings.cpp | 16 ++++++++++++++-- src/util/strencodings.h | 7 +++++++ 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'src/util') 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::max()) { + return false; + } + if (out != nullptr) { + *out = static_cast(u32); + } + return true; +} + bool ParseUInt32(const std::string& str, uint32_t *out) { if (!ParsePrechecks(str)) diff --git a/src/util/strencodings.h b/src/util/strencodings.h index a450b30ca2..26dc0a0ce3 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -115,6 +115,13 @@ constexpr inline bool IsSpace(char c) noexcept { */ [[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out); +/** + * Convert decimal string to unsigned 16-bit integer with strict parse error feedback. + * @returns true if the entire string could be parsed as valid integer, + * false if the entire string could not be parsed or if overflow or underflow occurred. + */ +[[nodiscard]] bool ParseUInt16(const std::string& str, uint16_t* out); + /** * Convert decimal string to unsigned 32-bit integer with strict parse error feedback. * @returns true if the entire string could be parsed as valid integer, -- cgit v1.2.3 From 6f09c0f6b57ac01a473c587a3e51e9d477866bb0 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 6 Mar 2021 21:17:50 +0100 Subject: util: add missing braces and apply clang format to SplitHostPort() --- src/util/strencodings.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/util') diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index 8ccb71280d..4734de3e0b 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -112,19 +112,20 @@ void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut) size_t colon = in.find_last_of(':'); // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator bool fHaveColon = colon != in.npos; - 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)) { + 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)) { uint16_t n; if (ParseUInt16(in.substr(colon + 1), &n)) { in = in.substr(0, colon); portOut = n; } } - if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']') - hostOut = in.substr(1, in.size()-2); - else + if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') { + hostOut = in.substr(1, in.size() - 2); + } else { hostOut = in; + } } std::string EncodeBase64(Span input) -- cgit v1.2.3