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 (c == '%')
{ {
if (sscanf(str + 1, "%2X", (unsigned int *) &c) != 1) unsigned int d;
str++;
if (sscanf(str, "%2X", &d) != 1)
{ {
/* Decoding error */ /* Decoding error */
free(decoded); free(decoded);
return NULL; return NULL;
} }
str += 2;
if (!c) if (!d)
{ {
/* Null character given, don't put that in the string. */ /* Null character given, don't put that in the string. */
str++;
continue; continue;
} }
c = (char) d;
str++;
} }
decoded[i] = c; decoded[i] = c;
@ -361,6 +367,8 @@ HttpUrlDecode(char *str)
str++; str++;
} }
decoded[i] = '\0';
return decoded; return decoded;
} }

View file

@ -24,6 +24,9 @@
#include <Matrix.h> #include <Matrix.h>
#include <string.h>
#include <stdlib.h>
#include <HttpServer.h> #include <HttpServer.h>
#include <Json.h> #include <Json.h>
#include <Util.h> #include <Util.h>
@ -40,11 +43,13 @@ MatrixHttpHandler(HttpServerContext * context, void *argp)
char *key; char *key;
char *val; char *val;
size_t i;
HashMap *response; HashMap *response;
char *requestPath; char *requestPath;
Array *pathParts; Array *pathParts;
char *pathPart;
requestPath = HttpRequestPath(context); requestPath = HttpRequestPath(context);
@ -85,8 +90,14 @@ MatrixHttpHandler(HttpServerContext * context, void *argp)
} }
pathParts = ArrayCreate(); pathParts = ArrayCreate();
key = requestPath;
while ((pathPart = strtok_r(key, "/", &key)))
{
char *decoded = HttpUrlDecode(pathPart);
ArrayAdd(pathParts, decoded);
}
/* TODO: Route requests here */ /* TODO: Route requests here */
@ -97,6 +108,13 @@ MatrixHttpHandler(HttpServerContext * context, void *argp)
JsonEncode(response, stream); JsonEncode(response, stream);
fprintf(stream, "\n"); fprintf(stream, "\n");
for (i = 0; i < ArraySize(pathParts); i++)
{
free(ArrayGet(pathParts, i));
}
ArrayFree(pathParts);
JsonFree(response); JsonFree(response);
finish: finish: