Skip to content

Commit

Permalink
cleanup: Initialise all local variables.
Browse files Browse the repository at this point in the history
It's unnecessary in any of these cases, but having the warning is
useful, and may catch errors in the future.
  • Loading branch information
iphydf committed Sep 1, 2023
1 parent 4d3c97f commit 608c881
Show file tree
Hide file tree
Showing 87 changed files with 493 additions and 488 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: 4 additions & 4 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void bootstrap_tox_live_network(Tox *tox, bool enable_tcp)
uint16_t port = bootstrap_nodes[j].port;
const uint8_t *key = bootstrap_nodes[j].key;

Tox_Err_Bootstrap err;
Tox_Err_Bootstrap err = TOX_ERR_BOOTSTRAP_OK;
tox_bootstrap(tox, ip, port, key, &err);

if (err != TOX_ERR_BOOTSTRAP_OK) {
Expand Down Expand Up @@ -287,7 +287,7 @@ static void autotox_add_friend(AutoTox *autotoxes, uint32_t adding, uint32_t add
{
uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_public_key(autotoxes[added].tox, public_key);
Tox_Err_Friend_Add err;
Tox_Err_Friend_Add err = TOX_ERR_FRIEND_ADD_OK;
tox_friend_add_norequest(autotoxes[adding].tox, public_key, &err);
ck_assert(err == TOX_ERR_FRIEND_ADD_OK);
}
Expand Down Expand Up @@ -330,7 +330,7 @@ static void bootstrap_autotoxes(struct Tox_Options *options, uint32_t tox_count,
const uint16_t dht_port = tox_self_get_udp_port(autotoxes[0].tox, nullptr);

for (uint32_t i = 1; i < tox_count; ++i) {
Tox_Err_Bootstrap err;
Tox_Err_Bootstrap err = TOX_ERR_BOOTSTRAP_OK;
tox_bootstrap(autotoxes[i].tox, "localhost", dht_port, dht_key, &err);
ck_assert(err == TOX_ERR_BOOTSTRAP_OK);
}
Expand All @@ -340,7 +340,7 @@ static void bootstrap_autotoxes(struct Tox_Options *options, uint32_t tox_count,
printf("bootstrapping all toxes to local TCP relay running on port %d\n", autotest_opts->tcp_port);

for (uint32_t i = 0; i < tox_count; ++i) {
Tox_Err_Bootstrap err;
Tox_Err_Bootstrap err = TOX_ERR_BOOTSTRAP_OK;
tox_add_tcp_relay(autotoxes[i].tox, "localhost", autotest_opts->tcp_port, dht_key, &err);
ck_assert(err == TOX_ERR_BOOTSTRAP_OK);
}
Expand Down
14 changes: 9 additions & 5 deletions auto_tests/conference_av_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static void handle_conference_connected(
return;
}

Tox_Err_Conference_Invite err;
Tox_Err_Conference_Invite err = TOX_ERR_CONFERENCE_INVITE_OK;
tox_conference_invite(tox, 1, 0, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox #%u failed to invite next friend: err = %d", autotox->index,
err);
Expand Down Expand Up @@ -167,7 +167,7 @@ static bool all_connected_to_group(uint32_t tox_count, AutoTox *autotoxes)
*/
static uint32_t random_false_index(const Random *rng, bool *list, const uint32_t length)
{
uint32_t index;
uint32_t index = 0;

do {
index = random_u32(rng) % length;
Expand Down 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 Expand Up @@ -433,7 +437,7 @@ static void test_groupav(AutoTox *autotoxes)
iterate_all_wait(autotoxes, NUM_AV_GROUP_TOX, ITERATION_INTERVAL);

for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) {
Tox_Err_Conference_Peer_Query err;
Tox_Err_Conference_Peer_Query err = TOX_ERR_CONFERENCE_PEER_QUERY_OK;
uint32_t peer_count = tox_conference_peer_count(autotoxes[i].tox, 0, &err);

if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/conference_double_invite_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void handle_conference_invite(
ck_assert_msg(!state->joined, "invitation callback generated for already joined conference");

if (friend_number != -1) {
Tox_Err_Conference_Join err;
Tox_Err_Conference_Join err = TOX_ERR_CONFERENCE_JOIN_OK;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,
"attempting to join the conference returned with an error: %d", err);
Expand All @@ -46,7 +46,7 @@ static void conference_double_invite_test(AutoTox *autotoxes)

{
// Create new conference, tox0 is the founder.
Tox_Err_Conference_New err;
Tox_Err_Conference_New err = TOX_ERR_CONFERENCE_NEW_OK;
state[0]->conference = tox_conference_new(autotoxes[0].tox, &err);
state[0]->joined = true;
ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK,
Expand All @@ -56,7 +56,7 @@ static void conference_double_invite_test(AutoTox *autotoxes)

{
// Invite friend.
Tox_Err_Conference_Invite err;
Tox_Err_Conference_Invite err = TOX_ERR_CONFERENCE_INVITE_OK;
tox_conference_invite(autotoxes[0].tox, 0, state[0]->conference, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK,
"attempting to invite a friend returned with an error: %d", err);
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/conference_invite_merge_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void handle_conference_invite(
State *state = (State *)autotox->state;

if (friend_number != -1) {
Tox_Err_Conference_Join err;
Tox_Err_Conference_Join err = TOX_ERR_CONFERENCE_JOIN_OK;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,
"attempting to join the conference returned with an error: %d", err);
Expand Down Expand Up @@ -48,7 +48,7 @@ static void do_invite(AutoTox *autotoxes, AutoTox *inviter, AutoTox *invitee, ui
{
fprintf(stderr, "#%u inviting #%u\n", inviter->index, invitee->index);

Tox_Err_Conference_Invite err;
Tox_Err_Conference_Invite err = TOX_ERR_CONFERENCE_INVITE_OK;
tox_conference_invite(inviter->tox, friendnum, ((State *)inviter->state)->conference, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK,
"#%u attempting to invite #%u (friendnumber %u) returned with an error: %d", inviter->index, invitee->index,
Expand Down Expand Up @@ -103,7 +103,7 @@ static void conference_invite_merge_test(AutoTox *autotoxes)

{
// Create new conference, tox 2 is the founder.
Tox_Err_Conference_New err;
Tox_Err_Conference_New err = TOX_ERR_CONFERENCE_NEW_OK;
state2->conference = tox_conference_new(autotoxes[2].tox, &err);
state2->connected = true;
ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK,
Expand Down
10 changes: 5 additions & 5 deletions auto_tests/conference_peer_nick_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static void handle_conference_invite(
autotox->index, friend_number, type, (unsigned)length);
fprintf(stderr, "tox%u joining conference\n", autotox->index);

Tox_Err_Conference_Join err;
Tox_Err_Conference_Join err = TOX_ERR_CONFERENCE_JOIN_OK;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK,
"attempting to join the conference returned with an error: %d", err);
Expand All @@ -39,7 +39,7 @@ static void handle_peer_list_changed(Tox *tox, uint32_t conference_number, void
fprintf(stderr, "handle_peer_list_changed(#%u, %u, _)\n",
autotox->index, conference_number);

Tox_Err_Conference_Peer_Query err;
Tox_Err_Conference_Peer_Query err = TOX_ERR_CONFERENCE_PEER_QUERY_OK;
uint32_t const count = tox_conference_peer_count(tox, conference_number, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK,
"failed to get conference peer count: err = %d", err);
Expand All @@ -52,7 +52,7 @@ static void rebuild_peer_list(Tox *tox)
for (uint32_t conference_number = 0;
conference_number < tox_conference_get_chatlist_size(tox);
++conference_number) {
Tox_Err_Conference_Peer_Query err;
Tox_Err_Conference_Peer_Query err = TOX_ERR_CONFERENCE_PEER_QUERY_OK;
uint32_t const count = tox_conference_peer_count(tox, conference_number, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK,
"failed to get conference peer count for conference %u: err = %d", conference_number, err);
Expand Down Expand Up @@ -90,7 +90,7 @@ static void conference_peer_nick_test(AutoTox *autotoxes)

{
// Create new conference, tox0 is the founder.
Tox_Err_Conference_New err;
Tox_Err_Conference_New err = TOX_ERR_CONFERENCE_NEW_OK;
state[0]->conference = tox_conference_new(autotoxes[0].tox, &err);
state[0]->joined = true;
ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK,
Expand All @@ -100,7 +100,7 @@ static void conference_peer_nick_test(AutoTox *autotoxes)

{
// Invite friend.
Tox_Err_Conference_Invite err;
Tox_Err_Conference_Invite err = TOX_ERR_CONFERENCE_INVITE_OK;
tox_conference_invite(autotoxes[0].tox, 0, state[0]->conference, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK,
"attempting to invite a friend returned with an error: %d", err);
Expand Down
12 changes: 6 additions & 6 deletions auto_tests/conference_simple_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void handle_conference_invite(Tox *tox, uint32_t friend_number, Tox_Confe
fprintf(stderr, "tox%u joining conference\n", state->id);

{
Tox_Err_Conference_Join err;
Tox_Err_Conference_Join err = TOX_ERR_CONFERENCE_JOIN_OK;
state->conference = tox_conference_join(tox, friend_number, cookie, length, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, "failed to join a conference: err = %d", err);
fprintf(stderr, "tox%u Joined conference %u\n", state->id, state->conference);
Expand All @@ -74,7 +74,7 @@ static void handle_conference_peer_list_changed(Tox *tox, uint32_t conference_nu
fprintf(stderr, "handle_conference_peer_list_changed(#%u, %u, _)\n",
state->id, conference_number);

Tox_Err_Conference_Peer_Query err;
Tox_Err_Conference_Peer_Query err = TOX_ERR_CONFERENCE_PEER_QUERY_OK;
uint32_t count = tox_conference_peer_count(tox, conference_number, &err);

if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
Expand All @@ -92,7 +92,7 @@ static void handle_conference_connected(Tox *tox, uint32_t conference_number, vo

// We're tox2, so now we invite tox3.
if (state->id == 2 && !state->invited_next) {
Tox_Err_Conference_Invite err;
Tox_Err_Conference_Invite err = TOX_ERR_CONFERENCE_INVITE_OK;
tox_conference_invite(tox, 1, state->conference, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox2 failed to invite tox3: err = %d", err);

Expand Down Expand Up @@ -187,7 +187,7 @@ int main(void)

{
// Create new conference, tox1 is the founder.
Tox_Err_Conference_New err;
Tox_Err_Conference_New err = TOX_ERR_CONFERENCE_NEW_OK;
state1.conference = tox_conference_new(tox1, &err);
state1.joined = true;
ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "failed to create a conference: err = %d", err);
Expand All @@ -196,7 +196,7 @@ int main(void)

{
// Invite friend.
Tox_Err_Conference_Invite err;
Tox_Err_Conference_Invite err = TOX_ERR_CONFERENCE_INVITE_OK;
tox_conference_invite(tox1, 0, state1.conference, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "failed to invite a friend: err = %d", err);
state1.invited_next = true;
Expand Down Expand Up @@ -229,7 +229,7 @@ int main(void)

{
fprintf(stderr, "tox1 sends a message to the group: \"hello!\"\n");
Tox_Err_Conference_Send_Message err;
Tox_Err_Conference_Send_Message err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK;
tox_conference_send_message(tox1, state1.conference, TOX_MESSAGE_TYPE_NORMAL,
(const uint8_t *)"hello!", 7, &err);

Expand Down
12 changes: 6 additions & 6 deletions auto_tests/conference_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void handle_conference_invite(
const AutoTox *autotox = (AutoTox *)user_data;
ck_assert_msg(type == TOX_CONFERENCE_TYPE_TEXT, "tox #%u: wrong conference type: %d", autotox->index, type);

Tox_Err_Conference_Join err;
Tox_Err_Conference_Join err = TOX_ERR_CONFERENCE_JOIN_OK;
uint32_t g_num = tox_conference_join(tox, friendnumber, data, length, &err);

ck_assert_msg(err == TOX_ERR_CONFERENCE_JOIN_OK, "tox #%u: error joining group: %d", autotox->index, err);
Expand All @@ -77,7 +77,7 @@ static void handle_conference_connected(
return;
}

Tox_Err_Conference_Invite err;
Tox_Err_Conference_Invite err = TOX_ERR_CONFERENCE_INVITE_OK;
tox_conference_invite(tox, 1, 0, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_INVITE_OK, "tox #%u failed to invite next friend: err = %d", autotox->index,
err);
Expand Down Expand Up @@ -181,7 +181,7 @@ static bool names_propagated(uint32_t tox_count, AutoTox *autotoxes)
*/
static uint32_t random_false_index(const Random *rng, bool *list, const uint32_t length)
{
uint32_t index;
uint32_t index = 0;

do {
index = random_u32(rng) % length;
Expand All @@ -204,7 +204,7 @@ static void run_conference_tests(AutoTox *autotoxes)
printf("restricting number of frozen peers to %u\n", max_frozen);

for (uint16_t i = 0; i < NUM_GROUP_TOX; ++i) {
Tox_Err_Conference_Set_Max_Offline err;
Tox_Err_Conference_Set_Max_Offline err = TOX_ERR_CONFERENCE_SET_MAX_OFFLINE_OK;
tox_conference_set_max_offline(autotoxes[i].tox, 0, max_frozen, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_SET_MAX_OFFLINE_OK,
"tox #%u failed to set max offline: err = %d", autotoxes[i].index, err);
Expand Down Expand Up @@ -291,7 +291,7 @@ static void run_conference_tests(AutoTox *autotoxes)
iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL);
}

Tox_Err_Conference_Send_Message err;
Tox_Err_Conference_Send_Message err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK;
ck_assert_msg(
tox_conference_send_message(
autotoxes[random_u32(rng) % NUM_GROUP_TOX].tox, 0, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)GROUP_MESSAGE,
Expand Down Expand Up @@ -382,7 +382,7 @@ static void test_many_group(AutoTox *autotoxes)
iterate_all_wait(autotoxes, NUM_GROUP_TOX, ITERATION_INTERVAL);

for (uint32_t i = 0; i < NUM_GROUP_TOX; ++i) {
Tox_Err_Conference_Peer_Query err;
Tox_Err_Conference_Peer_Query err = TOX_ERR_CONFERENCE_PEER_QUERY_OK;
uint32_t peer_count = tox_conference_peer_count(autotoxes[i].tox, 0, &err);

if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/conference_two_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(void)
Tox *tox1 = tox_new_log(nullptr, nullptr, &id);

// Create two conferences and then exit.
Tox_Err_Conference_New err;
Tox_Err_Conference_New err = TOX_ERR_CONFERENCE_NEW_OK;
tox_conference_new(tox1, &err);
ck_assert_msg(err == TOX_ERR_CONFERENCE_NEW_OK, "failed to create conference 1: %d", err);
tox_conference_new(tox1, &err);
Expand Down
12 changes: 6 additions & 6 deletions auto_tests/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

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

ck_assert_msg(sizeof(c) == sizeof(m) + CRYPTO_MAC_SIZE * sizeof(uint8_t),
"cyphertext should be CRYPTO_MAC_SIZE bytes longer than plaintext");
Expand All @@ -107,7 +107,7 @@ 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;
uint16_t clen = 0, mlen = 0;

encrypt_precompute(bobpk, alicesk, k);

Expand Down Expand Up @@ -275,7 +275,7 @@ 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, num2 = 0;
memcpy(&num1, nonce + (CRYPTO_NONCE_SIZE - sizeof(num1)), sizeof(num1));
num1 = net_ntohl(num1);
num2 = num + num1;
Expand All @@ -299,7 +299,7 @@ static void test_increment_nonce(void)
const Random *rng = system_random();
ck_assert(rng != nullptr);

uint32_t i;
uint32_t i = 0;

uint8_t n[CRYPTO_NONCE_SIZE];

Expand Down Expand Up @@ -331,7 +331,7 @@ static void test_memzero(void)
memcpy(src, test_c, sizeof(test_c));

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

for (i = 0; i < sizeof(src); i++) {
ck_assert_msg(src[i] == 0, "Memory is not zeroed");
Expand Down
Loading

0 comments on commit 608c881

Please sign in to comment.