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 8cd4827
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 140 deletions.
24 changes: 12 additions & 12 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ branches:
protection:
required_status_checks:
contexts:
- build-bootstrapd-docker
- build-compcert
- build-macos
- build-nacl
- build-tcc
- build-win32
- build-win64
- CodeFactor
- "bazel-release"
- "build-bootstrapd-docker"
- "build-compcert"
- "build-macos"
- "build-nacl"
- "build-tcc"
- "build-win32"
- "build-win64"
- "CodeFactor"
- "ci/circleci: asan"
- "ci/circleci: clang-tidy"
- "ci/circleci: infer"
- "ci/circleci: static-analysis"
- "ci/circleci: tsan"
- cimple
- cirrus-ci
- code-review/reviewable
- continuous-integration/appveyor/pr
- "cimple"
- "code-review/reviewable"
- "continuous-integration/appveyor/pr"

# Labels specific to c-toxcore.
labels:
Expand Down
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
Loading

0 comments on commit 8cd4827

Please sign in to comment.