From 4eef30a2a487e15c80b5c07b6b6b0eded634320b Mon Sep 17 00:00:00 2001 From: jgromes Date: Mon, 9 Sep 2019 10:04:07 +0200 Subject: [PATCH] [HTTP] Fixed incorrect string length --- src/protocols/HTTP.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/protocols/HTTP.cpp b/src/protocols/HTTP.cpp index 11b41d57..9200acb0 100644 --- a/src/protocols/HTTP.cpp +++ b/src/protocols/HTTP.cpp @@ -21,7 +21,7 @@ int16_t HTTPClient::get(const char* url, String& response) { char* hostEnd = strchr(hostStart + 1, '/'); host = new char[hostEnd - hostStart]; strncpy(host, hostStart + 1, hostEnd - hostStart - 1); - host[hostEnd - hostStart - 1] = 0x00; + host[hostEnd - hostStart - 1] = '\0'; // find the endpoint string endpoint = new char[url + strlen(url) - hostEnd + 1]; @@ -31,7 +31,7 @@ int16_t HTTPClient::get(const char* url, String& response) { char* hostEnd = strchr(url, '/'); host = new char[hostEnd - url + 1]; strncpy(host, url, hostEnd - url); - host[hostEnd - url] = 0x00; + host[hostEnd - url] = '\0'; // find the endpoint string endpoint = new char[url + strlen(url) - hostEnd + 1]; @@ -39,7 +39,7 @@ int16_t HTTPClient::get(const char* url, String& response) { } // build the GET request - char* request = new char[strlen(endpoint) + strlen(host) + 25]; + char* request = new char[strlen(endpoint) + strlen(host) + 25 + 1]; strcpy(request, "GET "); strcat(request, endpoint); strcat(request, " HTTP/1.1\r\nHost: "); @@ -72,7 +72,7 @@ int16_t HTTPClient::get(const char* url, String& response) { } // read the response - char* raw = new char[numBytes]; + char* raw = new char[numBytes + 1]; size_t rawLength = _tl->receive((uint8_t*)raw, numBytes); if(rawLength == 0) { delete[] raw; @@ -92,9 +92,9 @@ int16_t HTTPClient::get(const char* url, String& response) { delete[] raw; return(ERR_RESPONSE_MALFORMED); } - char* responseStr = new char[raw + rawLength - responseStart - 1]; + char* responseStr = new char[raw + rawLength - responseStart - 1 + 1]; strncpy(responseStr, responseStart + 2, raw + rawLength - responseStart - 1); - responseStr[raw + rawLength - responseStart - 2] = 0x00; + responseStr[raw + rawLength - responseStart - 2] = '\0'; response = String(responseStr); delete[] responseStr; @@ -122,7 +122,7 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response, char* hostEnd = strchr(hostStart + 1, '/'); host = new char[hostEnd - hostStart]; strncpy(host, hostStart + 1, hostEnd - hostStart - 1); - host[hostEnd - hostStart - 1] = 0x00; + host[hostEnd - hostStart - 1] = '\0'; // find the endpoint string endpoint = new char[url + strlen(url) - hostEnd + 1]; @@ -132,7 +132,7 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response, char* hostEnd = strchr(url, '/'); host = new char[hostEnd - url + 1]; strncpy(host, url, hostEnd - url); - host[hostEnd - url] = 0x00; + host[hostEnd - url] = '\0'; // find the endpoint string endpoint = new char[url + strlen(url) - hostEnd + 1]; @@ -142,7 +142,7 @@ int16_t HTTPClient::post(const char* url, const char* content, String& response, // build the POST request char contentLengthStr[8]; itoa(strlen(content), contentLengthStr, 10); - char* request = new char[strlen(endpoint) + strlen(host) + strlen(contentType) + strlen(contentLengthStr) + strlen(content) + 64]; + char* request = new char[strlen(endpoint) + strlen(host) + strlen(contentType) + strlen(contentLengthStr) + strlen(content) + 64 + 1]; strcpy(request, "POST "); strcat(request, endpoint); strcat(request, " HTTP/1.1\r\nHost: ");