From b2b0d518f4e49c8b8c1f83bec51bc406d127ea6c Mon Sep 17 00:00:00 2001 From: Mike Liao Date: Mon, 6 Mar 2023 03:20:29 +0000 Subject: [PATCH] fixed 3rd argument in memcpy (w/ null terminator) --- src/http_server.cc | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/http_server.cc b/src/http_server.cc index 7079820..e2f3dc3 100644 --- a/src/http_server.cc +++ b/src/http_server.cc @@ -237,8 +237,20 @@ void HttpServer::HandleHttpData(const EventData &raw_request, // Set response to write to client response_string = to_string(http_response, http_request.method() != HttpMethod::HEAD); - memcpy(raw_response->buffer, response_string.c_str(), kMaxBufferSize); - raw_response->length = response_string.length(); + const size_t res_len = response_string.length(); + + // additionally copy the null terminator + if (res_len < kMaxBufferSize) + memcpy(raw_response->buffer, response_string.c_str(), res_len + 1); + // null-terminate the buffer if response cannot fit inside buffer + else + { + memcpy(raw_response->buffer, response_string.c_str(), + kMaxBufferSize - 1); + raw_response->buffer[kMaxBufferSize - 1] = '\0'; + } + + raw_response->length = res_len; } HttpResponse HttpServer::HandleHttpRequest(const HttpRequest &request) {