Simplify while loops

Change `while` loops into concise `for` loops.

Signed-off-by: Dimitri Papadopoulos <3350651-DimitriPapadopoulos@users.noreply.gitlab.com>
This commit is contained in:
Dimitri Papadopoulos
2025-12-10 14:08:14 +01:00
parent 58321bf626
commit 1f1c96062c
10 changed files with 37 additions and 73 deletions

View File

@@ -288,7 +288,6 @@ static int radius_auth_pass(void *ctx, const char *pass, unsigned int pass_len)
uint32_t service;
char route[72];
char txt[64];
VALUE_PAIR *vp;
int ret;
/* send Access-Request */
@@ -422,9 +421,7 @@ static int radius_auth_pass(void *ctx, const char *pass, unsigned int pass_len)
uint32_t ipv4;
uint8_t ipv6[16];
vp = recvd;
while (vp != NULL) {
for (VALUE_PAIR *vp = recvd; vp != NULL; vp = vp->next) {
if (vp->attribute == PW_SERVICE_TYPE &&
vp->lvalue != PW_FRAMED) {
oc_syslog(
@@ -572,15 +569,12 @@ static int radius_auth_pass(void *ctx, const char *pass, unsigned int pass_len)
#endif
(unsigned int)vp->type);
}
vp = vp->next;
}
ret = 0;
goto cleanup;
} else if (ret == CHALLENGE_RC) {
vp = recvd;
while (vp != NULL) {
for (VALUE_PAIR *vp = recvd; vp != NULL; vp = vp->next) {
if (vp->attribute == PW_STATE &&
vp->type == PW_TYPE_STRING) {
/* State */
@@ -595,7 +589,6 @@ static int radius_auth_pass(void *ctx, const char *pass, unsigned int pass_len)
pctx->passwd_counter, vp->strvalue);
ret = ERR_AUTH_CONTINUE;
}
vp = vp->next;
}
/* PW_STATE or PW_REPLY_MESSAGE is empty or MAX_CHALLENGES limit exceeded */

View File

@@ -401,7 +401,6 @@ int forward_msg(void *pool, int ifd, uint8_t icmd, int ofd, uint8_t ocmd,
struct iovec iov[3];
char data[5];
uint32_t length;
ssize_t left;
uint8_t rcmd;
struct msghdr hdr;
int ret;
@@ -446,9 +445,7 @@ int forward_msg(void *pool, int ifd, uint8_t icmd, int ofd, uint8_t ocmd,
return ERR_BAD_COMMAND;
}
left = length;
while (left > 0) {
for (ssize_t left = length; left > 0; left -= ret) {
char buf[1024];
ret = recv(ifd, buf, sizeof(buf), 0);
@@ -468,8 +465,6 @@ int forward_msg(void *pool, int ifd, uint8_t icmd, int ofd, uint8_t ocmd,
__FILE__, __LINE__, strerror(errno));
return ERR_BAD_COMMAND;
}
left -= ret;
}
return 0;

View File

@@ -71,15 +71,17 @@ static snapshot_entry_t *snapshot_find(struct snapshot_t *snapshot,
{
struct htable_iter iter;
size_t hash = snapshot_hash_filename(filename);
snapshot_entry_t *entry = htable_firstval(&snapshot->ht, &iter, hash);
while (entry != NULL) {
for (snapshot_entry_t *entry =
htable_firstval(&snapshot->ht, &iter, hash);
entry != NULL;
entry = htable_nextval(&snapshot->ht, &iter, hash)) {
if (strcmp(entry->name, filename) == 0) {
break;
return entry;
}
entry = htable_nextval(&snapshot->ht, &iter, hash);
}
return entry;
return NULL;
}
static int snapshot_file_name_from_fd(int fd, char *file_name,
@@ -170,13 +172,12 @@ cleanup:
void snapshot_terminate(struct snapshot_t *snapshot)
{
struct htable_iter iter;
snapshot_entry_t *entry = htable_first(&snapshot->ht, &iter);
while (entry != NULL) {
for (snapshot_entry_t *entry = htable_first(&snapshot->ht, &iter);
entry != NULL; entry = htable_next(&snapshot->ht, &iter)) {
htable_delval(&snapshot->ht, &iter);
close(entry->fd);
talloc_free(entry);
entry = htable_next(&snapshot->ht, &iter);
}
}
@@ -309,10 +310,9 @@ size_t snapshot_entry_count(struct snapshot_t *snapshot)
{
struct htable_iter iter;
size_t count = 0;
snapshot_entry_t *entry = htable_first(&snapshot->ht, &iter);
while (entry != NULL) {
entry = htable_next(&snapshot->ht, &iter);
for (snapshot_entry_t *entry = htable_first(&snapshot->ht, &iter);
entry != NULL; entry = htable_next(&snapshot->ht, &iter)) {
count++;
}

View File

@@ -49,16 +49,13 @@ static void ip_from_seed(uint8_t *seed, unsigned int seed_size, void *ip,
void ip_lease_deinit(struct ip_lease_db_st *db)
{
struct ip_lease_st *cache;
struct htable_iter iter;
cache = htable_first(&db->ht, &iter);
while (cache != NULL) {
for (struct ip_lease_st *cache = htable_first(&db->ht, &iter);
cache != NULL; cache = htable_next(&db->ht, &iter)) {
/* disable the destructor */
cache->db = NULL;
talloc_free(cache);
cache = htable_next(&db->ht, &iter);
}
htable_clear(&db->ht);
}

View File

@@ -97,7 +97,6 @@ void main_ban_db_deinit(main_server_st *s)
unsigned int main_ban_db_elems(main_server_st *s)
{
struct htable *db = s->ban_db;
ban_entry_st *t;
struct htable_iter iter;
time_t now = time(NULL);
unsigned int banned = 0;
@@ -105,12 +104,11 @@ unsigned int main_ban_db_elems(main_server_st *s)
if (db == NULL || GETCONFIG(s)->max_ban_score == 0)
return 0;
t = htable_first(db, &iter);
while (t != NULL) {
for (ban_entry_st *t = htable_first(db, &iter); t != NULL;
t = htable_next(db, &iter)) {
if (t->expires > now && IS_BANNED(s, t)) {
banned++;
}
t = htable_next(db, &iter);
}
return banned;
}
@@ -344,21 +342,19 @@ unsigned int check_if_banned(main_server_st *s, struct sockaddr_storage *addr,
void cleanup_banned_entries(main_server_st *s)
{
struct htable *db = s->ban_db;
ban_entry_st *t;
struct htable_iter iter;
time_t now = time(NULL);
if (db == NULL)
return;
t = htable_first(db, &iter);
while (t != NULL) {
for (ban_entry_st *t = htable_first(db, &iter); t != NULL;
t = htable_next(db, &iter)) {
if (now >= t->expires &&
now > t->last_reset + GETCONFIG(s)->ban_reset_time) {
htable_delval(db, &iter);
talloc_free(t);
}
t = htable_next(db, &iter);
}
}

View File

@@ -546,7 +546,6 @@ static void method_list_banned(method_ctx *ctx, int cfd, uint8_t *msg,
unsigned int msg_size)
{
BanListRep rep = BAN_LIST_REP__INIT;
struct ban_entry_st *e = NULL;
struct htable *db = ctx->s->ban_db;
int ret;
struct htable_iter iter;
@@ -554,15 +553,14 @@ static void method_list_banned(method_ctx *ctx, int cfd, uint8_t *msg,
mslog(ctx->s, NULL, LOG_DEBUG, "ctl: list-banned-ips");
e = htable_first(db, &iter);
while (e != NULL) {
for (struct ban_entry_st *e = htable_first(db, &iter); e != NULL;
e = htable_next(db, &iter)) {
ret = append_ban_info(ctx, &rep, e, now);
if (ret < 0) {
mslog(ctx->s, NULL, LOG_ERR,
"error appending ban info to reply");
return;
}
e = htable_next(db, &iter);
}
ret = send_msg(ctx->pool, cfd, CTL_CMD_LIST_BANNED_REP, &rep,

View File

@@ -46,7 +46,6 @@ void handle_secm_list_cookies_reply(void *pool, int fd, sec_mod_st *sec)
{
SecmListCookiesReplyMsg msg = SECM_LIST_COOKIES_REPLY_MSG__INIT;
struct htable *db = sec->client_db;
client_entry_st *t;
struct htable_iter iter;
CookieIntMsg *cookies;
int ret;
@@ -71,10 +70,10 @@ void handle_secm_list_cookies_reply(void *pool, int fd, sec_mod_st *sec)
return;
}
t = htable_first(db, &iter);
while (t != NULL) {
for (client_entry_st *t = htable_first(db, &iter); t != NULL;
t = htable_next(db, &iter)) {
if IS_CLIENT_ENTRY_EXPIRED (sec, t, now)
goto cont;
continue;
if (msg.n_cookies >= db->elems)
break;
@@ -108,9 +107,6 @@ void handle_secm_list_cookies_reply(void *pool, int fd, sec_mod_st *sec)
msg.cookies[msg.n_cookies] = &cookies[msg.n_cookies];
msg.n_cookies++;
cont:
t = htable_next(db, &iter);
}
ret = send_msg(

View File

@@ -178,17 +178,15 @@ static void clean_entry(sec_mod_st *sec, client_entry_st *e)
void cleanup_client_entries(sec_mod_st *sec)
{
struct htable *db = sec->client_db;
client_entry_st *t;
struct htable_iter iter;
time_t now = time(NULL);
t = htable_first(db, &iter);
while (t != NULL) {
for (client_entry_st *t = htable_first(db, &iter); t != NULL;
t = htable_next(db, &iter)) {
if IS_CLIENT_ENTRY_EXPIRED_FULL (sec, t, now, 1) {
htable_delval(db, &iter);
clean_entry(sec, t);
}
t = htable_next(db, &iter);
}
}

View File

@@ -41,14 +41,14 @@
int handle_resume_delete_req(sec_mod_st *sec, const SessionResumeFetchMsg *req)
{
tls_cache_st *cache;
struct htable_iter iter;
size_t key;
key = hash_any(req->session_id.data, req->session_id.len, 0);
cache = htable_firstval(sec->tls_db.ht, &iter, key);
while (cache != NULL) {
for (tls_cache_st *cache = htable_firstval(sec->tls_db.ht, &iter, key);
cache != NULL;
cache = htable_nextval(sec->tls_db.ht, &iter, key)) {
if (req->session_id.len == cache->session_id_size &&
memcmp(req->session_id.data, cache->session_id,
req->session_id.len) == 0) {
@@ -60,8 +60,6 @@ int handle_resume_delete_req(sec_mod_st *sec, const SessionResumeFetchMsg *req)
sec->tls_db.entries--;
return 0;
}
cache = htable_nextval(sec->tls_db.ht, &iter, key);
}
return 0;
@@ -70,7 +68,6 @@ int handle_resume_delete_req(sec_mod_st *sec, const SessionResumeFetchMsg *req)
int handle_resume_fetch_req(sec_mod_st *sec, const SessionResumeFetchMsg *req,
SessionResumeReplyMsg *rep)
{
tls_cache_st *cache;
struct htable_iter iter;
size_t key;
@@ -78,8 +75,9 @@ int handle_resume_fetch_req(sec_mod_st *sec, const SessionResumeFetchMsg *req,
key = hash_any(req->session_id.data, req->session_id.len, 0);
cache = htable_firstval(sec->tls_db.ht, &iter, key);
while (cache != NULL) {
for (tls_cache_st *cache = htable_firstval(sec->tls_db.ht, &iter, key);
cache != NULL;
cache = htable_nextval(sec->tls_db.ht, &iter, key)) {
if (req->session_id.len == cache->session_id_size &&
memcmp(req->session_id.data, cache->session_id,
req->session_id.len) == 0) {
@@ -110,8 +108,6 @@ int handle_resume_fetch_req(sec_mod_st *sec, const SessionResumeFetchMsg *req,
return 0;
}
}
cache = htable_nextval(sec->tls_db.ht, &iter, key);
}
return 0;
@@ -178,14 +174,13 @@ int handle_resume_store_req(sec_mod_st *sec,
void expire_tls_sessions(sec_mod_st *sec)
{
tls_cache_st *cache;
struct htable_iter iter;
time_t now, exp;
now = time(NULL);
cache = htable_first(sec->tls_db.ht, &iter);
while (cache != NULL) {
for (tls_cache_st *cache = htable_first(sec->tls_db.ht, &iter);
cache != NULL; cache = htable_next(sec->tls_db.ht, &iter)) {
gnutls_datum_t d;
d.data = (void *)cache->session_data;
@@ -203,6 +198,5 @@ void expire_tls_sessions(sec_mod_st *sec)
talloc_free(cache);
sec->tls_db.entries--;
}
cache = htable_next(sec->tls_db.ht, &iter);
}
}

View File

@@ -421,11 +421,10 @@ void tls_cache_init(void *pool, tls_sess_db_st *db)
void tls_cache_deinit(tls_sess_db_st *db)
{
tls_cache_st *cache;
struct htable_iter iter;
cache = htable_first(db->ht, &iter);
while (cache != NULL) {
for (tls_cache_st *cache = htable_first(db->ht, &iter); cache != NULL;
cache = htable_next(db->ht, &iter)) {
if (cache->session_data_size > 0) {
safe_memset(cache->session_data, 0,
cache->session_data_size);
@@ -433,8 +432,6 @@ void tls_cache_deinit(tls_sess_db_st *db)
cache->session_id_size = 0;
}
talloc_free(cache);
cache = htable_next(db->ht, &iter);
}
htable_clear(db->ht);
db->entries = 0;