From 2bc83a4b5183f5167c4dd84029dfa4819cd3b3f1 Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 7 Dec 2021 17:09:35 +0000 Subject: [PATCH] cleanup: Use `static_assert` instead of preprocessor `#error`. `ccompat.h` ensures that if it doesn't exist, then `static_assert` has no effect. --- toxcore/Messenger.c | 5 ++++ toxcore/Messenger.h | 4 ---- toxcore/network.c | 13 +++++------ toxcore/onion_announce.c | 5 ++++ toxcore/onion_announce.h | 4 ---- toxcore/tox.c | 49 +++++++++++++++------------------------- 6 files changed, 34 insertions(+), 46 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 42ddd357..a1974628 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -24,6 +24,11 @@ #include "state.h" #include "util.h" +//!TOKSTYLE- +static_assert(MAX_CONCURRENT_FILE_PIPES <= UINT8_MAX + 1, + "uint8_t cannot represent all file transfer numbers"); +//!TOKSTYLE+ + static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data, uint32_t length, uint8_t congestion_control); static void m_register_default_plugins(Messenger *m); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 673fd425..73b13690 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -24,10 +24,6 @@ /* This cannot be bigger than 256 */ #define MAX_CONCURRENT_FILE_PIPES 256 -#if !defined(__SPLINT__) && MAX_CONCURRENT_FILE_PIPES > UINT8_MAX + 1 -#error "uint8_t cannot represent all file transfer numbers" -#endif - #define FRIEND_ADDRESS_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) diff --git a/toxcore/network.c b/toxcore/network.c index 3efa3283..f2bbbf09 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -194,13 +194,12 @@ static int inet_pton6(const char *addrString, struct in6_addr *addrbuf) #endif #endif -#if TOX_INET6_ADDRSTRLEN < INET6_ADDRSTRLEN -#error "TOX_INET6_ADDRSTRLEN should be greater or equal to INET6_ADDRSTRLEN (#INET6_ADDRSTRLEN)" -#endif - -#if TOX_INET_ADDRSTRLEN < INET_ADDRSTRLEN -#error "TOX_INET_ADDRSTRLEN should be greater or equal to INET_ADDRSTRLEN (#INET_ADDRSTRLEN)" -#endif +//!TOKSTYLE- +static_assert(TOX_INET6_ADDRSTRLEN >= INET6_ADDRSTRLEN, + "TOX_INET6_ADDRSTRLEN should be greater or equal to INET6_ADDRSTRLEN (#INET6_ADDRSTRLEN)"); +static_assert(TOX_INET_ADDRSTRLEN >= INET_ADDRSTRLEN, + "TOX_INET_ADDRSTRLEN should be greater or equal to INET_ADDRSTRLEN (#INET_ADDRSTRLEN)"); +//!TOKSTYLE+ static int make_proto(int proto) { diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 6d981313..1cfe35cb 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -26,6 +26,11 @@ #define DATA_REQUEST_MIN_SIZE ONION_DATA_REQUEST_MIN_SIZE #define DATA_REQUEST_MIN_SIZE_RECV (DATA_REQUEST_MIN_SIZE + ONION_RETURN_3) +//!TOKSTYLE- +static_assert(ONION_PING_ID_SIZE == CRYPTO_PUBLIC_KEY_SIZE, + "announce response packets assume that ONION_PING_ID_SIZE is equal to CRYPTO_PUBLIC_KEY_SIZE"); +//!TOKSTYLE+ + typedef struct Onion_Announce_Entry { uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; IP_Port ret_ip_port; diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h index c34e948c..4f44614a 100644 --- a/toxcore/onion_announce.h +++ b/toxcore/onion_announce.h @@ -25,10 +25,6 @@ #define ONION_DATA_RESPONSE_MIN_SIZE (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE) -#if ONION_PING_ID_SIZE != CRYPTO_PUBLIC_KEY_SIZE -#error "announce response packets assume that ONION_PING_ID_SIZE is equal to CRYPTO_PUBLIC_KEY_SIZE" -#endif - #define ONION_DATA_REQUEST_MIN_SIZE (1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE) #define MAX_DATA_REQUEST_SIZE (ONION_MAX_DATA_SIZE - ONION_DATA_REQUEST_MIN_SIZE) diff --git a/toxcore/tox.c b/toxcore/tox.c index 64daab01..df07931b 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -32,37 +32,24 @@ #define SET_ERROR_PARAMETER(param, x) do { if (param) { *param = x; } } while (0) -#if TOX_HASH_LENGTH != CRYPTO_SHA256_SIZE -#error "TOX_HASH_LENGTH is assumed to be equal to CRYPTO_SHA256_SIZE" -#endif - -#if FILE_ID_LENGTH != CRYPTO_SYMMETRIC_KEY_SIZE -#error "FILE_ID_LENGTH is assumed to be equal to CRYPTO_SYMMETRIC_KEY_SIZE" -#endif - -#if TOX_FILE_ID_LENGTH != CRYPTO_SYMMETRIC_KEY_SIZE -#error "TOX_FILE_ID_LENGTH is assumed to be equal to CRYPTO_SYMMETRIC_KEY_SIZE" -#endif - -#if TOX_FILE_ID_LENGTH != TOX_HASH_LENGTH -#error "TOX_FILE_ID_LENGTH is assumed to be equal to TOX_HASH_LENGTH" -#endif - -#if TOX_PUBLIC_KEY_SIZE != CRYPTO_PUBLIC_KEY_SIZE -#error "TOX_PUBLIC_KEY_SIZE is assumed to be equal to CRYPTO_PUBLIC_KEY_SIZE" -#endif - -#if TOX_SECRET_KEY_SIZE != CRYPTO_SECRET_KEY_SIZE -#error "TOX_SECRET_KEY_SIZE is assumed to be equal to CRYPTO_SECRET_KEY_SIZE" -#endif - -#if TOX_MAX_NAME_LENGTH != MAX_NAME_LENGTH -#error "TOX_MAX_NAME_LENGTH is assumed to be equal to MAX_NAME_LENGTH" -#endif - -#if TOX_MAX_STATUS_MESSAGE_LENGTH != MAX_STATUSMESSAGE_LENGTH -#error "TOX_MAX_STATUS_MESSAGE_LENGTH is assumed to be equal to MAX_STATUSMESSAGE_LENGTH" -#endif +//!TOKSTYLE- +static_assert(TOX_HASH_LENGTH == CRYPTO_SHA256_SIZE, + "TOX_HASH_LENGTH is assumed to be equal to CRYPTO_SHA256_SIZE"); +static_assert(FILE_ID_LENGTH == CRYPTO_SYMMETRIC_KEY_SIZE, + "FILE_ID_LENGTH is assumed to be equal to CRYPTO_SYMMETRIC_KEY_SIZE"); +static_assert(TOX_FILE_ID_LENGTH == CRYPTO_SYMMETRIC_KEY_SIZE, + "TOX_FILE_ID_LENGTH is assumed to be equal to CRYPTO_SYMMETRIC_KEY_SIZE"); +static_assert(TOX_FILE_ID_LENGTH == TOX_HASH_LENGTH, + "TOX_FILE_ID_LENGTH is assumed to be equal to TOX_HASH_LENGTH"); +static_assert(TOX_PUBLIC_KEY_SIZE == CRYPTO_PUBLIC_KEY_SIZE, + "TOX_PUBLIC_KEY_SIZE is assumed to be equal to CRYPTO_PUBLIC_KEY_SIZE"); +static_assert(TOX_SECRET_KEY_SIZE == CRYPTO_SECRET_KEY_SIZE, + "TOX_SECRET_KEY_SIZE is assumed to be equal to CRYPTO_SECRET_KEY_SIZE"); +static_assert(TOX_MAX_NAME_LENGTH == MAX_NAME_LENGTH, + "TOX_MAX_NAME_LENGTH is assumed to be equal to MAX_NAME_LENGTH"); +static_assert(TOX_MAX_STATUS_MESSAGE_LENGTH == MAX_STATUSMESSAGE_LENGTH, + "TOX_MAX_STATUS_MESSAGE_LENGTH is assumed to be equal to MAX_STATUSMESSAGE_LENGTH"); +//!TOKSTYLE+ struct Tox { // XXX: Messenger *must* be the first member, because toxav casts its