mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 20:13:44 +02:00
Use static_assert more.
This commit is contained in:
parent
b9f60fc3c2
commit
7fd78a87d1
4 changed files with 22 additions and 21 deletions
|
@ -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
|
||||
|
|
|
@ -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<InetAddress *>(p); }
|
||||
static ZT_INLINE InetAddress *asInetAddress(sockaddr_in6 *p) noexcept { return reinterpret_cast<InetAddress *>(p); }
|
||||
static ZT_INLINE InetAddress *asInetAddress(sockaddr *p) noexcept { return reinterpret_cast<InetAddress *>(p); }
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -48,7 +48,7 @@
|
|||
|
||||
#ifdef __UNIX_LIKE__
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/time.h> // NOLINT(modernize-deprecated-headers,hicpp-deprecated-headers)
|
||||
#include <sys/types.h>
|
||||
#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<uint64_t,uint64_t>::iterator i(tm.begin());i!=tm.end();) {
|
||||
for(Map<uint64_t,uint64_t>::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<Buf::Slice,16> 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<ZT_NUM_C25519_TEST_VECTORS;++t) {
|
||||
for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { // NOLINT(modernize-loop-convert)
|
||||
C25519::agree(C25519_TEST_VECTORS[t].priv1,C25519_TEST_VECTORS[t].pub2,key);
|
||||
foo = key[0]; // prevent optimization
|
||||
}
|
||||
|
@ -1084,7 +1073,7 @@ extern "C" const char *ZTT_benchmarkCrypto()
|
|||
ZT_T_PRINTF("[crypto] Benchmarking Ed25519 signature... ");
|
||||
int64_t start = now();
|
||||
for(int i=0;i<150;++i) {
|
||||
for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) {
|
||||
for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { // NOLINT(modernize-loop-convert)
|
||||
C25519::sign(C25519_TEST_VECTORS[t].priv1,C25519_TEST_VECTORS[t].pub1,sig,sizeof(sig),sig);
|
||||
foo = sig[0];
|
||||
}
|
||||
|
@ -1097,7 +1086,7 @@ extern "C" const char *ZTT_benchmarkCrypto()
|
|||
ZT_T_PRINTF("[crypto] Benchmarking Ed25519 signature verification... ");
|
||||
int64_t start = now();
|
||||
for(int i=0;i<15;++i) {
|
||||
for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) {
|
||||
for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { // NOLINT(modernize-loop-convert)
|
||||
if (C25519::verify(C25519_TEST_VECTORS[t].pub1,C25519_TEST_VECTORS[t].agreementSha512,64,C25519_TEST_VECTORS[t].agreementSignedBy1,96))
|
||||
++foo;
|
||||
}
|
||||
|
@ -1109,7 +1098,7 @@ extern "C" const char *ZTT_benchmarkCrypto()
|
|||
{
|
||||
uint8_t key[48];
|
||||
ZT_T_PRINTF("[crypto] Benchmarking ECC384 ECDH... ");
|
||||
volatile uint8_t *volatile pub = (volatile uint8_t *)ECC384_TV0_PUBLIC;
|
||||
volatile uint8_t *volatile pub = (volatile uint8_t *)ECC384_TV0_PUBLIC; // NOLINT(hicpp-use-auto,modernize-use-auto)
|
||||
int64_t start = now();
|
||||
for(int i=0;i<500;++i) {
|
||||
ECC384ECDH((const uint8_t *)pub,ECC384_TV0_PRIVATE,key);
|
||||
|
|
Loading…
Add table
Reference in a new issue