Skip to content

Commit

Permalink
cleanup: Reduce the scope of for-loop iterator variables.
Browse files Browse the repository at this point in the history
Found by tokstyle after TokTok/hs-tokstyle#95.
  • Loading branch information
iphydf committed Jan 1, 2022
1 parent 6f61de5 commit e1fd303
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 128 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f0f315faebe7fc88b15ba656c5866c3932b5495c23ee99bdac539ba986688c92 /usr/local/bin/tox-bootstrapd
2b75296c24edbefa40809eeef66fa74791e28da407ee9364fb8e516c2ee882aa /usr/local/bin/tox-bootstrapd
38 changes: 12 additions & 26 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ static int realloc_friendlist(Messenger *m, uint32_t num)
*/
int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk)
{
uint32_t i;

for (i = 0; i < m->numfriends; ++i) {
for (uint32_t i = 0; i < m->numfriends; ++i) {
if (m->friendlist[i].status > 0) {
if (id_equal(real_pk, m->friendlist[i].real_pk)) {
return i;
Expand Down Expand Up @@ -114,9 +112,8 @@ static uint16_t address_checksum(const uint8_t *address, uint32_t len)
{
uint8_t checksum[2] = {0};
uint16_t check;
uint32_t i;

for (i = 0; i < len; ++i) {
for (uint32_t i = 0; i < len; ++i) {
checksum[i % 2] ^= address[i];
}

Expand Down Expand Up @@ -181,9 +178,7 @@ static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t sta
return FAERR_NOMEM;
}

uint32_t i;

for (i = 0; i <= m->numfriends; ++i) {
for (uint32_t i = 0; i <= m->numfriends; ++i) {
if (m->friendlist[i].status == NOFRIEND) {
m->friendlist[i].status = status;
m->friendlist[i].friendcon_id = friendcon_id;
Expand Down Expand Up @@ -422,6 +417,7 @@ int m_delfriend(Messenger *m, int32_t friendnumber)

kill_friend_connection(m->fr_c, m->friendlist[friendnumber].friendcon_id);
memset(&m->friendlist[friendnumber], 0, sizeof(Friend));

uint32_t i;

for (i = m->numfriends; i != 0; --i) {
Expand Down Expand Up @@ -595,9 +591,8 @@ int setname(Messenger *m, const uint8_t *name, uint16_t length)
}

m->name_length = length;
uint32_t i;

for (i = 0; i < m->numfriends; ++i) {
for (uint32_t i = 0; i < m->numfriends; ++i) {
m->friendlist[i].name_sent = 0;
}

Expand Down Expand Up @@ -666,9 +661,7 @@ int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length)

m->statusmessage_length = length;

uint32_t i;

for (i = 0; i < m->numfriends; ++i) {
for (uint32_t i = 0; i < m->numfriends; ++i) {
m->friendlist[i].statusmessage_sent = 0;
}

Expand All @@ -686,9 +679,8 @@ int m_set_userstatus(Messenger *m, uint8_t status)
}

m->userstatus = (Userstatus)status;
uint32_t i;

for (i = 0; i < m->numfriends; ++i) {
for (uint32_t i = 0; i < m->numfriends; ++i) {
m->friendlist[i].userstatus_sent = 0;
}

Expand Down Expand Up @@ -2026,8 +2018,6 @@ void kill_messenger(Messenger *m)
return;
}

uint32_t i;

if (m->tcp_server) {
kill_TCP_server(m->tcp_server);
}
Expand All @@ -2040,7 +2030,7 @@ void kill_messenger(Messenger *m)
kill_dht(m->dht);
kill_networking(m->net);

for (i = 0; i < m->numfriends; ++i) {
for (uint32_t i = 0; i < m->numfriends; ++i) {
clear_receipts(m, i);
}

Expand Down Expand Up @@ -2399,10 +2389,9 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le

static void do_friends(Messenger *m, void *userdata)
{
uint32_t i;
uint64_t temp_time = mono_time_get(m->mono_time);

for (i = 0; i < m->numfriends; ++i) {
for (uint32_t i = 0; i < m->numfriends; ++i) {
if (m->friendlist[i].status == FRIEND_ADDED) {
int fr = send_friend_request_packet(m->fr_c, m->friendlist[i].friendcon_id, m->friendlist[i].friendrequest_nospam,
m->friendlist[i].info,
Expand Down Expand Up @@ -2969,10 +2958,9 @@ static State_Load_Status friends_list_load(Messenger *m, const uint8_t *data, ui
}

uint32_t num = length / l_friend_size;
uint32_t i;
const uint8_t *cur_data = data;

for (i = 0; i < num; ++i) {
for (uint32_t i = 0; i < num; ++i) {
struct Saved_Friend temp = { 0 };
const uint8_t *next_data = friend_load(&temp, cur_data);
assert(next_data - cur_data == l_friend_size);
Expand Down Expand Up @@ -3200,9 +3188,8 @@ bool messenger_load_state_section(Messenger *m, const uint8_t *data, uint32_t le
uint32_t count_friendlist(const Messenger *m)
{
uint32_t ret = 0;
uint32_t i;

for (i = 0; i < m->numfriends; ++i) {
for (uint32_t i = 0; i < m->numfriends; ++i) {
if (m->friendlist[i].status > 0) {
++ret;
}
Expand All @@ -3226,10 +3213,9 @@ uint32_t copy_friendlist(Messenger const *m, uint32_t *out_list, uint32_t list_s
return 0;
}

uint32_t i;
uint32_t ret = 0;

for (i = 0; i < m->numfriends; ++i) {
for (uint32_t i = 0; i < m->numfriends; ++i) {
if (ret >= list_size) {
break; /* Abandon ship */
}
Expand Down
45 changes: 12 additions & 33 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,11 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c

// TODO(irungentoo): detect and kill bad relays.
// TODO(irungentoo): thread safety?
unsigned int i;
int ret = -1;

bool limit_reached = 0;

for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
uint8_t status = con_to->connections[i].status;
uint8_t connection_id = con_to->connections[i].connection_id;
Expand Down Expand Up @@ -337,7 +336,7 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c
ret = 0;

/* Send oob packets to all relays tied to the connection. */
for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
uint8_t status = con_to->connections[i].status;

Expand Down Expand Up @@ -488,9 +487,7 @@ void set_onion_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_onion_
*/
static int find_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key)
{
unsigned int i;

for (i = 0; i < tcp_c->connections_length; ++i) {
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
TCP_Connection_to *con_to = get_connection(tcp_c, i);

if (con_to) {
Expand Down Expand Up @@ -570,9 +567,7 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number)
return -1;
}

unsigned int i;

for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection) {
unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
Expand Down Expand Up @@ -622,9 +617,7 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,
return -1;
}

unsigned int i;

for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection) {
unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
Expand All @@ -648,9 +641,7 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,
return -1;
}

unsigned int i;

for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection) {
unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
Expand All @@ -671,9 +662,7 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,

static bool tcp_connection_in_conn(TCP_Connection_to *con_to, unsigned int tcp_connections_number)
{
unsigned int i;

for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
return 1;
}
Expand All @@ -687,13 +676,11 @@ static bool tcp_connection_in_conn(TCP_Connection_to *con_to, unsigned int tcp_c
*/
static int add_tcp_connection_to_conn(TCP_Connection_to *con_to, unsigned int tcp_connections_number)
{
unsigned int i;

if (tcp_connection_in_conn(con_to, tcp_connections_number)) {
return -1;
}

for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection == 0) {
con_to->connections[i].tcp_connection = tcp_connections_number + 1;
con_to->connections[i].status = TCP_CONNECTIONS_STATUS_NONE;
Expand All @@ -710,9 +697,7 @@ static int add_tcp_connection_to_conn(TCP_Connection_to *con_to, unsigned int tc
*/
static int rm_tcp_connection_from_conn(TCP_Connection_to *con_to, unsigned int tcp_connections_number)
{
unsigned int i;

for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
con_to->connections[i].tcp_connection = 0;
con_to->connections[i].status = TCP_CONNECTIONS_STATUS_NONE;
Expand Down Expand Up @@ -778,9 +763,7 @@ int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number
return -1;
}

unsigned int i;

for (i = 0; i < tcp_c->connections_length; ++i) {
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
TCP_Connection_to *con_to = get_connection(tcp_c, i);

if (con_to) {
Expand Down Expand Up @@ -821,9 +804,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
return -1;
}

unsigned int i;

for (i = 0; i < tcp_c->connections_length; ++i) {
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
TCP_Connection_to *con_to = get_connection(tcp_c, i);

if (con_to) {
Expand Down Expand Up @@ -867,9 +848,7 @@ static int sleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connection
kill_TCP_connection(tcp_con->connection);
tcp_con->connection = nullptr;

unsigned int i;

for (i = 0; i < tcp_c->connections_length; ++i) {
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
TCP_Connection_to *con_to = get_connection(tcp_c, i);

if (con_to) {
Expand Down
30 changes: 10 additions & 20 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,7 @@ static int kill_accepted(TCP_Server *tcp_server, int index)
return -1;
}

uint32_t i;

for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) {
for (uint32_t i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) {
rm_connection_index(tcp_server, &tcp_server->accepted_connection_array[index], i);
}

Expand Down Expand Up @@ -670,7 +668,6 @@ static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id)
*/
static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const uint8_t *public_key)
{
uint32_t i;
uint32_t index = -1;
TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id];

Expand All @@ -683,7 +680,7 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const
return 0;
}

for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) {
for (uint32_t i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) {
if (con->connections[i].status != 0) {
if (public_key_cmp(public_key, con->connections[i].public_key) == 0) {
if (send_routing_response(con, i + NUM_RESERVED_PORTS, public_key) == -1) {
Expand Down Expand Up @@ -723,7 +720,7 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const
uint32_t other_id = -1;
TCP_Secure_Connection *other_conn = &tcp_server->accepted_connection_array[other_index];

for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) {
for (uint32_t i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) {
if (other_conn->connections[i].status == 1
&& public_key_cmp(other_conn->connections[i].public_key, con->public_key) == 0) {
other_id = i;
Expand Down Expand Up @@ -785,10 +782,9 @@ static int rm_connection_index(TCP_Server *tcp_server, TCP_Secure_Connection *co
}

if (con->connections[con_number].status) {
uint32_t index = con->connections[con_number].index;
uint8_t other_id = con->connections[con_number].other_id;

if (con->connections[con_number].status == 2) {
const uint32_t index = con->connections[con_number].index;
const uint8_t other_id = con->connections[con_number].other_id;

if (index >= tcp_server->size_accepted_connections) {
return -1;
Expand Down Expand Up @@ -1096,16 +1092,13 @@ TCP_Server *new_TCP_server(const Logger *logger, uint8_t ipv6_enabled, uint16_t

const Family family = ipv6_enabled ? net_family_ipv6 : net_family_ipv4;

uint32_t i;
#ifdef TCP_SERVER_USE_EPOLL
struct epoll_event ev;
#endif

for (i = 0; i < num_sockets; ++i) {
for (uint32_t i = 0; i < num_sockets; ++i) {
Socket sock = new_listening_TCP_socket(family, ports[i]);

if (sock_valid(sock)) {
#ifdef TCP_SERVER_USE_EPOLL
struct epoll_event ev;

ev.events = EPOLLIN | EPOLLET;
ev.data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_LISTENING << 32);

Expand Down Expand Up @@ -1142,9 +1135,7 @@ TCP_Server *new_TCP_server(const Logger *logger, uint8_t ipv6_enabled, uint16_t
#ifndef TCP_SERVER_USE_EPOLL
static void do_TCP_accept_new(TCP_Server *tcp_server)
{
uint32_t i;

for (i = 0; i < tcp_server->num_listening_socks; ++i) {
for (uint32_t i = 0; i < tcp_server->num_listening_socks; ++i) {
Socket sock;

do {
Expand Down Expand Up @@ -1266,9 +1257,8 @@ static void do_TCP_confirmed(TCP_Server *tcp_server, const Mono_Time *mono_time)

tcp_server->last_run_pinged = mono_time_get(mono_time);
#endif
uint32_t i;

for (i = 0; i < tcp_server->size_accepted_connections; ++i) {
for (uint32_t i = 0; i < tcp_server->size_accepted_connections; ++i) {
TCP_Secure_Connection *conn = &tcp_server->accepted_connection_array[i];

if (conn->status != TCP_STATUS_CONFIRMED) {
Expand Down
4 changes: 1 addition & 3 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -3051,9 +3051,7 @@ void do_net_crypto(Net_Crypto *c, void *userdata)

void kill_net_crypto(Net_Crypto *c)
{
uint32_t i;

for (i = 0; i < c->crypto_connections_length; ++i) {
for (uint32_t i = 0; i < c->crypto_connections_length; ++i) {
crypto_kill(c, i);
}

Expand Down
4 changes: 1 addition & 3 deletions toxcore/onion_announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ static void generate_ping_id(const Onion_Announce *onion_a, uint64_t time, const
*/
static int in_entries(const Onion_Announce *onion_a, const uint8_t *public_key)
{
unsigned int i;

for (i = 0; i < ONION_ANNOUNCE_MAX_ENTRIES; ++i) {
for (unsigned int i = 0; i < ONION_ANNOUNCE_MAX_ENTRIES; ++i) {
if (!mono_time_is_timeout(onion_a->mono_time, onion_a->entries[i].time, ONION_ANNOUNCE_TIMEOUT)
&& public_key_cmp(onion_a->entries[i].public_key, public_key) == 0) {
return i;
Expand Down
Loading

0 comments on commit e1fd303

Please sign in to comment.