From 805630dbfcd409a5d49bc89102f4183b71f713f9 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Wed, 8 Oct 2025 01:59:05 +0200 Subject: Use free function pointer for chunk/step libweb now supports deallocating user-defined data whenever an error occurs during a chunked transfer or an asynchronous HTTP response, thus avoiding memory leaks. --- libweb | 2 +- main.c | 15 +++++++-------- zip.c | 10 +++------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/libweb b/libweb index b80741b..e85d90f 160000 --- a/libweb +++ b/libweb @@ -1 +1 @@ -Subproject commit b80741b3e30444b30514b3a4432f40294034c4f9 +Subproject commit e85d90fbf37cbd5a3264a2debd9b51438836c729 diff --git a/main.c b/main.c index 7a367b0..436c50d 100644 --- a/main.c +++ b/main.c @@ -711,8 +711,10 @@ failure: return -1; } -static void free_search(struct search *const s) +static void free_search(void *const p) { + struct search *const s = p; + if (!s) return; @@ -735,7 +737,7 @@ static int search_step(const struct http_payload *const p, if (page_search(r, &s->search)) { fprintf(stderr, "%s: page_search failed\n", __func__); - goto failure; + return -1; } free_search(s); @@ -743,17 +745,13 @@ static int search_step(const struct http_payload *const p, case CFTW_FATAL: fprintf(stderr, "%s: cftw_step failed\n", __func__); - goto failure; + return -1; case CFTW_AGAIN: break; } return 0; - -failure: - free_search(s); - return -1; } static int search(const struct http_payload *const p, @@ -822,7 +820,8 @@ static int search(const struct http_payload *const p, *r = (const struct http_response) { .step.payload = search_step, - .args = s + .args = s, + .free = free_search }; s->search.root = s->root.str; diff --git a/zip.c b/zip.c index 704bf36..7e12c73 100644 --- a/zip.c +++ b/zip.c @@ -60,7 +60,6 @@ static int dump_final(void *const buf, const size_t n, bool *const done, { if (errno == EAGAIN || errno == EWOULDBLOCK) { - free_zip(z); *done = true; return 0; } @@ -251,12 +250,8 @@ static int step(void *const buf, const size_t n, bool *const done, void *const user, void *const args) { struct zip *const z = args; - const int ret = z->next(buf, n, done, user, z); - if (ret < 0) - free_zip(z); - - return ret; + return z->next(buf, n, done, user, z); } int zip(const char *const dir, struct http_response *const r) @@ -326,7 +321,8 @@ int zip(const char *const dir, struct http_response *const r) { .status = HTTP_STATUS_OK, .chunk = step, - .args = z + .args = z, + .free = free_zip }; if (http_response_add_header(r, "Content-Type", "application/zip") -- cgit v1.2.3