From 7fd78a87d12e04c4242b1ccafdafbcf6030e3760 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 31 Mar 2020 13:45:05 -0700 Subject: [PATCH] Use static_assert more. --- node/Fingerprint.hpp | 2 ++ node/InetAddress.hpp | 5 +++++ node/Protocol.hpp | 5 +++++ node/Tests.cpp | 31 ++++++++++--------------------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/node/Fingerprint.hpp b/node/Fingerprint.hpp index e3b0e582c..6a7c70ae2 100644 --- a/node/Fingerprint.hpp +++ b/node/Fingerprint.hpp @@ -99,6 +99,8 @@ private: ZT_Fingerprint _fp; }; +static_assert(sizeof(Fingerprint) == sizeof(ZT_Fingerprint),"Fingerprint should be the same size as the underlying C ZT_Fingerprint"); + } // namespace ZeroTier #endif diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp index 9412f07dc..17ed9cabb 100644 --- a/node/InetAddress.hpp +++ b/node/InetAddress.hpp @@ -485,6 +485,11 @@ private: sockaddr_storage _data; }; +static_assert(sizeof(sockaddr_storage) == sizeof(InetAddress),"InetAddress sizing incorrect"); +static_assert(sizeof(sockaddr_in) <= sizeof(InetAddress),"InetAddress sizing incorrect"); +static_assert(sizeof(sockaddr_in6) <= sizeof(InetAddress),"InetAddress sizing incorrect"); +static_assert(sizeof(sockaddr) <= sizeof(InetAddress),"InetAddress sizing incorrect"); + static ZT_INLINE InetAddress *asInetAddress(sockaddr_in *p) noexcept { return reinterpret_cast(p); } static ZT_INLINE InetAddress *asInetAddress(sockaddr_in6 *p) noexcept { return reinterpret_cast(p); } static ZT_INLINE InetAddress *asInetAddress(sockaddr *p) noexcept { return reinterpret_cast(p); } diff --git a/node/Protocol.hpp b/node/Protocol.hpp index 89e6621a1..3ed5ef801 100644 --- a/node/Protocol.hpp +++ b/node/Protocol.hpp @@ -1017,6 +1017,11 @@ ZT_PACKED_STRUCT(struct UNSUPPORTED_OPERATION__NETWORK_CONFIG_REQUEST /****************************************************************************/ +static_assert(sizeof(Protocol::Header) == ZT_PROTO_MIN_PACKET_LENGTH,"Protocol::Header struct packing error"); +static_assert(sizeof(Protocol::FragmentHeader) == ZT_PROTO_MIN_FRAGMENT_LENGTH,"Protocol::FragmentHeader struct packing error"); +static_assert(ZT_PROTO_MAX_PACKET_LENGTH < ZT_BUF_MEM_SIZE,"maximum packet length won't fit in Buf"); +static_assert(ZT_PROTO_PACKET_ENCRYPTED_SECTION_START == (ZT_PROTO_MIN_PACKET_LENGTH-1),"encrypted packet section must start right before protocol verb at one less than minimum packet size"); + /** * Convenience function to pull packet ID from a raw buffer * diff --git a/node/Tests.cpp b/node/Tests.cpp index f05cf335b..c50bd9962 100644 --- a/node/Tests.cpp +++ b/node/Tests.cpp @@ -48,7 +48,7 @@ #ifdef __UNIX_LIKE__ #include -#include +#include // NOLINT(modernize-deprecated-headers,hicpp-deprecated-headers) #include #endif @@ -68,7 +68,7 @@ static int64_t now() tmp.HighPart = ft.dwHighDateTime; return (int64_t)( ((tmp.QuadPart - 116444736000000000LL) / 10000L) + st.wMilliseconds ); #else - timeval tv; + timeval tv; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) gettimeofday(&tv,nullptr); return ( (1000LL * (int64_t)tv.tv_sec) + (int64_t)(tv.tv_usec / 1000) ); #endif @@ -232,8 +232,6 @@ public: long *cnt; }; -#define ZT_T_ASSERT(e) if (!(e)) { ZT_T_PRINTF("FAILED (simple assert: " #e ")" ZT_EOL_S); return "simple assert: " #e; } - extern "C" const char *ZTT_general() { try { @@ -266,21 +264,12 @@ extern "C" const char *ZTT_general() { ZT_T_PRINTF("[general] Checking structure sizes, alignment, and packing... "); - ZT_T_ASSERT(sizeof(StructPackingTestSample) == 146); - StructPackingTestSample packtest; + static_assert(sizeof(StructPackingTestSample) == 146,"StructPackingTestSample packed incorrectly"); + StructPackingTestSample packtest; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) if (((uintptr_t)&(packtest.woop) - (uintptr_t)&packtest) != 8) return "structure packing check failed: incorrect index of test field in example packed struct (compiler or environment problem)"; if (((uintptr_t)&(packtest.hoho[5]) - (uintptr_t)&packtest) != 57) return "structure packing check failed: incorrect index of test array indexed field in example packed struct (compiler or environment problem)"; - ZT_T_ASSERT(ZT_PROTO_MAX_PACKET_LENGTH < ZT_BUF_MEM_SIZE); - ZT_T_ASSERT(ZT_PROTO_PACKET_ENCRYPTED_SECTION_START < ZT_PROTO_MIN_PACKET_LENGTH); - ZT_T_ASSERT(sizeof(Protocol::Header) == ZT_PROTO_MIN_PACKET_LENGTH); - ZT_T_ASSERT(sizeof(Protocol::FragmentHeader) == ZT_PROTO_MIN_FRAGMENT_LENGTH); - ZT_T_ASSERT(sizeof(sockaddr_storage) == sizeof(InetAddress)); - ZT_T_ASSERT(sizeof(sockaddr_in) <= sizeof(InetAddress)); - ZT_T_ASSERT(sizeof(sockaddr_in6) <= sizeof(InetAddress)); - ZT_T_ASSERT(sizeof(sockaddr) <= sizeof(InetAddress)); - ZT_T_ASSERT(sizeof(Fingerprint) == sizeof(ZT_Fingerprint)); ZT_T_PRINTF("OK" ZT_EOL_S); } @@ -477,7 +466,7 @@ extern "C" const char *ZTT_general() return "Map::get() failed"; } } - for(Map::iterator i(tm.begin());i!=tm.end();) { + for(Map::iterator i(tm.begin());i!=tm.end();) { // NOLINT(hicpp-use-auto,modernize-use-auto) if ((i->first & 1U) == 0) tm.erase(i++); else ++i; @@ -549,7 +538,7 @@ extern "C" const char *ZTT_general() FCV ref; int frags = 1 + (int)(Utils::random() % 16); - int skip = ((k & 3U) == 1) ? -1 : (int)(Utils::random() % frags); + int skip = ((k & 3) == 1) ? -1 : (int)(Utils::random() % frags); bool complete = false; message.resize(frags); ref.resize(frags); @@ -1069,7 +1058,7 @@ extern "C" const char *ZTT_benchmarkCrypto() ZT_T_PRINTF("[crypto] Benchmarking Curve25519 ECDH... "); int64_t start = now(); for(int i=0;i<150;++i) { - for (int t=0;t