From f5f41e534801bbee1c83b7ae069c39f45440d4b6 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Thu, 20 Oct 2022 15:20:51 +0530 Subject: [PATCH] merge bitcoin#19314: Use uint16_t instead of unsigned short --- src/addrdb.cpp | 4 +++- src/net.cpp | 5 +++-- src/net.h | 4 ++-- src/netaddress.cpp | 10 +++++----- src/netaddress.h | 10 +++++----- src/netbase.cpp | 4 ++-- src/qt/optionsmodel.h | 4 +++- src/serialize.h | 6 +++--- src/test/net_tests.cpp | 13 ++++++++----- 9 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 51aa410855..e7f98c9538 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -14,6 +14,8 @@ #include #include +#include + namespace { template @@ -36,7 +38,7 @@ template bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data) { // Generate random temporary filename - unsigned short randv = 0; + uint16_t randv = 0; GetRandBytes((unsigned char*)&randv, sizeof(randv)); std::string tmpfn = strprintf("%s.%04x", prefix, randv); diff --git a/src/net.cpp b/src/net.cpp index 99cfaa9ec1..98cb0b3157 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -53,6 +53,7 @@ #include #endif +#include #include #include @@ -134,9 +135,9 @@ void CConnman::AddOneShot(const std::string& strDest) vOneShots.push_back(strDest); } -unsigned short GetListenPort() +uint16_t GetListenPort() { - return (unsigned short)(gArgs.GetArg("-port", Params().GetDefaultPort())); + return (uint16_t)(gArgs.GetArg("-port", Params().GetDefaultPort())); } // find 'best' local address for a particular peer diff --git a/src/net.h b/src/net.h index dce09d7bc4..a7e7074ce2 100644 --- a/src/net.h +++ b/src/net.h @@ -29,8 +29,8 @@ #include #include +#include #include -#include #include #include #include @@ -683,7 +683,7 @@ private: friend struct ConnmanTestMsg; }; void Discover(); -unsigned short GetListenPort(); +uint16_t GetListenPort(); struct CombinerAll { diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 701f0286ad..bb4ba3095c 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -836,15 +836,15 @@ CService::CService() : port(0) { } -CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn) +CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn) { } -CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNetAddr(ipv4Addr), port(portIn) +CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn) { } -CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn) +CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn) { } @@ -872,7 +872,7 @@ bool CService::SetSockAddr(const struct sockaddr *paddr) } } -unsigned short CService::GetPort() const +uint16_t CService::GetPort() const { return port; } @@ -944,7 +944,7 @@ std::string CService::ToString(bool fUseGetnameinfo) const return ToStringIPPort(fUseGetnameinfo); } -void CService::SetPort(unsigned short portIn) +void CService::SetPort(uint16_t portIn) { port = portIn; } diff --git a/src/netaddress.h b/src/netaddress.h index c6eba8d5c9..d25d63f656 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -505,11 +505,11 @@ class CService : public CNetAddr public: CService(); - CService(const CNetAddr& ip, unsigned short port); - CService(const struct in_addr& ipv4Addr, unsigned short port); + CService(const CNetAddr& ip, uint16_t port); + CService(const struct in_addr& ipv4Addr, uint16_t port); explicit CService(const struct sockaddr_in& addr); - void SetPort(unsigned short portIn); - unsigned short GetPort() const; + void SetPort(uint16_t portIn); + uint16_t GetPort() const; bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const; bool SetSockAddr(const struct sockaddr* paddr); friend bool operator==(const CService& a, const CService& b); @@ -520,7 +520,7 @@ class CService : public CNetAddr std::string ToStringPort() const; std::string ToStringIPPort(bool fUseGetnameinfo = true) const; - CService(const struct in6_addr& ipv6Addr, unsigned short port); + CService(const struct in6_addr& ipv6Addr, uint16_t port); explicit CService(const struct sockaddr_in6& addr); SERIALIZE_METHODS(CService, obj) diff --git a/src/netbase.cpp b/src/netbase.cpp index 73c15643ee..69ebc88239 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -799,11 +799,11 @@ bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int ProxyCredentials random_auth; static std::atomic_int counter(0); random_auth.username = random_auth.password = strprintf("%i", counter++); - if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket)) { + if (!Socks5(strDest, (uint16_t)port, &random_auth, hSocket)) { return false; } } else { - if (!Socks5(strDest, (unsigned short)port, 0, hSocket)) { + if (!Socks5(strDest, (uint16_t)port, 0, hSocket)) { return false; } } diff --git a/src/qt/optionsmodel.h b/src/qt/optionsmodel.h index f542a6f955..5d105106d0 100644 --- a/src/qt/optionsmodel.h +++ b/src/qt/optionsmodel.h @@ -7,6 +7,8 @@ #include +#include + #include namespace interfaces { @@ -14,7 +16,7 @@ class Node; } extern const char *DEFAULT_GUI_PROXY_HOST; -static constexpr unsigned short DEFAULT_GUI_PROXY_PORT = 9050; +static constexpr uint16_t DEFAULT_GUI_PROXY_PORT = 9050; /** Interface from Qt to configuration data structure for Bitcoin client. To Qt, the options are presented as a list with the different options diff --git a/src/serialize.h b/src/serialize.h index db87f77694..17a569d2b2 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -17,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -281,7 +281,7 @@ template size_t GetSerializeSize(const T& t, int nVersion = 0); inline unsigned int GetSizeOfCompactSize(uint64_t nSize) { if (nSize < 253) return sizeof(unsigned char); - else if (nSize <= std::numeric_limits::max()) return sizeof(unsigned char) + sizeof(unsigned short); + else if (nSize <= std::numeric_limits::max()) return sizeof(unsigned char) + sizeof(uint16_t); else if (nSize <= std::numeric_limits::max()) return sizeof(unsigned char) + sizeof(unsigned int); else return sizeof(unsigned char) + sizeof(uint64_t); } @@ -295,7 +295,7 @@ void WriteCompactSize(Stream& os, uint64_t nSize) { ser_writedata8(os, nSize); } - else if (nSize <= std::numeric_limits::max()) + else if (nSize <= std::numeric_limits::max()) { ser_writedata8(os, 253); ser_writedata16(os, nSize); diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 6c07a540e6..e5dc8abbb1 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -2,12 +2,11 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include + #include #include #include -#include -#include -#include #include #include #include @@ -18,8 +17,12 @@ #include #include +#include #include #include +#include + +#include class CAddrManSerializationMock : public CAddrMan { @@ -84,10 +87,10 @@ BOOST_FIXTURE_TEST_SUITE(net_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(cnode_listen_port) { // test default - unsigned short port = GetListenPort(); + uint16_t port = GetListenPort(); BOOST_CHECK(port == Params().GetDefaultPort()); // test set port - unsigned short altPort = 12345; + uint16_t altPort = 12345; BOOST_CHECK(gArgs.SoftSetArg("-port", std::to_string(altPort))); port = GetListenPort(); BOOST_CHECK(port == altPort);