From e5ddc178968b1f1babf70d77507f37936099b193 Mon Sep 17 00:00:00 2001 From: Jason_000 Date: Wed, 1 Nov 2023 22:56:03 +0000 Subject: [PATCH] Correct memory count --- src/optionals/http/http.c | 6 +++++- src/optionals/http/http.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/optionals/http/http.c b/src/optionals/http/http.c index e2255d65..2930d106 100644 --- a/src/optionals/http/http.c +++ b/src/optionals/http/http.c @@ -243,16 +243,20 @@ static void createResponse(DictuVM *vm, Response *response) { response->len = 0; response->res = NULL; + response->firstIteration = true; } static size_t writeResponse(char *ptr, size_t size, size_t nmemb, void *data) { Response *response = (Response *) data; size_t new_len = response->len + size * nmemb; - response->res = GROW_ARRAY(response->vm, response->res, char, response->len, new_len + 1); + response->res = GROW_ARRAY(response->vm, response->res, char, response->len + !response->firstIteration, new_len + 1); + response->firstIteration = false; + if (response->res == NULL) { printf("Unable to allocate memory\n"); exit(71); } + memcpy(response->res + response->len, ptr, size * nmemb); response->res[new_len] = '\0'; response->len = new_len; diff --git a/src/optionals/http/http.h b/src/optionals/http/http.h index fe13633d..57c860e9 100644 --- a/src/optionals/http/http.h +++ b/src/optionals/http/http.h @@ -14,6 +14,7 @@ typedef struct response { char *res; size_t len; long statusCode; + bool firstIteration; } Response; Value createHTTPModule(DictuVM *vm);