Fix HttpUrlDecode memory issue; write pathParts array

This commit is contained in:
Jordan Bancino 2022-09-28 14:45:45 -04:00
parent acffd82b48
commit 0ec976d1dd
2 changed files with 30 additions and 4 deletions

View file

@ -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;
}

View file

@ -24,6 +24,9 @@
#include <Matrix.h>
#include <string.h>
#include <stdlib.h>
#include <HttpServer.h>
#include <Json.h>
#include <Util.h>
@ -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: