Skip to content

Commit ce4f29e

Browse files
committed
cleanup: Fix all -Wsign-compare warnings.
1 parent 4d4251c commit ce4f29e

17 files changed

+27
-32
lines changed

.github/scripts/flags-clang.sh

-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ add_flag -Wno-padded
3636
# This warns on things like _XOPEN_SOURCE, which we currently need (we
3737
# probably won't need these in the future).
3838
add_flag -Wno-reserved-id-macro
39-
# TODO(iphydf): Clean these up. They are likely not bugs, but still
40-
# potential issues and probably confusing.
41-
add_flag -Wno-sign-compare
4239
# We don't want to have default cases, we want to explicitly define all cases
4340
add_flag -Wno-switch-default
4441
# __attribute__((nonnull)) causes this warning on defensive null checks.

.github/scripts/flags-gcc.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ add_flag -Wunused-value
4747

4848
# struct Foo foo = {0}; is a common idiom.
4949
add_flag -Wno-missing-field-initializers
50-
# TODO(iphydf): Clean these up. They are likely not bugs, but still
51-
# potential issues and probably confusing.
50+
# Checked by clang, but gcc is warning when it's not necessary.
5251
add_flag -Wno-sign-compare
5352
# File transfer code has this.
5453
add_flag -Wno-type-limits

auto_tests/conference_av_test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static void test_groupav(AutoTox *autotoxes)
420420
tox_events_callback_conference_connected(autotoxes[i].dispatch, handle_conference_connected);
421421
}
422422

423-
ck_assert_msg(toxav_add_av_groupchat(autotoxes[0].tox, audio_callback, &autotoxes[0]) != UINT32_MAX,
423+
ck_assert_msg(toxav_add_av_groupchat(autotoxes[0].tox, audio_callback, &autotoxes[0]) != -1,
424424
"failed to create group");
425425
printf("tox #%u: inviting its first friend\n", autotoxes[0].index);
426426
ck_assert_msg(tox_conference_invite(autotoxes[0].tox, 0, 0, nullptr) != 0, "failed to invite friend");

auto_tests/conference_double_invite_test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void handle_conference_invite(
2828

2929
ck_assert_msg(!state->joined, "invitation callback generated for already joined conference");
3030

31-
if (friend_number != -1) {
31+
if (friend_number != UINT32_MAX) {
3232
Tox_Err_Conference_Join err;
3333
state->conference = tox_conference_join(autotox->tox, friend_number, cookie, length, &err);
3434
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,

auto_tests/conference_invite_merge_test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void handle_conference_invite(
2121
const uint8_t *cookie = tox_event_conference_invite_get_cookie(event);
2222
const size_t length = tox_event_conference_invite_get_cookie_length(event);
2323

24-
if (friend_number != -1) {
24+
if (friend_number != UINT32_MAX) {
2525
Tox_Err_Conference_Join err;
2626
state->conference = tox_conference_join(autotox->tox, friend_number, cookie, length, &err);
2727
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,

other/analysis/run-clang

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ run() {
2727
-Wno-missing-noreturn \
2828
-Wno-old-style-cast \
2929
-Wno-padded \
30-
-Wno-sign-compare \
3130
-Wno-switch-default \
3231
-Wno-tautological-pointer-compare \
3332
-Wno-unreachable-code-return \

other/bootstrap_daemon/src/tox-bootstrapd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ int main(int argc, char *argv[])
618618
break;
619619

620620
default:
621-
log_write(LOG_LEVEL_INFO, "Received (%d) signal. Exiting.\n", caught_signal);
621+
log_write(LOG_LEVEL_INFO, "Received (%ld) signal. Exiting.\n", (long)caught_signal);
622622
}
623623

624624
lan_discovery_kill(broadcast);

other/fun/save-generator.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ int main(int argc, char *argv[])
137137
printf("Failed to set status. Error number: %d\n", err);
138138
}
139139

140-
for (unsigned int i = 2; i < argc; i++) { //start at 2 because that is where the tox ids are
140+
for (int i = 2; i < argc; i++) { //start at 2 because that is where the tox ids are
141141
uint8_t *address = hex_string_to_bin(argv[i]);
142142
Tox_Err_Friend_Add friend_err;
143143
tox_friend_add(tox, address, (const uint8_t *)GENERATED_REQUEST_MESSAGE, strlen(GENERATED_REQUEST_MESSAGE),
144144
&friend_err);
145145
free(address);
146146

147147
if (friend_err != TOX_ERR_FRIEND_ADD_OK) {
148-
printf("Failed to add friend number %u. Error number: %d\n", i - 1, friend_err);
148+
printf("Failed to add friend number %d. Error number: %d\n", i - 1, friend_err);
149149
}
150150
}
151151

other/fun/sign.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static int load_file(const char *filename, unsigned char **result)
3535
fseek(f, 0, SEEK_SET);
3636
*result = (unsigned char *)malloc(size + 1);
3737

38-
if (size != fread(*result, sizeof(char), size, f)) {
38+
if ((size_t)size != fread(*result, sizeof(char), size, f)) {
3939
free(*result);
4040
fclose(f);
4141
return -2; // -2 means file reading fail
@@ -55,13 +55,13 @@ int main(int argc, char *argv[])
5555
crypto_sign_ed25519_keypair(pk, sk);
5656
printf("Public key:\n");
5757

58-
for (int i = 0; i < crypto_sign_ed25519_PUBLICKEYBYTES; ++i) {
58+
for (uint32_t i = 0; i < crypto_sign_ed25519_PUBLICKEYBYTES; ++i) {
5959
printf("%02X", pk[i]);
6060
}
6161

6262
printf("\nSecret key:\n");
6363

64-
for (int i = 0; i < crypto_sign_ed25519_SECRETKEYBYTES; ++i) {
64+
for (uint32_t i = 0; i < crypto_sign_ed25519_SECRETKEYBYTES; ++i) {
6565
printf("%02X", sk[i]);
6666
}
6767

other/fun/strkey.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ int main(int argc, char *argv[])
117117
#endif
118118
crypto_box_keypair(public_key, secret_key);
119119

120-
for (int i = 0; i <= crypto_box_PUBLICKEYBYTES - len; ++i) {
120+
for (uint32_t i = 0; i <= crypto_box_PUBLICKEYBYTES - len; ++i) {
121121
if (memcmp(public_key + i, desired_bin, len) == 0) {
122122
found = 1;
123123
break;

toxav/groupav.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ int groupchat_enable_av(const Logger *log, Tox *tox, Group_Chats *g_c, uint32_t
476476
return -1;
477477
}
478478

479-
for (uint32_t i = 0; i < numpeers; ++i) {
479+
for (uint32_t i = 0; i < (uint32_t)numpeers; ++i) {
480480
group_av_peer_new(group_av, conference_number, i);
481481
}
482482

@@ -508,7 +508,7 @@ int groupchat_disable_av(const Group_Chats *g_c, uint32_t conference_number)
508508
return -1;
509509
}
510510

511-
for (uint32_t i = 0; i < numpeers; ++i) {
511+
for (uint32_t i = 0; i < (uint32_t)numpeers; ++i) {
512512
group_av_peer_delete(group_av, conference_number, group_peer_get_object(g_c, conference_number, i));
513513
group_peer_set_object(g_c, conference_number, i, nullptr);
514514
}

toxcore/DHT.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,12 @@ int dht_create_packet(const Memory *mem, const Random *rng,
380380

381381
const int encrypted_length = encrypt_data_symmetric(shared_key, nonce, plain, plain_length, encrypted);
382382

383-
if (encrypted_length == -1) {
383+
if (encrypted_length < 0) {
384384
mem_delete(mem, encrypted);
385385
return -1;
386386
}
387387

388-
if (length < 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + encrypted_length) {
388+
if (length < 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + (size_t)encrypted_length) {
389389
mem_delete(mem, encrypted);
390390
return -1;
391391
}
@@ -1347,15 +1347,15 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
13471347
plain[0] = num_nodes;
13481348
memcpy(plain + 1 + nodes_length, sendback_data, length);
13491349

1350-
const uint32_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
1351-
const uint32_t data_size = 1 + nodes_length + length + crypto_size;
1350+
const uint16_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
1351+
const uint16_t data_size = 1 + nodes_length + length + crypto_size;
13521352
VLA(uint8_t, data, data_size);
13531353

13541354
const int len = dht_create_packet(dht->mem, dht->rng,
13551355
dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
13561356
plain, 1 + nodes_length + length, data, data_size);
13571357

1358-
if (len != data_size) {
1358+
if (len < 0 || (uint32_t)len != data_size) {
13591359
return -1;
13601360
}
13611361

toxcore/announce.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ static int create_reply(Announcements *announce, const IP_Port *source,
579579
const int plain_reply_max_len = (int)reply_max_length -
580580
(1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE);
581581

582-
if (plain_reply_max_len < sizeof(uint64_t)) {
582+
if (plain_reply_max_len < (int)sizeof(uint64_t)) {
583583
return -1;
584584
}
585585

toxcore/group.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,7 @@ static int handle_send_peers(Group_Chats *g_c, uint32_t groupnumber, const uint8
25202520

25212521
non_null(1, 3) nullable(6)
25222522
static void handle_direct_packet(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length,
2523-
int connection_index, void *userdata)
2523+
uint32_t connection_index, void *userdata)
25242524
{
25252525
if (length == 0) {
25262526
return;
@@ -2832,7 +2832,7 @@ static bool check_message_info(uint32_t message_number, uint8_t message_id, Grou
28322832

28332833
non_null(1, 3) nullable(6)
28342834
static void handle_message_packet_group(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length,
2835-
int connection_index, void *userdata)
2835+
uint32_t connection_index, void *userdata)
28362836
{
28372837
if (length < sizeof(uint16_t) + sizeof(uint32_t) + 1) {
28382838
return;

toxcore/group_chats.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -8353,7 +8353,7 @@ bool gc_group_is_valid(const GC_Chat *chat)
83538353
/** Return true if `group_number` designates an active group in session `c`. */
83548354
static bool group_number_valid(const GC_Session *c, int group_number)
83558355
{
8356-
if (group_number < 0 || group_number >= c->chats_index) {
8356+
if (group_number < 0 || (uint32_t)group_number >= c->chats_index) {
83578357
return false;
83588358
}
83598359

toxcore/onion_client.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ static int handle_announce_response(void *object, const IP_Port *source, const u
955955
}
956956

957957
uint8_t plain[1 + ONION_PING_ID_SIZE + ONION_ANNOUNCE_RESPONSE_MAX_SIZE - ONION_ANNOUNCE_RESPONSE_MIN_SIZE];
958-
const int plain_size = 1 + ONION_PING_ID_SIZE + length - ONION_ANNOUNCE_RESPONSE_MIN_SIZE;
958+
const uint32_t plain_size = 1 + ONION_PING_ID_SIZE + length - ONION_ANNOUNCE_RESPONSE_MIN_SIZE;
959959
int len;
960960
const uint16_t nonce_start = 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH;
961961
const uint16_t ciphertext_start = nonce_start + CRYPTO_NONCE_SIZE;

toxencryptsave/toxencryptsave.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], si
231231
ciphertext += crypto_box_NONCEBYTES;
232232

233233
/* now encrypt */
234-
if (encrypt_data_symmetric(key->key, nonce, plaintext, plaintext_len, ciphertext)
235-
!= plaintext_len + crypto_box_MACBYTES) {
234+
const int32_t encrypted_len = encrypt_data_symmetric(key->key, nonce, plaintext, plaintext_len, ciphertext);
235+
if (encrypted_len < 0 || (size_t)encrypted_len != plaintext_len + crypto_box_MACBYTES) {
236236
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED);
237237
return false;
238238
}
@@ -316,8 +316,8 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], s
316316
ciphertext += crypto_box_NONCEBYTES;
317317

318318
/* decrypt the ciphertext */
319-
if (decrypt_data_symmetric(key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext)
320-
!= decrypt_length) {
319+
const int32_t decrypted_len = decrypt_data_symmetric(key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext);
320+
if (decrypted_len < 0 || (size_t)decrypted_len != decrypt_length) {
321321
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED);
322322
return false;
323323
}

0 commit comments

Comments
 (0)