From 0ec976d1dd2d90a7f7b349b3576550ab1b221355 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Wed, 28 Sep 2022 14:45:45 -0400 Subject: [PATCH] Fix HttpUrlDecode memory issue; write pathParts array --- src/Http.c | 16 ++++++++++++---- src/Matrix.c | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Http.c b/src/Http.c index d45c216..af19cea 100644 --- a/src/Http.c +++ b/src/Http.c @@ -339,20 +339,26 @@ HttpUrlDecode(char *str) if (c == '%') { - if (sscanf(str + 1, "%2X", (unsigned int *) &c) != 1) + unsigned int d; + + str++; + + if (sscanf(str, "%2X", &d) != 1) { /* Decoding error */ free(decoded); return NULL; } - str += 2; - if (!c) + if (!d) { /* Null character given, don't put that in the string. */ - str++; continue; } + + c = (char) d; + + str++; } decoded[i] = c; @@ -361,6 +367,8 @@ HttpUrlDecode(char *str) str++; } + decoded[i] = '\0'; + return decoded; } diff --git a/src/Matrix.c b/src/Matrix.c index 1027584..89bf498 100644 --- a/src/Matrix.c +++ b/src/Matrix.c @@ -24,6 +24,9 @@ #include +#include +#include + #include #include #include @@ -40,11 +43,13 @@ MatrixHttpHandler(HttpServerContext * context, void *argp) char *key; char *val; + size_t i; HashMap *response; char *requestPath; Array *pathParts; + char *pathPart; requestPath = HttpRequestPath(context); @@ -85,8 +90,14 @@ MatrixHttpHandler(HttpServerContext * context, void *argp) } pathParts = ArrayCreate(); + key = requestPath; + while ((pathPart = strtok_r(key, "/", &key))) + { + char *decoded = HttpUrlDecode(pathPart); + ArrayAdd(pathParts, decoded); + } /* TODO: Route requests here */ @@ -97,6 +108,13 @@ MatrixHttpHandler(HttpServerContext * context, void *argp) JsonEncode(response, stream); fprintf(stream, "\n"); + for (i = 0; i < ArraySize(pathParts); i++) + { + free(ArrayGet(pathParts, i)); + } + + ArrayFree(pathParts); + JsonFree(response); finish: