From 876e5a7de4b02a498b3d73cf092ba849bf7faced Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 5 Dec 2023 20:21:28 +0100 Subject: [PATCH] fix(lnm): some small bugs --- lnm/src/http/lnm_http_loop_process.c | 5 +++-- lnm/src/loop/lnm_loop.c | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lnm/src/http/lnm_http_loop_process.c b/lnm/src/http/lnm_http_loop_process.c index 8122058..791393e 100644 --- a/lnm/src/http/lnm_http_loop_process.c +++ b/lnm/src/http/lnm_http_loop_process.c @@ -275,9 +275,10 @@ void lnm_http_loop_process_finish(lnm_http_conn *conn) { return; } - lnm_http_loop_ctx_reset(conn->ctx); + lnm_http_loop_ctx *ctx = conn->ctx; + lnm_http_loop_ctx_reset(ctx); - conn->state = lnm_loop_state_req; + ctx->state = lnm_http_loop_state_parse_req; } void (*process_fns[])(lnm_http_conn *conn) = { diff --git a/lnm/src/loop/lnm_loop.c b/lnm/src/loop/lnm_loop.c index f44c829..a412b23 100644 --- a/lnm/src/loop/lnm_loop.c +++ b/lnm/src/loop/lnm_loop.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "lnm/common.h" #include "lnm/loop_internal.h" @@ -43,10 +44,9 @@ lnm_err lnm_loop_accept(lnm_loop *l) { // Append connection to list of connections if ((size_t)conn_fd >= l->conns.len) { - lnm_loop_conn **new = - l->conns.len == 0 - ? calloc(sizeof(lnm_loop_conn *), conn_fd + 1) - : realloc(l->conns.arr, sizeof(lnm_loop_conn *) * (conn_fd + 1)); + // We always calloc as a realloc might introduce unitialized values in the + // array + lnm_loop_conn **new = calloc(sizeof(lnm_loop_conn *), conn_fd + 1); if (new == NULL) { close(conn_fd); @@ -54,6 +54,11 @@ lnm_err lnm_loop_accept(lnm_loop *l) { return lnm_err_failed_alloc; } + if (l->conns.len > 0) { + memcpy(new, l->conns.arr, l->conns.len * sizeof(lnm_loop_conn *)); + free(l->conns.arr); + } + l->conns.arr = new; l->conns.len = conn_fd + 1; }