Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Minor cleanups of warnings given by cppcheck. #1743

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ START_TEST(test_basic)
uint8_t packet_resp[4096];
int recv_data_len = net_recv(sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
"Failed to receive server response to request. %u", recv_data_len);
"Failed to receive server response to request. %d", recv_data_len);
memcpy(&size, packet_resp, 2);
ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
"Wrong packet size for request response.");
Expand Down
64 changes: 29 additions & 35 deletions auto_tests/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,29 @@ END_TEST

START_TEST(test_endtoend)
{
unsigned char pk1[CRYPTO_PUBLIC_KEY_SIZE];
unsigned char sk1[CRYPTO_SECRET_KEY_SIZE];
unsigned char pk2[CRYPTO_PUBLIC_KEY_SIZE];
unsigned char sk2[CRYPTO_SECRET_KEY_SIZE];
unsigned char k1[CRYPTO_SHARED_KEY_SIZE];
unsigned char k2[CRYPTO_SHARED_KEY_SIZE];

unsigned char n[CRYPTO_NONCE_SIZE];

unsigned char m[500];
unsigned char c1[sizeof(m) + CRYPTO_MAC_SIZE];
unsigned char c2[sizeof(m) + CRYPTO_MAC_SIZE];
unsigned char c3[sizeof(m) + CRYPTO_MAC_SIZE];
unsigned char c4[sizeof(m) + CRYPTO_MAC_SIZE];
unsigned char m1[sizeof(m)];
unsigned char m2[sizeof(m)];
unsigned char m3[sizeof(m)];
unsigned char m4[sizeof(m)];

uint16_t mlen;
uint16_t c1len, c2len, c3len, c4len;
uint16_t m1len, m2len, m3len, m4len;

uint8_t testno;

// Test 100 random messages and keypairs
for (testno = 0; testno < 100; testno++) {
for (uint8_t testno = 0; testno < 100; testno++) {
unsigned char pk1[CRYPTO_PUBLIC_KEY_SIZE];
unsigned char sk1[CRYPTO_SECRET_KEY_SIZE];
unsigned char pk2[CRYPTO_PUBLIC_KEY_SIZE];
unsigned char sk2[CRYPTO_SECRET_KEY_SIZE];
unsigned char k1[CRYPTO_SHARED_KEY_SIZE];
unsigned char k2[CRYPTO_SHARED_KEY_SIZE];

unsigned char n[CRYPTO_NONCE_SIZE];

unsigned char m[500];
unsigned char c1[sizeof(m) + CRYPTO_MAC_SIZE];
unsigned char c2[sizeof(m) + CRYPTO_MAC_SIZE];
unsigned char c3[sizeof(m) + CRYPTO_MAC_SIZE];
unsigned char c4[sizeof(m) + CRYPTO_MAC_SIZE];
unsigned char m1[sizeof(m)];
unsigned char m2[sizeof(m)];
unsigned char m3[sizeof(m)];
unsigned char m4[sizeof(m)];

//Generate random message (random length from 100 to 500)
mlen = (random_u32() % 400) + 100;
const uint16_t mlen = (random_u32() % 400) + 100;
rand_bytes(m, mlen);
rand_bytes(n, CRYPTO_NONCE_SIZE);

Expand All @@ -178,21 +172,21 @@ START_TEST(test_endtoend)
ck_assert_msg(memcmp(k1, k2, CRYPTO_SHARED_KEY_SIZE) == 0, "encrypt_precompute: bad");

//Encrypt all four ways
c1len = encrypt_data(pk2, sk1, n, m, mlen, c1);
c2len = encrypt_data(pk1, sk2, n, m, mlen, c2);
c3len = encrypt_data_symmetric(k1, n, m, mlen, c3);
c4len = encrypt_data_symmetric(k2, n, m, mlen, c4);
const uint16_t c1len = encrypt_data(pk2, sk1, n, m, mlen, c1);
const uint16_t c2len = encrypt_data(pk1, sk2, n, m, mlen, c2);
const uint16_t c3len = encrypt_data_symmetric(k1, n, m, mlen, c3);
const uint16_t c4len = encrypt_data_symmetric(k2, n, m, mlen, c4);

ck_assert_msg(c1len == c2len && c1len == c3len && c1len == c4len, "cyphertext lengths differ");
ck_assert_msg(c1len == mlen + (uint16_t)CRYPTO_MAC_SIZE, "wrong cyphertext length");
ck_assert_msg(memcmp(c1, c2, c1len) == 0 && memcmp(c1, c3, c1len) == 0
&& memcmp(c1, c4, c1len) == 0, "crypertexts differ");

//Decrypt all four ways
m1len = decrypt_data(pk2, sk1, n, c1, c1len, m1);
m2len = decrypt_data(pk1, sk2, n, c1, c1len, m2);
m3len = decrypt_data_symmetric(k1, n, c1, c1len, m3);
m4len = decrypt_data_symmetric(k2, n, c1, c1len, m4);
const uint16_t m1len = decrypt_data(pk2, sk1, n, c1, c1len, m1);
const uint16_t m2len = decrypt_data(pk1, sk2, n, c1, c1len, m2);
const uint16_t m3len = decrypt_data_symmetric(k1, n, c1, c1len, m3);
const uint16_t m4len = decrypt_data_symmetric(k2, n, c1, c1len, m4);

ck_assert_msg(m1len == m2len && m1len == m3len && m1len == m4len, "decrypted text lengths differ");
ck_assert_msg(m1len == mlen, "wrong decrypted text length");
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ START_TEST(test_addr_resolv_localhost)
ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror);
net_kill_strerror(strerror);

ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6,
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6,
ip.family.value);
IP6 ip6_loopback = get_ip6_loopback();
ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.",
Expand All @@ -83,7 +83,7 @@ START_TEST(test_addr_resolv_localhost)
net_kill_strerror(strerror);

#if USE_IPV6
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6,
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6,
ip.family.value);
ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.",
ip_ntoa(&ip, ip_str, sizeof(ip_str)));
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/save_load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static void test_few_clients(void)
uint8_t address[TOX_ADDRESS_SIZE];
tox_self_get_address(tox2, address);
uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr);
ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
ck_assert_msg(test == 0, "Failed to add friend error code: %u", test);

uint8_t off = 1;

Expand Down
2 changes: 1 addition & 1 deletion auto_tests/set_name_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void test_set_name(void)
tox_callback_friend_name(tox2, nickchange_callback);
Tox_Err_Set_Info err_n;
bool ret = tox_self_set_name(tox1, (const uint8_t *)NICKNAME, sizeof(NICKNAME), &err_n);
ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_name failed because %u\n", err_n);
ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_name failed because %d\n", err_n);

bool nickname_updated = false;

Expand Down
2 changes: 1 addition & 1 deletion auto_tests/set_status_message_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void test_set_status_message(void)
tox_callback_friend_status_message(tox2, status_callback);
bool ret = tox_self_set_status_message(tox1, (const uint8_t *)STATUS_MESSAGE, sizeof(STATUS_MESSAGE),
&err_n);
ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %u\n", err_n);
ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %d\n", err_n);

bool status_updated = false;

Expand Down
2 changes: 1 addition & 1 deletion auto_tests/toxav_basic_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void t_toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enable

static void t_toxav_call_state_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data)
{
printf("Handling CALL STATE callback: %d\n", state);
printf("Handling CALL STATE callback: %u\n", state);
((CallControl *)user_data)->state = state;
}

Expand Down
2 changes: 1 addition & 1 deletion auto_tests/toxav_many_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void t_toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enable

static void t_toxav_call_state_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data)
{
printf("Handling CALL STATE callback: %d %p\n", state, (void *)av);
printf("Handling CALL STATE callback: %u %p\n", state, (void *)av);
((CallControl *)user_data)[friend_number].state = state;
}

Expand Down
12 changes: 2 additions & 10 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2614,17 +2614,9 @@ void do_messenger(Messenger *m, void *userdata)
LOGGER_TRACE(m->log, "Friend num in DHT %u != friend num in msger %u", dht_get_num_friends(m->dht), m->numfriends);
}

Friend *msgfptr;
DHT_Friend *dhtfptr;

for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
if (dht2m[friend_idx] >= 0) {
msgfptr = &m->friendlist[dht2m[friend_idx]];
} else {
msgfptr = nullptr;
}

dhtfptr = dht_get_friend(m->dht, friend_idx);
const Friend *const msgfptr = dht2m[friend_idx] >= 0 ? &m->friendlist[dht2m[friend_idx]] : nullptr;
const DHT_Friend *const dhtfptr = dht_get_friend(m->dht, friend_idx);

if (msgfptr) {
char id_str[IDSTRING_LEN];
Expand Down
2 changes: 1 addition & 1 deletion toxcore/ccompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// "function") is used. Note the semantic difference: alloca'd memory does not
// get freed at the end of the declaration's scope. Do not use VLA() in loops or
// you may run out of stack space.
#if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#if !defined(DISABLE_VLA) && !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
// C99 VLAs.
#define VLA(type, name, size) type name[size]
#define SIZEOF_VLA sizeof
Expand Down