From 39aad94ae4116222230deb9c7f89d28d0f58bc60 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 9 May 2022 16:58:10 -0400 Subject: [PATCH] refactor: Better handle non-null terminated string --- runtime/src/http_parser_settings.c | 20 ++++++++++++-------- tests/html/Makefile | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/runtime/src/http_parser_settings.c b/runtime/src/http_parser_settings.c index 0a0adb7..4ff8bf5 100644 --- a/runtime/src/http_parser_settings.c +++ b/runtime/src/http_parser_settings.c @@ -38,10 +38,10 @@ http_parser_settings_on_url(http_parser *parser, const char *at, size_t length) /* Full URL excludes query params if present */ size_t full_url_length = query_params == NULL ? length : query_params - at; - size_t to_copy = full_url_length < HTTP_MAX_FULL_URL_LENGTH - 1 ? full_url_length - : HTTP_MAX_FULL_URL_LENGTH - 1; - strncpy(http_request->full_url, at, to_copy); - http_request->full_url[to_copy] = '\0'; + size_t size_to_copy = full_url_length < HTTP_MAX_FULL_URL_LENGTH - 1 ? full_url_length + : HTTP_MAX_FULL_URL_LENGTH - 1; + memcpy(http_request->full_url, at, size_to_copy); + http_request->full_url[size_to_copy] = '\0'; if (query_params != NULL) { char *prev = query_params + 1; @@ -53,8 +53,10 @@ http_parser_settings_on_url(http_parser *parser, const char *at, size_t length) http_request->query_params[http_request->query_params_count].value_length = len < HTTP_MAX_QUERY_PARAM_LENGTH - 1 ? len : HTTP_MAX_QUERY_PARAM_LENGTH - 1; - strncpy(http_request->query_params[http_request->query_params_count].value, prev, - http_request->query_params[http_request->query_params_count].value_length); + memcpy(http_request->query_params[http_request->query_params_count].value, prev, + http_request->query_params[http_request->query_params_count].value_length); + http_request->query_params[http_request->query_params_count] + .value[http_request->query_params[http_request->query_params_count].value_length] = '\0'; http_request->query_params_count++; prev = cur; @@ -64,8 +66,10 @@ http_parser_settings_on_url(http_parser *parser, const char *at, size_t length) http_request->query_params[http_request->query_params_count].value_length = len < HTTP_MAX_QUERY_PARAM_LENGTH - 1 ? len : HTTP_MAX_QUERY_PARAM_LENGTH - 1; - strncpy(http_request->query_params[http_request->query_params_count].value, prev, - http_request->query_params[http_request->query_params_count].value_length); + memcpy(http_request->query_params[http_request->query_params_count].value, prev, + http_request->query_params[http_request->query_params_count].value_length); + http_request->query_params[http_request->query_params_count] + .value[http_request->query_params[http_request->query_params_count].value_length] = '\0'; http_request->query_params_count++; } diff --git a/tests/html/Makefile b/tests/html/Makefile index 388f666..22aa7e5 100644 --- a/tests/html/Makefile +++ b/tests/html/Makefile @@ -34,13 +34,13 @@ debug: sledgert html --eval-command="run spec.json" client: - http :1337 + http :1337/index.html browser-args: xdg-open "http://localhost:1337/index.html" client-stdin: - echo "Example STDIN" | http :1337 + echo "Example STDIN" | http :1337/index.html client-args: http ":1337/index.html?firstArg&secondArg&thirdArg"