From 613655767b30c315e70b8c093a82d01361b422f6 Mon Sep 17 00:00:00 2001 From: cpq Date: Tue, 1 Nov 2022 21:01:03 +0000 Subject: [PATCH] Housekeeping at the end of c->label --- mongoose.c | 9 +++++++-- src/http.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/mongoose.c b/mongoose.c index f0c3d0cd..3de97644 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1695,7 +1695,9 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data, if (ev == MG_EV_WRITE || ev == MG_EV_POLL) { struct mg_fd *fd = (struct mg_fd *) fn_data; // Read to send IO buffer directly, avoid extra on-stack buffer - size_t n, max = MG_IO_SIZE, space, *cl = (size_t *) c->label; + size_t n, max = MG_IO_SIZE, space; + size_t *cl = (size_t *) &c->label[(sizeof(c->label) - sizeof(size_t)) / + sizeof(size_t) * sizeof(size_t)]; if (c->send.size < max) mg_iobuf_resize(&c->send, max); if (c->send.len >= c->send.size) return; // Rate limit if ((space = c->send.size - c->send.len) > *cl) space = *cl; @@ -1866,9 +1868,12 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, c->is_resp = 0; mg_fs_close(fd); } else { + // Track to-be-sent content length at the end of c->label, aligned + size_t *clp = (size_t *) &c->label[(sizeof(c->label) - sizeof(size_t)) / + sizeof(size_t) * sizeof(size_t)]; c->pfn = static_cb; c->pfn_data = fd; - *(size_t *) c->label = (size_t) cl; // Track to-be-sent content length + *clp = (size_t) cl; } } } diff --git a/src/http.c b/src/http.c index b46ebfe4..f01dbac4 100644 --- a/src/http.c +++ b/src/http.c @@ -355,7 +355,9 @@ static void static_cb(struct mg_connection *c, int ev, void *ev_data, if (ev == MG_EV_WRITE || ev == MG_EV_POLL) { struct mg_fd *fd = (struct mg_fd *) fn_data; // Read to send IO buffer directly, avoid extra on-stack buffer - size_t n, max = MG_IO_SIZE, space, *cl = (size_t *) c->label; + size_t n, max = MG_IO_SIZE, space; + size_t *cl = (size_t *) &c->label[(sizeof(c->label) - sizeof(size_t)) / + sizeof(size_t) * sizeof(size_t)]; if (c->send.size < max) mg_iobuf_resize(&c->send, max); if (c->send.len >= c->send.size) return; // Rate limit if ((space = c->send.size - c->send.len) > *cl) space = *cl; @@ -526,9 +528,12 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, c->is_resp = 0; mg_fs_close(fd); } else { + // Track to-be-sent content length at the end of c->label, aligned + size_t *clp = (size_t *) &c->label[(sizeof(c->label) - sizeof(size_t)) / + sizeof(size_t) * sizeof(size_t)]; c->pfn = static_cb; c->pfn_data = fd; - *(size_t *) c->label = (size_t) cl; // Track to-be-sent content length + *clp = (size_t) cl; } } }