cleanup: Enforce for loop consistency.

All for-loops must have an init-decl, a condition, and an increment
statement. Any loop that doesn't have one of these should be a while
loop (only 2 of these exist in toxav, none in toxcore).
This commit is contained in:
iphydf 2021-12-09 13:38:28 +00:00
parent fa359091c7
commit e4ad0c833c
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
7 changed files with 31 additions and 29 deletions

View File

@ -137,9 +137,8 @@ void ac_iterate(ACSession *ac)
struct JitterBuffer *const j_buf = (struct JitterBuffer *)ac->j_buf; struct JitterBuffer *const j_buf = (struct JitterBuffer *)ac->j_buf;
int rc = 0; int rc = 0;
struct RTPMessage *msg = jbuf_read(j_buf, &rc);
for (; msg != nullptr || rc == 2; msg = jbuf_read(j_buf, &rc)) { for (struct RTPMessage *msg = jbuf_read(j_buf, &rc); msg != nullptr || rc == 2; msg = jbuf_read(j_buf, &rc)) {
pthread_mutex_unlock(ac->queue_mutex); pthread_mutex_unlock(ac->queue_mutex);
if (rc == 2) { if (rc == 2) {
@ -297,11 +296,13 @@ static struct JitterBuffer *jbuf_new(uint32_t capacity)
} }
static void jbuf_clear(struct JitterBuffer *q) static void jbuf_clear(struct JitterBuffer *q)
{ {
for (; q->bottom != q->top; ++q->bottom) { while (q->bottom != q->top) {
if (q->queue[q->bottom % q->size]) { if (q->queue[q->bottom % q->size]) {
free(q->queue[q->bottom % q->size]); free(q->queue[q->bottom % q->size]);
q->queue[q->bottom % q->size] = nullptr; q->queue[q->bottom % q->size] = nullptr;
} }
++q->bottom;
} }
} }
static void jbuf_free(struct JitterBuffer *q) static void jbuf_free(struct JitterBuffer *q)

View File

@ -61,11 +61,13 @@ static Group_JitterBuffer *create_queue(unsigned int capacity)
static void clear_queue(Group_JitterBuffer *q) static void clear_queue(Group_JitterBuffer *q)
{ {
for (; q->bottom != q->top; ++q->bottom) { while (q->bottom != q->top) {
if (q->queue[q->bottom % q->size]) { if (q->queue[q->bottom % q->size]) {
free(q->queue[q->bottom % q->size]); free(q->queue[q->bottom % q->size]);
q->queue[q->bottom % q->size] = nullptr; q->queue[q->bottom % q->size] = nullptr;
} }
++q->bottom;
} }
} }

View File

@ -545,9 +545,7 @@ static MSICall *new_call(MSISession *session, uint32_t friend_number)
session->calls = tmp; session->calls = tmp;
/* Set fields in between to null */ /* Set fields in between to null */
uint32_t i = session->calls_tail + 1; for (uint32_t i = session->calls_tail + 1; i < friend_number; ++i) {
for (; i < friend_number; ++i) {
session->calls[i] = nullptr; session->calls[i] = nullptr;
} }

View File

@ -105,9 +105,9 @@ uint16_t rb_size(const RingBuffer *b)
uint16_t rb_data(const RingBuffer *b, void **dest) uint16_t rb_data(const RingBuffer *b, void **dest)
{ {
uint16_t i = 0; uint16_t i;
for (; i < rb_size(b); ++i) { for (i = 0; i < rb_size(b); ++i) {
dest[i] = b->data[(b->start + i) % b->size]; dest[i] = b->data[(b->start + i) % b->size];
} }

View File

@ -242,9 +242,7 @@ void toxav_iterate(ToxAV *av)
uint64_t start = current_time_monotonic(av->toxav_mono_time); uint64_t start = current_time_monotonic(av->toxav_mono_time);
int32_t rc = 500; int32_t rc = 500;
ToxAVCall *i = av->calls[av->calls_head]; for (ToxAVCall *i = av->calls[av->calls_head]; i; i = i->next) {
for (; i; i = i->next) {
if (i->active) { if (i->active) {
pthread_mutex_lock(i->toxav_call_mutex); pthread_mutex_lock(i->toxav_call_mutex);
pthread_mutex_unlock(av->mutex); pthread_mutex_unlock(av->mutex);

View File

@ -236,10 +236,9 @@ void increment_nonce(uint8_t *nonce)
* that loop bounds and their potential underflow or overflow * that loop bounds and their potential underflow or overflow
* are independent of user-controlled input (you may have heard of the Heartbleed bug). * are independent of user-controlled input (you may have heard of the Heartbleed bug).
*/ */
uint32_t i = crypto_box_NONCEBYTES;
uint_fast16_t carry = 1U; uint_fast16_t carry = 1U;
for (; i != 0; --i) { for (uint32_t i = crypto_box_NONCEBYTES; i != 0; --i) {
carry += (uint_fast16_t)nonce[i - 1]; carry += (uint_fast16_t)nonce[i - 1];
nonce[i - 1] = (uint8_t)carry; nonce[i - 1] = (uint8_t)carry;
carry >>= 8; carry >>= 8;
@ -260,10 +259,9 @@ void increment_nonce_number(uint8_t *nonce, uint32_t increment)
num_as_nonce[crypto_box_NONCEBYTES - 2] = increment >> 8; num_as_nonce[crypto_box_NONCEBYTES - 2] = increment >> 8;
num_as_nonce[crypto_box_NONCEBYTES - 1] = increment; num_as_nonce[crypto_box_NONCEBYTES - 1] = increment;
uint32_t i = crypto_box_NONCEBYTES;
uint_fast16_t carry = 0U; uint_fast16_t carry = 0U;
for (; i != 0; --i) { for (uint32_t i = crypto_box_NONCEBYTES; i != 0; --i) {
carry += (uint_fast16_t)nonce[i - 1] + (uint_fast16_t)num_as_nonce[i - 1]; carry += (uint_fast16_t)nonce[i - 1] + (uint_fast16_t)num_as_nonce[i - 1];
nonce[i - 1] = (uint8_t)carry; nonce[i - 1] = (uint8_t)carry;
carry >>= 8; carry >>= 8;

View File

@ -2193,7 +2193,7 @@ static unsigned int send_peers(Group_Chats *g_c, const Group_c *g, int friendcon
uint16_t sent = 0; uint16_t sent = 0;
for (uint32_t i = 0;; ++i) { for (uint32_t i = 0; i <= g->numpeers; ++i) {
if (i == g->numpeers if (i == g->numpeers
|| (p - response_packet) + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1 + g->group[i].nick_len > || (p - response_packet) + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1 + g->group[i].nick_len >
sizeof(response_packet)) { sizeof(response_packet)) {
@ -3073,27 +3073,23 @@ static bool groupchat_freeze_timedout(Group_Chats *g_c, uint32_t groupnumber, vo
/* Push non-empty slots to start. */ /* Push non-empty slots to start. */
static void squash_connections(Group_c *g) static void squash_connections(Group_c *g)
{ {
uint16_t i = 0; uint16_t num_connected = 0;
for (uint16_t j = 0; j < MAX_GROUP_CONNECTIONS; ++j) { for (uint16_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[j].type != GROUPCHAT_CONNECTION_NONE) { if (g->connections[i].type != GROUPCHAT_CONNECTION_NONE) {
g->connections[i] = g->connections[j]; g->connections[num_connected] = g->connections[i];
++i; ++num_connected;
} }
} }
for (; i < MAX_GROUP_CONNECTIONS; ++i) { for (uint16_t i = num_connected; i < MAX_GROUP_CONNECTIONS; ++i) {
g->connections[i].type = GROUPCHAT_CONNECTION_NONE; g->connections[i].type = GROUPCHAT_CONNECTION_NONE;
} }
} }
#define MIN_EMPTY_CONNECTIONS (1 + MAX_GROUP_CONNECTIONS / 10) #define MIN_EMPTY_CONNECTIONS (1 + MAX_GROUP_CONNECTIONS / 10)
/* Remove old connections as necessary to ensure we have space for new static uint16_t empty_connection_count(Group_c *g)
* connections. This invalidates connections array indices (which is
* why we do this periodically rather than on adding a connection).
*/
static void clean_connections(Group_Chats *g_c, Group_c *g)
{ {
uint16_t to_clear = MIN_EMPTY_CONNECTIONS; uint16_t to_clear = MIN_EMPTY_CONNECTIONS;
@ -3107,7 +3103,16 @@ static void clean_connections(Group_Chats *g_c, Group_c *g)
} }
} }
for (; to_clear > 0; --to_clear) { return to_clear;
}
/* Remove old connections as necessary to ensure we have space for new
* connections. This invalidates connections array indices (which is
* why we do this periodically rather than on adding a connection).
*/
static void clean_connections(Group_Chats *g_c, Group_c *g)
{
for (uint16_t to_clear = empty_connection_count(g); to_clear > 0; --to_clear) {
// Remove a connection. Prefer non-closest connections, and given // Remove a connection. Prefer non-closest connections, and given
// that prefer non-online connections, and given that prefer earlier // that prefer non-online connections, and given that prefer earlier
// slots. // slots.