diff --git a/.circleci/config.yml b/.circleci/config.yml index 0d6bdc44..867e251a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -12,6 +12,7 @@ workflows: # Static analysis - clang-analyze - clang-tidy + - cpplint - infer - static-analysis @@ -116,3 +117,15 @@ jobs: other/analysis/run-clang-tidy || other/analysis/run-clang-tidy || other/analysis/run-clang-tidy + + cpplint: + working_directory: ~/work + docker: + - image: ubuntu + + steps: + - run: *apt_install + - run: apt-get install -y --no-install-recommends python3-pip + - checkout + - run: pip install cpplint + - run: other/analysis/run-cpplint diff --git a/.github/settings.yml b/.github/settings.yml index 8f2c7932..0a941c80 100644 --- a/.github/settings.yml +++ b/.github/settings.yml @@ -30,6 +30,7 @@ branches: - "ci/circleci: asan" - "ci/circleci: clang-analyze" - "ci/circleci: clang-tidy" + - "ci/circleci: cpplint" - "ci/circleci: infer" - "ci/circleci: msan" - "ci/circleci: static-analysis" diff --git a/other/analysis/run-cpplint b/other/analysis/run-cpplint new file mode 100755 index 00000000..4a86cb5f --- /dev/null +++ b/other/analysis/run-cpplint @@ -0,0 +1,11 @@ +#!/bin/sh + +FILTER="-whitespace" +FILTER="$FILTER,-build/include_subdir" +FILTER="$FILTER,-readability/casting" +FILTER="$FILTER,-runtime/arrays" +FILTER="$FILTER,-runtime/printf" +FILTER="$FILTER,-runtime/int" +FILTER="$FILTER,-build/header_guard" + +cpplint --filter="$FILTER" toxav/*.[ch] toxcore/*.[ch] toxencryptsave/*.[ch] diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c index bad172c1..b92b0b95 100644 --- a/other/bootstrap_daemon/src/config.c +++ b/other/bootstrap_daemon/src/config.c @@ -41,16 +41,14 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS}; - int i; - - for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { + for (int i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; ++i) { log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]); } // similar procedure to the one of reading config file below *tcp_relay_ports = (uint16_t *)malloc(DEFAULT_TCP_RELAY_PORTS_COUNT * sizeof(uint16_t)); - for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { + for (int i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; ++i) { (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i]; @@ -61,7 +59,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por continue; } - (*tcp_relay_port_count) ++; + ++*tcp_relay_port_count; } // the loop above skips invalid ports, so we adjust the allocated memory size @@ -90,9 +88,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por *tcp_relay_ports = (uint16_t *)malloc(config_port_count * sizeof(uint16_t)); - int i; - - for (i = 0; i < config_port_count; i ++) { + for (int i = 0; i < config_port_count; ++i) { config_setting_t *elem = config_setting_get_elem(ports_array, i); if (elem == nullptr) { @@ -115,7 +111,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por continue; } - (*tcp_relay_port_count) ++; + ++*tcp_relay_port_count; } // the loop above skips invalid ports, so we adjust the allocated memory size @@ -264,9 +260,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k log_write(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); } else { log_write(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); - int i; - for (i = 0; i < *tcp_relay_port_count; i ++) { + for (int i = 0; i < *tcp_relay_port_count; ++i) { log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); } } @@ -300,9 +295,8 @@ static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string) uint8_t *ret = (uint8_t *)malloc(len); const char *pos = hex_string; - size_t i; - for (i = 0; i < len; ++i, pos += 2) { + for (size_t i = 0; i < len; ++i, pos += 2) { unsigned int val; sscanf(pos, "%02x", &val); ret[i] = val; @@ -411,7 +405,7 @@ next: // though it's freed when the element is removed, so we free it right away in order to keep memory // consumption minimal config_setting_remove_elem(node_list, 0); - i++; + ++i; } config_destroy(&cfg); diff --git a/other/fun/cracker.c b/other/fun/cracker.c index 19569e8c..7c30d1b8 100644 --- a/other/fun/cracker.c +++ b/other/fun/cracker.c @@ -38,7 +38,7 @@ static void print_key(const uint8_t *client_id) { - for (uint32_t j = 0; j < 32; j++) { + for (uint32_t j = 0; j < 32; ++j) { printf("%02X", client_id[j]); } } @@ -80,17 +80,17 @@ static size_t match_hex_prefix(const uint8_t *key, const uint8_t *prefix, size_t uint8_t diff = 0; size_t i; - for (i = 0; i < prefix_len / 2; i++) { + for (i = 0; i < prefix_len / 2; ++i) { diff = key[i] ^ prefix[i]; // First check high nibble if ((diff & 0xF0) == 0) { - same++; + ++same; } // Then low nibble if (diff == 0) { - same++; + ++same; } else { break; } @@ -102,7 +102,7 @@ static size_t match_hex_prefix(const uint8_t *key, const uint8_t *prefix, size_t // First check high nibble if ((diff & 0xF0) == 0) { - same++; + ++same; } } @@ -114,7 +114,7 @@ static void cracker_core(uint64_t range_start, uint64_t range_end, uint64_t rang { #pragma omp parallel for firstprivate(priv_key_shadow) shared(longest_match, range_start, range_end, range_offs, hex_prefix, prefix_chars_len) schedule(static) default(none) - for (uint64_t batch = range_start; batch < range_end; batch++) { + for (uint64_t batch = range_start; batch < range_end; ++batch) { uint8_t *priv_key = (uint8_t *) priv_key_shadow; /* * We can't use the first and last bytes because they are masked in diff --git a/other/fun/minimal-save-generator.c b/other/fun/minimal-save-generator.c index b4833ea8..393bb7aa 100644 --- a/other/fun/minimal-save-generator.c +++ b/other/fun/minimal-save-generator.c @@ -34,7 +34,7 @@ int main(void) sodium_bin2hex(public_key_str, sizeof(public_key_str), public_key, sizeof(public_key)); sodium_bin2hex(secret_key_str, sizeof(secret_key_str), secret_key, sizeof(secret_key)); - for (size_t i = 0; i < sizeof(public_key_str); i ++) { + for (size_t i = 0; i < sizeof(public_key_str); ++i) { public_key_str[i] = toupper(public_key_str[i]); secret_key_str[i] = toupper(secret_key_str[i]); } @@ -45,14 +45,14 @@ int main(void) // calculate checksum for tox id printing unsigned char checksum[2] = {0}; - for (size_t i = 0; i < crypto_box_PUBLICKEYBYTES; i ++) { + for (size_t i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) { checksum[i % 2] ^= public_key[i]; } char checksum_str[sizeof(checksum) * 2 + 1]; sodium_bin2hex(checksum_str, sizeof(checksum_str), checksum, sizeof(checksum)); - for (size_t i = 0; i < sizeof(checksum_str); i ++) { + for (size_t i = 0; i < sizeof(checksum_str); ++i) { checksum_str[i] = toupper(checksum_str[i]); } diff --git a/other/fun/sign.c b/other/fun/sign.c index 56dcc8e1..f6dcc71e 100644 --- a/other/fun/sign.c +++ b/other/fun/sign.c @@ -55,15 +55,14 @@ int main(int argc, char *argv[]) if (argc == 2 && argv[1][0] == 'g') { crypto_sign_ed25519_keypair(pk, sk); printf("Public key:\n"); - int i; - for (i = 0; i < crypto_sign_ed25519_PUBLICKEYBYTES; i++) { + for (int i = 0; i < crypto_sign_ed25519_PUBLICKEYBYTES; ++i) { printf("%02X", pk[i]); } printf("\nSecret key:\n"); - for (i = 0; i < crypto_sign_ed25519_SECRETKEYBYTES; i++) { + for (int i = 0; i < crypto_sign_ed25519_SECRETKEYBYTES; ++i) { printf("%02X", sk[i]); } diff --git a/other/fun/strkey.c b/other/fun/strkey.c index 2857f031..0b0aa5c2 100644 --- a/other/fun/strkey.c +++ b/other/fun/strkey.c @@ -47,9 +47,7 @@ static void print_key(unsigned char *key) { - size_t i; - - for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++) { + for (size_t i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) { if (key[i] < 16) { fprintf(stdout, "0"); } @@ -115,12 +113,11 @@ int main(int argc, char *argv[]) do { #ifdef PRINT_TRIES_COUNT - tries ++; + ++tries; #endif crypto_box_keypair(public_key, secret_key); - int i; - for (i = 0; i <= crypto_box_PUBLICKEYBYTES - len; i ++) { + for (int i = 0; i <= crypto_box_PUBLICKEYBYTES - len; ++i) { if (memcmp(public_key + i, desired_bin, len) == 0) { found = 1; break; @@ -132,7 +129,7 @@ int main(int argc, char *argv[]) do { #ifdef PRINT_TRIES_COUNT - tries ++; + ++tries; #endif crypto_box_keypair(public_key, secret_key); } while (memcmp(p, desired_bin, len) != 0); diff --git a/toxav/audio.h b/toxav/audio.h index 2a5ebd69..4f335c29 100644 --- a/toxav/audio.h +++ b/toxav/audio.h @@ -5,15 +5,15 @@ #ifndef C_TOXCORE_TOXAV_AUDIO_H #define C_TOXCORE_TOXAV_AUDIO_H +#include +#include + #include "toxav.h" #include "../toxcore/logger.h" #include "../toxcore/util.h" #include "rtp.h" -#include -#include - #define AUDIO_JITTERBUFFER_COUNT 3 #define AUDIO_MAX_SAMPLE_RATE 48000 #define AUDIO_MAX_CHANNEL_COUNT 2 diff --git a/toxav/groupav.h b/toxav/groupav.h index 08e13630..e6f25f70 100644 --- a/toxav/groupav.h +++ b/toxav/groupav.h @@ -5,12 +5,12 @@ #ifndef C_TOXCORE_TOXAV_GROUPAV_H #define C_TOXCORE_TOXAV_GROUPAV_H -#include "../toxcore/group.h" -#include "../toxcore/tox.h" - // Audio encoding/decoding #include +#include "../toxcore/group.h" +#include "../toxcore/tox.h" + #define GROUP_AUDIO_PACKET_ID 192 // TODO(iphydf): Use this better typed one instead of the void-pointer one below. diff --git a/toxav/msi.c b/toxav/msi.c index b6d63f5d..b98cb4a9 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -4,14 +4,14 @@ */ #include "msi.h" -#include "../toxcore/logger.h" -#include "../toxcore/util.h" - #include #include #include #include +#include "../toxcore/logger.h" +#include "../toxcore/util.h" + #define MSI_MAXMSG_SIZE 256 /** diff --git a/toxav/msi.h b/toxav/msi.h index 5837fb61..a8ca3ad3 100644 --- a/toxav/msi.h +++ b/toxav/msi.h @@ -5,15 +5,15 @@ #ifndef C_TOXCORE_TOXAV_MSI_H #define C_TOXCORE_TOXAV_MSI_H +#include +#include + #include "audio.h" #include "video.h" #include "../toxcore/Messenger.h" #include "../toxcore/logger.h" -#include -#include - /** * Error codes. */ diff --git a/toxav/ring_buffer.c b/toxav/ring_buffer.c index 783c4c90..9e4063ee 100644 --- a/toxav/ring_buffer.c +++ b/toxav/ring_buffer.c @@ -5,10 +5,10 @@ */ #include "ring_buffer.h" -#include "../toxcore/ccompat.h" - #include +#include "../toxcore/ccompat.h" + struct RingBuffer { uint16_t size; /* Max size */ uint16_t start; diff --git a/toxav/rtp.h b/toxav/rtp.h index 91b48af9..1467bf5b 100644 --- a/toxav/rtp.h +++ b/toxav/rtp.h @@ -5,14 +5,14 @@ #ifndef C_TOXCORE_TOXAV_RTP_H #define C_TOXCORE_TOXAV_RTP_H +#include + #include "bwcontroller.h" #include "../toxcore/Messenger.h" #include "../toxcore/logger.h" #include "../toxcore/tox.h" -#include - #ifdef __cplusplus extern "C" { #endif diff --git a/toxav/toxav.c b/toxav/toxav.c index 2c409e8c..74354a47 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -4,6 +4,12 @@ */ #include "toxav.h" +#include +#include +#include +#include +#include + #include "msi.h" #include "rtp.h" @@ -12,12 +18,6 @@ #include "../toxcore/mono_time.h" #include "../toxcore/util.h" -#include -#include -#include -#include -#include - // TODO(zoff99): don't hardcode this, let the application choose it // VPX Info: Time to spend encoding, in microseconds (it's a *soft* deadline) #define WANTED_MAX_ENCODER_FPS 40 diff --git a/toxav/video.h b/toxav/video.h index 7b3fa598..1fd4c04e 100644 --- a/toxav/video.h +++ b/toxav/video.h @@ -5,13 +5,6 @@ #ifndef C_TOXCORE_TOXAV_VIDEO_H #define C_TOXCORE_TOXAV_VIDEO_H -#include "toxav.h" - -#include "../toxcore/logger.h" -#include "../toxcore/util.h" -#include "ring_buffer.h" -#include "rtp.h" - #include #include #include @@ -19,9 +12,15 @@ #include #include - #include +#include "toxav.h" + +#include "../toxcore/logger.h" +#include "../toxcore/util.h" +#include "ring_buffer.h" +#include "rtp.h" + typedef struct VCSession { /* encoding */ vpx_codec_ctx_t encoder[1]; diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 8767c294..dd55933b 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -8,6 +8,10 @@ */ #include "DHT.h" +#include +#include +#include + #include "LAN_discovery.h" #include "logger.h" #include "mono_time.h" @@ -16,10 +20,6 @@ #include "state.h" #include "util.h" -#include -#include -#include - /* The timeout after which a node is discarded completely. */ #define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL) diff --git a/toxcore/DHT.h b/toxcore/DHT.h index fbcb69dc..abcba969 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -9,14 +9,14 @@ #ifndef C_TOXCORE_TOXCORE_DHT_H #define C_TOXCORE_TOXCORE_DHT_H +#include + #include "crypto_core.h" #include "logger.h" #include "mono_time.h" #include "network.h" #include "ping_array.h" -#include - #ifdef __cplusplus extern "C" { #endif diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1ba0f3bd..5ea58e31 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -31,14 +31,16 @@ typedef struct Packets_Array { } Packets_Array; typedef enum Crypto_Conn_State { - CRYPTO_CONN_FREE = 0, /* the connection slot is free. This value is 0 so it is valid after - * `crypto_memzero(...)` of the parent struct - */ + /* the connection slot is free. This value is 0 so it is valid after + * `crypto_memzero(...)` of the parent struct + */ + CRYPTO_CONN_FREE = 0, CRYPTO_CONN_NO_CONNECTION, /* the connection is allocated, but not yet used */ CRYPTO_CONN_COOKIE_REQUESTING, /* we are sending cookie request packets */ CRYPTO_CONN_HANDSHAKE_SENT, /* we are sending handshake packets */ - CRYPTO_CONN_NOT_CONFIRMED, /* we are sending handshake packets. - * we have received one from the other, but no data */ + /* we are sending handshake packets. + * we have received one from the other, but no data */ + CRYPTO_CONN_NOT_CONFIRMED, CRYPTO_CONN_ESTABLISHED, /* the connection is established */ } Crypto_Conn_State; diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 3466c902..8ab88409 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -9,13 +9,13 @@ #ifndef C_TOXCORE_TOXCORE_NET_CRYPTO_H #define C_TOXCORE_TOXCORE_NET_CRYPTO_H +#include + #include "DHT.h" #include "LAN_discovery.h" #include "TCP_connection.h" #include "logger.h" -#include - /*** Crypto payloads. */ /*** Ranges. */ diff --git a/toxcore/network.c b/toxcore/network.c index c0e26c97..bfca9f1f 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -1615,7 +1615,7 @@ size_t net_socket_data_recv_buffer(Socket sock) #else #ifdef OS_WIN32 - unsigned long count = 0; + u_long count = 0; ioctlsocket(sock.socket, FIONREAD, &count); #else int count = 0; diff --git a/toxcore/network.h b/toxcore/network.h index aa6e6c5a..2bce8cd9 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -9,12 +9,12 @@ #ifndef C_TOXCORE_TOXCORE_NETWORK_H #define C_TOXCORE_TOXCORE_NETWORK_H -#include "logger.h" - #include // bool #include // size_t #include // uint*_t +#include "logger.h" + #ifdef __cplusplus extern "C" { #endif diff --git a/toxcore/ping.h b/toxcore/ping.h index f117db8f..34196aa2 100644 --- a/toxcore/ping.h +++ b/toxcore/ping.h @@ -10,11 +10,11 @@ #ifndef C_TOXCORE_TOXCORE_PING_H #define C_TOXCORE_TOXCORE_PING_H +#include + #include "DHT.h" #include "network.h" -#include - typedef struct Ping Ping; Ping *ping_new(const struct Mono_Time *mono_time, DHT *dht); diff --git a/toxcore/ping_array.h b/toxcore/ping_array.h index 3212272e..528a4646 100644 --- a/toxcore/ping_array.h +++ b/toxcore/ping_array.h @@ -9,11 +9,11 @@ #ifndef C_TOXCORE_TOXCORE_PING_ARRAY_H #define C_TOXCORE_TOXCORE_PING_ARRAY_H -#include "mono_time.h" - #include #include +#include "mono_time.h" + #ifdef __cplusplus extern "C" { #endif diff --git a/toxcore/tox_api.c b/toxcore/tox_api.c index 203cae1b..89524ae4 100644 --- a/toxcore/tox_api.c +++ b/toxcore/tox_api.c @@ -3,11 +3,11 @@ */ #include "tox.h" -#include "ccompat.h" - #include #include +#include "ccompat.h" + #define SET_ERROR_PARAMETER(param, x) \ do { \ if (param) { \ diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c index 09472f00..1edacae5 100644 --- a/toxencryptsave/toxencryptsave.c +++ b/toxencryptsave/toxencryptsave.c @@ -6,9 +6,6 @@ /* * Batch encryption functions. */ -#include "../toxcore/ccompat.h" -#include "../toxcore/crypto_core.h" -#include "defines.h" #include "toxencryptsave.h" #include @@ -16,7 +13,10 @@ #include #include -//!TOKSTYLE- +#include "../toxcore/ccompat.h" +#include "../toxcore/crypto_core.h" +#include "defines.h" + static_assert(TOX_PASS_SALT_LENGTH == crypto_pwhash_scryptsalsa208sha256_SALTBYTES, "TOX_PASS_SALT_LENGTH is assumed to be equal to crypto_pwhash_scryptsalsa208sha256_SALTBYTES"); static_assert(TOX_PASS_KEY_LENGTH == CRYPTO_SHARED_KEY_SIZE, @@ -24,7 +24,6 @@ static_assert(TOX_PASS_KEY_LENGTH == CRYPTO_SHARED_KEY_SIZE, static_assert(TOX_PASS_ENCRYPTION_EXTRA_LENGTH == (crypto_box_MACBYTES + crypto_box_NONCEBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES + TOX_ENC_SAVE_MAGIC_LENGTH), "TOX_PASS_ENCRYPTION_EXTRA_LENGTH is assumed to be equal to (crypto_box_MACBYTES + crypto_box_NONCEBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES + TOX_ENC_SAVE_MAGIC_LENGTH)"); -//!TOKSTYLE+ #define SET_ERROR_PARAMETER(param, x) \ do { \