From 2f439b9fa28f65c66b66b643efdfedbde6565831 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 30 Apr 2023 14:29:30 +0000 Subject: [PATCH] http: Stop memory leak in failure path --- src/http.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/http.c b/src/http.c index 629cbc7..9e29d73 100644 --- a/src/http.c +++ b/src/http.c @@ -473,7 +473,6 @@ static int http__header(struct http_req* req, struct httplex* lex) int http_req_parse(struct http_req* req, const char* input) { - int rc = -1; memset(req, 0, sizeof(*req)); struct httplex lex; @@ -488,17 +487,20 @@ int http_req_parse(struct http_req* req, const char* input) req->header_length = lex.next_pos - input; - rc = 0; + httplex_destroy(&lex); + return 0; + failure: httplex_destroy(&lex); - return rc; + http_req_free(req); + return -1; } void http_req_free(struct http_req* req) { free(req->content_type); - for (size_t i = 0; i < req->field_index; ++i) { + for (size_t i = 0; i < HTTP_FIELD_INDEX_MAX && req->field[i].key; ++i) { free(req->field[i].key); free(req->field[i].value); }