From e4ad0c833c644046e87be5f2aa105500463f3915 Mon Sep 17 00:00:00 2001 From: iphydf Date: Thu, 9 Dec 2021 13:38:28 +0000 Subject: [PATCH] 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). --- toxav/audio.c | 7 ++++--- toxav/groupav.c | 4 +++- toxav/msi.c | 4 +--- toxav/ring_buffer.c | 4 ++-- toxav/toxav.c | 4 +--- toxcore/crypto_core.c | 6 ++---- toxcore/group.c | 31 ++++++++++++++++++------------- 7 files changed, 31 insertions(+), 29 deletions(-) diff --git a/toxav/audio.c b/toxav/audio.c index e38cc823..8c4e3c14 100644 --- a/toxav/audio.c +++ b/toxav/audio.c @@ -137,9 +137,8 @@ void ac_iterate(ACSession *ac) struct JitterBuffer *const j_buf = (struct JitterBuffer *)ac->j_buf; 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); if (rc == 2) { @@ -297,11 +296,13 @@ static struct JitterBuffer *jbuf_new(uint32_t capacity) } 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]) { free(q->queue[q->bottom % q->size]); q->queue[q->bottom % q->size] = nullptr; } + + ++q->bottom; } } static void jbuf_free(struct JitterBuffer *q) diff --git a/toxav/groupav.c b/toxav/groupav.c index 3bc2649a..211d9261 100644 --- a/toxav/groupav.c +++ b/toxav/groupav.c @@ -61,11 +61,13 @@ static Group_JitterBuffer *create_queue(unsigned int capacity) 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]) { free(q->queue[q->bottom % q->size]); q->queue[q->bottom % q->size] = nullptr; } + + ++q->bottom; } } diff --git a/toxav/msi.c b/toxav/msi.c index 4772ac8a..d06c853c 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -545,9 +545,7 @@ static MSICall *new_call(MSISession *session, uint32_t friend_number) session->calls = tmp; /* Set fields in between to null */ - uint32_t i = session->calls_tail + 1; - - for (; i < friend_number; ++i) { + for (uint32_t i = session->calls_tail + 1; i < friend_number; ++i) { session->calls[i] = nullptr; } diff --git a/toxav/ring_buffer.c b/toxav/ring_buffer.c index e8019f6f..432ee8e5 100644 --- a/toxav/ring_buffer.c +++ b/toxav/ring_buffer.c @@ -105,9 +105,9 @@ uint16_t rb_size(const RingBuffer *b) 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]; } diff --git a/toxav/toxav.c b/toxav/toxav.c index 298bb813..eba92f9f 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -242,9 +242,7 @@ void toxav_iterate(ToxAV *av) uint64_t start = current_time_monotonic(av->toxav_mono_time); int32_t rc = 500; - ToxAVCall *i = av->calls[av->calls_head]; - - for (; i; i = i->next) { + for (ToxAVCall *i = av->calls[av->calls_head]; i; i = i->next) { if (i->active) { pthread_mutex_lock(i->toxav_call_mutex); pthread_mutex_unlock(av->mutex); diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 3cc42174..c66c5329 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -236,10 +236,9 @@ void increment_nonce(uint8_t *nonce) * that loop bounds and their potential underflow or overflow * 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; - for (; i != 0; --i) { + for (uint32_t i = crypto_box_NONCEBYTES; i != 0; --i) { carry += (uint_fast16_t)nonce[i - 1]; nonce[i - 1] = (uint8_t)carry; 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 - 1] = increment; - uint32_t i = crypto_box_NONCEBYTES; 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]; nonce[i - 1] = (uint8_t)carry; carry >>= 8; diff --git a/toxcore/group.c b/toxcore/group.c index 4b331f98..20251d70 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -2193,7 +2193,7 @@ static unsigned int send_peers(Group_Chats *g_c, const Group_c *g, int friendcon uint16_t sent = 0; - for (uint32_t i = 0;; ++i) { + for (uint32_t i = 0; i <= g->numpeers; ++i) { if (i == g->numpeers || (p - response_packet) + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1 + g->group[i].nick_len > 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. */ 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) { - if (g->connections[j].type != GROUPCHAT_CONNECTION_NONE) { - g->connections[i] = g->connections[j]; - ++i; + for (uint16_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) { + if (g->connections[i].type != GROUPCHAT_CONNECTION_NONE) { + g->connections[num_connected] = g->connections[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; } } #define MIN_EMPTY_CONNECTIONS (1 + MAX_GROUP_CONNECTIONS / 10) -/* 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) +static uint16_t empty_connection_count(Group_c *g) { 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 // that prefer non-online connections, and given that prefer earlier // slots.