Skip to content

Commit

Permalink
cleanup: Fix a few more clang-tidy warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Sep 2, 2023
1 parent 4d3c97f commit b6c570a
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 108 deletions.
3 changes: 3 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ CheckOptions:
value: Camel_Snake_Case
- key: readability-identifier-naming.VariableCase
value: lower_case

- key: llvmlibc-restrict-system-libc-headers.Includes
value: "arpa/inet.h,assert.h,ctype.h,errno.h,fcntl.h,getopt.h,libconfig.h,linux/netdevice.h,math.h,netdb.h,netinet/in.h,opus.h,pthread.h,signal.h,sodium/crypto_scalarmult_curve25519.h,sodium.h,sodium/randombytes.h,stdio.h,stdlib.h,string.h,sys/ioctl.h,syslog.h,sys/resource.h,sys/socket.h,sys/stat.h,sys/time.h,sys/types.h,time.h,unistd.h,vpx/vp8cx.h,vpx/vp8dx.h,vpx/vpx_decoder.h,vpx/vpx_encoder.h,vpx/vpx_image.h"
8 changes: 6 additions & 2 deletions auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,12 @@ static void test_eventual_audio(AutoTox *autotoxes, const bool *disabled, uint64
uint64_t start = autotoxes[0].clock;

while (autotoxes[0].clock < start + timeout) {
if (test_audio(autotoxes, disabled, true)
&& test_audio(autotoxes, disabled, true)) {
if (!test_audio(autotoxes, disabled, true)) {
continue;
}

// It needs to succeed twice in a row for the test to pass.
if (test_audio(autotoxes, disabled, true)) {
printf("audio test successful after %d seconds\n", (int)((autotoxes[0].clock - start) / 1000));
return;
}
Expand Down
29 changes: 11 additions & 18 deletions auto_tests/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

static void rand_bytes(const Random *rng, uint8_t *b, size_t blen)
{
size_t i;

for (i = 0; i < blen; i++) {
for (size_t i = 0; i < blen; i++) {
b[i] = random_u08(rng);
}
}
Expand Down Expand Up @@ -84,19 +82,18 @@ static void test_known(void)
{
uint8_t c[147];
uint8_t m[131];
uint16_t clen, mlen;

ck_assert_msg(sizeof(c) == sizeof(m) + CRYPTO_MAC_SIZE * sizeof(uint8_t),
"cyphertext should be CRYPTO_MAC_SIZE bytes longer than plaintext");
ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");

clen = encrypt_data(bobpk, alicesk, test_nonce, test_m, sizeof(test_m) / sizeof(uint8_t), c);
const uint16_t clen = encrypt_data(bobpk, alicesk, test_nonce, test_m, sizeof(test_m) / sizeof(uint8_t), c);

ck_assert_msg(memcmp(test_c, c, sizeof(c)) == 0, "cyphertext doesn't match test vector");
ck_assert_msg(clen == sizeof(c) / sizeof(uint8_t), "wrong ciphertext length");

mlen = decrypt_data(bobpk, alicesk, test_nonce, test_c, sizeof(test_c) / sizeof(uint8_t), m);
const uint16_t mlen = decrypt_data(bobpk, alicesk, test_nonce, test_c, sizeof(test_c) / sizeof(uint8_t), m);

ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector");
ck_assert_msg(mlen == sizeof(m) / sizeof(uint8_t), "wrong plaintext length");
Expand All @@ -107,7 +104,6 @@ static void test_fast_known(void)
uint8_t k[CRYPTO_SHARED_KEY_SIZE];
uint8_t c[147];
uint8_t m[131];
uint16_t clen, mlen;

encrypt_precompute(bobpk, alicesk, k);

Expand All @@ -116,12 +112,12 @@ static void test_fast_known(void)
ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");

clen = encrypt_data_symmetric(k, test_nonce, test_m, sizeof(test_m) / sizeof(uint8_t), c);
const uint16_t clen = encrypt_data_symmetric(k, test_nonce, test_m, sizeof(test_m) / sizeof(uint8_t), c);

ck_assert_msg(memcmp(test_c, c, sizeof(c)) == 0, "cyphertext doesn't match test vector");
ck_assert_msg(clen == sizeof(c) / sizeof(uint8_t), "wrong ciphertext length");

mlen = decrypt_data_symmetric(k, test_nonce, test_c, sizeof(test_c) / sizeof(uint8_t), m);
const uint16_t mlen = decrypt_data_symmetric(k, test_nonce, test_c, sizeof(test_c) / sizeof(uint8_t), m);

ck_assert_msg(memcmp(test_m, m, sizeof(m)) == 0, "decrypted text doesn't match test vector");
ck_assert_msg(mlen == sizeof(m) / sizeof(uint8_t), "wrong plaintext length");
Expand Down Expand Up @@ -275,10 +271,10 @@ static void test_large_data_symmetric(void)

static void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num)
{
uint32_t num1, num2;
uint32_t num1 = 0;
memcpy(&num1, nonce + (CRYPTO_NONCE_SIZE - sizeof(num1)), sizeof(num1));
num1 = net_ntohl(num1);
num2 = num + num1;
uint32_t num2 = num + num1;

if (num2 < num1) {
for (uint16_t i = CRYPTO_NONCE_SIZE - sizeof(num1); i != 0; --i) {
Expand All @@ -299,25 +295,23 @@ static void test_increment_nonce(void)
const Random *rng = system_random();
ck_assert(rng != nullptr);

uint32_t i;

uint8_t n[CRYPTO_NONCE_SIZE];

for (i = 0; i < CRYPTO_NONCE_SIZE; ++i) {
for (uint32_t i = 0; i < CRYPTO_NONCE_SIZE; ++i) {
n[i] = random_u08(rng);
}

uint8_t n1[CRYPTO_NONCE_SIZE];

memcpy(n1, n, CRYPTO_NONCE_SIZE);

for (i = 0; i < (1 << 18); ++i) {
for (uint32_t i = 0; i < (1 << 18); ++i) {
increment_nonce_number_cmp(n, 1);
increment_nonce(n1);
ck_assert_msg(memcmp(n, n1, CRYPTO_NONCE_SIZE) == 0, "Bad increment_nonce function");
}

for (i = 0; i < (1 << 18); ++i) {
for (uint32_t i = 0; i < (1 << 18); ++i) {
const uint32_t r = random_u32(rng);
increment_nonce_number_cmp(n, r);
increment_nonce_number(n1, r);
Expand All @@ -331,9 +325,8 @@ static void test_memzero(void)
memcpy(src, test_c, sizeof(test_c));

crypto_memzero(src, sizeof(src));
size_t i;

for (i = 0; i < sizeof(src); i++) {
for (size_t i = 0; i < sizeof(src); i++) {
ck_assert_msg(src[i] == 0, "Memory is not zeroed");
}
}
Expand Down
17 changes: 8 additions & 9 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,23 +574,22 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub

static void test_announce(void)
{
uint32_t i, j;
uint32_t index[NUM_ONIONS];
Onions *onions[NUM_ONIONS];
const Random *rng = system_random();
ck_assert(rng != nullptr);
const Memory *mem = system_memory();
ck_assert(mem != nullptr);

for (i = 0; i < NUM_ONIONS; ++i) {
for (uint32_t i = 0; i < NUM_ONIONS; ++i) {
index[i] = i + 1;
onions[i] = new_onions(mem, rng, i + 36655, &index[i]);
ck_assert_msg(onions[i] != nullptr, "Failed to create onions. %u", i);
}

IP ip = get_loopback();

for (i = 3; i < NUM_ONIONS; ++i) {
for (uint32_t i = 3; i < NUM_ONIONS; ++i) {
IP_Port ip_port = {ip, net_port(onions[i - 1]->onion->net)};
dht_bootstrap(onions[i]->onion->dht, &ip_port, dht_get_self_public_key(onions[i - 1]->onion->dht));
IP_Port ip_port1 = {ip, net_port(onions[i - 2]->onion->net)};
Expand All @@ -604,7 +603,7 @@ static void test_announce(void)
do {
connected = 0;

for (i = 0; i < NUM_ONIONS; ++i) {
for (uint32_t i = 0; i < NUM_ONIONS; ++i) {
do_onions(onions[i]);
connected += dht_isconnected(onions[i]->onion->dht);
}
Expand All @@ -614,8 +613,8 @@ static void test_announce(void)

printf("connected\n");

for (i = 0; i < 25 * 2; ++i) {
for (j = 0; j < NUM_ONIONS; ++j) {
for (uint32_t i = 0; i < 25 * 2; ++i) {
for (uint32_t j = 0; j < NUM_ONIONS; ++j) {
do_onions(onions[j]);
}

Expand All @@ -637,7 +636,7 @@ static void test_announce(void)
IP_Port ip_port;

do {
for (i = 0; i < NUM_ONIONS; ++i) {
for (uint32_t i = 0; i < NUM_ONIONS; ++i) {
do_onions(onions[i]);
}

Expand All @@ -647,7 +646,7 @@ static void test_announce(void)
printf("Waiting for ips\n");

do {
for (i = 0; i < NUM_ONIONS; ++i) {
for (uint32_t i = 0; i < NUM_ONIONS; ++i) {
do_onions(onions[i]);
}

Expand All @@ -657,7 +656,7 @@ static void test_announce(void)
onion_getfriendip(onions[NUM_LAST]->onion_c, frnum, &ip_port);
ck_assert_msg(ip_port.port == net_port(onions[NUM_FIRST]->onion->net), "Port in returned ip not correct.");

for (i = 0; i < NUM_ONIONS; ++i) {
for (uint32_t i = 0; i < NUM_ONIONS; ++i) {
kill_onions(mem, onions[i]);
}
}
Expand Down
32 changes: 15 additions & 17 deletions auto_tests/tox_many_tcp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ static void test_many_clients_tcp(void)
long long unsigned int cur_time = time(nullptr);
Tox *toxes[NUM_TOXES_TCP];
uint32_t index[NUM_TOXES_TCP];
uint32_t i, j;
uint32_t to_comp = 974536;

for (i = 0; i < NUM_TOXES_TCP; ++i) {
for (uint32_t i = 0; i < NUM_TOXES_TCP; ++i) {
struct Tox_Options *opts = tox_options_new(nullptr);

if (i == 0) {
Expand All @@ -72,7 +71,7 @@ static void test_many_clients_tcp(void)
tox_callback_friend_request(toxes[i], accept_friend_request);
uint8_t dpk[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(toxes[0], dpk);
Tox_Err_Bootstrap error = TOX_ERR_BOOTSTRAP_OK;
Tox_Err_Bootstrap error;
ck_assert_msg(tox_add_tcp_relay(toxes[i], TOX_LOCALHOST, tcp_relay_port, dpk, &error), "add relay error, %u, %d", i,
error);
uint16_t first_port = tox_self_get_udp_port(toxes[0], nullptr);
Expand All @@ -88,12 +87,12 @@ static void test_many_clients_tcp(void)

uint8_t address[TOX_ADDRESS_SIZE];

for (i = 0; i < NUM_FRIENDS; ++i) {
for (uint32_t i = 0; i < NUM_FRIENDS; ++i) {
loop_top:
pairs[i].tox1 = random_u32(rng) % NUM_TOXES_TCP;
pairs[i].tox2 = (pairs[i].tox1 + random_u32(rng) % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP;

for (j = 0; j < i; ++j) {
for (uint32_t j = 0; j < i; ++j) {
if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) {
goto loop_top;
}
Expand All @@ -114,8 +113,8 @@ static void test_many_clients_tcp(void)
while (true) {
uint16_t counter = 0;

for (i = 0; i < NUM_TOXES_TCP; ++i) {
for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) {
for (uint32_t i = 0; i < NUM_TOXES_TCP; ++i) {
for (uint32_t j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) {
if (tox_friend_get_connection_status(toxes[i], j, nullptr) == TOX_CONNECTION_TCP) {
++counter;
}
Expand All @@ -126,14 +125,14 @@ static void test_many_clients_tcp(void)
break;
}

for (i = 0; i < NUM_TOXES_TCP; ++i) {
for (uint32_t i = 0; i < NUM_TOXES_TCP; ++i) {
tox_iterate(toxes[i], &to_comp);
}

c_sleep(50);
}

for (i = 0; i < NUM_TOXES_TCP; ++i) {
for (uint32_t i = 0; i < NUM_TOXES_TCP; ++i) {
tox_kill(toxes[i]);
}

Expand All @@ -149,10 +148,9 @@ static void test_many_clients_tcp_b(void)
long long unsigned int cur_time = time(nullptr);
Tox *toxes[NUM_TOXES_TCP];
uint32_t index[NUM_TOXES_TCP];
uint32_t i, j;
uint32_t to_comp = 974536;

for (i = 0; i < NUM_TOXES_TCP; ++i) {
for (uint32_t i = 0; i < NUM_TOXES_TCP; ++i) {
struct Tox_Options *opts = tox_options_new(nullptr);

if (i < NUM_TCP_RELAYS) {
Expand Down Expand Up @@ -183,12 +181,12 @@ static void test_many_clients_tcp_b(void)

uint8_t address[TOX_ADDRESS_SIZE];

for (i = 0; i < NUM_FRIENDS; ++i) {
for (uint32_t i = 0; i < NUM_FRIENDS; ++i) {
loop_top:
pairs[i].tox1 = random_u32(rng) % NUM_TOXES_TCP;
pairs[i].tox2 = (pairs[i].tox1 + random_u32(rng) % (NUM_TOXES_TCP - 1) + 1) % NUM_TOXES_TCP;

for (j = 0; j < i; ++j) {
for (uint32_t j = 0; j < i; ++j) {
if (pairs[j].tox2 == pairs[i].tox1 && pairs[j].tox1 == pairs[i].tox2) {
goto loop_top;
}
Expand All @@ -211,8 +209,8 @@ static void test_many_clients_tcp_b(void)
while (true) {
uint16_t counter = 0;

for (i = 0; i < NUM_TOXES_TCP; ++i) {
for (j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) {
for (uint32_t i = 0; i < NUM_TOXES_TCP; ++i) {
for (uint32_t j = 0; j < tox_self_get_friend_list_size(toxes[i]); ++j) {
if (tox_friend_get_connection_status(toxes[i], j, nullptr) == TOX_CONNECTION_TCP) {
++counter;
}
Expand All @@ -228,14 +226,14 @@ static void test_many_clients_tcp_b(void)
break;
}

for (i = 0; i < NUM_TOXES_TCP; ++i) {
for (uint32_t i = 0; i < NUM_TOXES_TCP; ++i) {
tox_iterate(toxes[i], &to_comp);
}

c_sleep(30);
}

for (i = 0; i < NUM_TOXES_TCP; ++i) {
for (uint32_t i = 0; i < NUM_TOXES_TCP; ++i) {
tox_kill(toxes[i]);
}

Expand Down
31 changes: 13 additions & 18 deletions other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,38 +82,33 @@ static void manage_keys(DHT *dht)
fclose(keys_file);
}

static void print_log(void *context, Logger_Level level, const char *file, int line,
const char *func, const char *message, void *userdata)
static const char *strlevel(Logger_Level level)
{
const char *strlevel;

switch (level) {
case LOGGER_LEVEL_TRACE:
strlevel = "TRACE";
break;
return "TRACE";

case LOGGER_LEVEL_DEBUG:
strlevel = "DEBUG";
break;
return "DEBUG";

case LOGGER_LEVEL_INFO:
strlevel = "INFO";
break;
return "INFO";

case LOGGER_LEVEL_WARNING:
strlevel = "WARNING";
break;
return "WARNING";

case LOGGER_LEVEL_ERROR:
strlevel = "ERROR";
break;
return "ERROR";

default:
strlevel = "<unknown>";
break;
return "<unknown>";
}
}

fprintf(stderr, "[%s] %s:%d(%s) %s\n", strlevel, file, line, func, message);
static void print_log(void *context, Logger_Level level, const char *file, int line,
const char *func, const char *message, void *userdata)
{
fprintf(stderr, "[%s] %s:%d(%s) %s\n", strlevel(level), file, line, func, message);
}

int main(int argc, char *argv[])
Expand All @@ -139,7 +134,7 @@ int main(int argc, char *argv[])

Logger *logger = logger_new();

if (MIN_LOGGER_LEVEL == LOGGER_LEVEL_TRACE || MIN_LOGGER_LEVEL == LOGGER_LEVEL_DEBUG) {
if (MIN_LOGGER_LEVEL <= LOGGER_LEVEL_DEBUG) {
logger_callback_log(logger, print_log, nullptr, nullptr);
}

Expand Down
Loading

0 comments on commit b6c570a

Please sign in to comment.