From ed4807118e0083c1cced83b9cabc2a31e174a318 Mon Sep 17 00:00:00 2001 From: abolfazl Date: Mon, 29 Jun 2026 19:04:18 +0330 Subject: [PATCH 1/6] First Commit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ff98a9eae..1ac27e6ba 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# FujiNet +# FujiNet afdsssssssssss A multi-function peripheral built on ESP32 hardware being developed for the multiple 8-bit systems From ee670ea8ea9b0aa923b9d4d688ddba0f9f4cbb16 Mon Sep 17 00:00:00 2001 From: yazdan-iot Date: Tue, 30 Jun 2026 15:50:55 +0330 Subject: [PATCH 2/6] Fix: check malloc() result in fnHttpClient to prevent null pointer crash If malloc() fails to allocate the response buffer, _buffer remains nullptr. The HTTP event handler later calls memcpy() into this buffer without checking, which would cause a crash on data reception. This adds a null check in the constructor (with a debug log) and a guard in the HTTP_EVENT_ON_DATA handler to safely drop data instead of crashing if the buffer wasn't allocated. --- lib/http/fnHttpClient.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/http/fnHttpClient.cpp b/lib/http/fnHttpClient.cpp index a76dedf08..1dd380858 100755 --- a/lib/http/fnHttpClient.cpp +++ b/lib/http/fnHttpClient.cpp @@ -23,6 +23,10 @@ const char *webdav_depths[] = {"0", "1", "infinity"}; fnHttpClient::fnHttpClient() { _buffer = (char *)malloc(DEFAULT_HTTP_BUF_SIZE); + if (_buffer == nullptr) + { + Debug_printf("fnHttpClient::fnHttpClient() failed to allocate %d byte buffer\r\n", DEFAULT_HTTP_BUF_SIZE); + } } // Close connection, destroy any resoruces @@ -354,9 +358,16 @@ esp_err_t fnHttpClient::_httpevent_handler(esp_http_client_event_t *evt) Debug_printf("HTTP_EVENT_ON_DATA: Data: %p, Datalen: %d\r\n", evt->data, evt->data_len); #endif - client->_buffer_pos = 0; - client->_buffer_len = (evt->data_len > DEFAULT_HTTP_BUF_SIZE) ? DEFAULT_HTTP_BUF_SIZE : evt->data_len; - memcpy(client->_buffer, evt->data, client->_buffer_len); + if (client->_buffer == nullptr) { + Debug_printf("HTTP_EVENT_ON_DATA: _buffer is null, dropping data\r\n"); + client->_buffer_pos = 0; + client->_buffer_len = 0; + } + else { + client->_buffer_pos = 0; + client->_buffer_len = (evt->data_len > DEFAULT_HTTP_BUF_SIZE) ? DEFAULT_HTTP_BUF_SIZE : evt->data_len; + memcpy(client->_buffer, evt->data, client->_buffer_len); + } // Now let the reader know there's data in the buffer xTaskNotifyGive(client->_taskh_consumer); From 478fbd373c31530f59e0dd59b7dcb0dae7adae0f Mon Sep 17 00:00:00 2001 From: yazdan-iot Date: Tue, 30 Jun 2026 16:02:58 +0330 Subject: [PATCH 3/6] Remove accidental test text from README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ac27e6ba..ff98a9eae 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# FujiNet afdsssssssssss +# FujiNet A multi-function peripheral built on ESP32 hardware being developed for the multiple 8-bit systems From 37718b34e0b2eb4a709c29f9855b90fcd85144f2 Mon Sep 17 00:00:00 2001 From: yazdan-iot Date: Thu, 2 Jul 2026 18:27:35 +0330 Subject: [PATCH 4/6] Fix 7 bugs in lib/http/ --- lib/http/fnHttpClient.cpp | 6 ++++-- lib/http/httpService.cpp | 14 +++++++++++--- lib/http/httpServiceParser.cpp | 2 +- lib/http/mgHttpClient.cpp | 6 ++++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/http/fnHttpClient.cpp b/lib/http/fnHttpClient.cpp index 1dd380858..5c3ab7c3b 100755 --- a/lib/http/fnHttpClient.cpp +++ b/lib/http/fnHttpClient.cpp @@ -297,7 +297,7 @@ esp_err_t fnHttpClient::_httpevent_handler(esp_http_client_event_t *evt) Debug_printf("HTTP_EVENT_ON_HEADER %u\r\n", uxTaskGetStackHighWaterMark(nullptr)); #endif // Check to see if we should store this response header - if (client->_stored_headers.size() <= 0) + if (client->_stored_headers.size() == 0) break; client->set_header_value(evt->header_key, evt->header_value); @@ -822,7 +822,9 @@ char *fnHttpClient::get_header(int index, char *buffer, int buffer_len) auto vi = _stored_headers.begin(); std::advance(vi, index); - return strncpy(buffer, vi->second.c_str(), buffer_len); + strncpy(buffer, vi->second.c_str(), buffer_len - 1); + buffer[buffer_len - 1] = '\0'; + return buffer; } const std::string fnHttpClient::get_header(int index) diff --git a/lib/http/httpService.cpp b/lib/http/httpService.cpp index af5201e77..ec4e3e3d4 100644 --- a/lib/http/httpService.cpp +++ b/lib/http/httpService.cpp @@ -65,7 +65,11 @@ char to_hex(char code) /* IMPORTANT: be sure to free() the returned string after use */ char *url_encode(char *str) { - char *pstr = str, *buf = (char *)malloc(strlen(str) * 3 + 1), *pbuf = buf; + char *pstr = str; + char *buf = (char *)malloc(strlen(str) * 3 + 1); + if (buf == NULL) + return NULL; + char *pbuf = buf; while (*pstr) { if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') @@ -84,7 +88,11 @@ char *url_encode(char *str) /* IMPORTANT: be sure to free() the returned string after use */ char *url_decode(char *str) { - char *pstr = str, *buf = (char *)malloc(strlen(str) + 1), *pbuf = buf; + char *pstr = str; + char *buf = (char *)malloc(strlen(str) + 1); + if (buf == NULL) + return NULL; + char *pbuf = buf; while (*pstr) { if (*pstr == '%') @@ -341,7 +349,7 @@ void fnHttpService::parse_query(httpd_req_t *req, queryparts *results) } /// @todo Error if path_end == 0, the index to substr becomes -1 - results->path += results->full_uri.substr(0, path_end - 1); + results->path += results->full_uri.substr(0, path_end); results->query += results->full_uri.substr(path_end + 1); // URL Decode query diff --git a/lib/http/httpServiceParser.cpp b/lib/http/httpServiceParser.cpp index ac4830cae..812bc0e8a 100644 --- a/lib/http/httpServiceParser.cpp +++ b/lib/http/httpServiceParser.cpp @@ -652,7 +652,7 @@ const string fnHttpServiceParser::substitute_tag(const string &tag) { strncat(result, "\n", MAX_PRINTER_LIST_BUFFER-1); } diff --git a/lib/http/mgHttpClient.cpp b/lib/http/mgHttpClient.cpp index 202d75c44..b4cdc403e 100644 --- a/lib/http/mgHttpClient.cpp +++ b/lib/http/mgHttpClient.cpp @@ -984,7 +984,7 @@ int mgHttpClient::COPY(const char *destination, bool overwrite, bool move) _flush_response(); // Set method - _method = HTTP_MOVE; + _method = move ? HTTP_MOVE : HTTP_COPY; // Set detination set_header("Destination", destination); // Set overwrite @@ -1110,7 +1110,9 @@ char *mgHttpClient::get_header(int index, char *buffer, int buffer_len) auto vi = _stored_headers.begin(); std::advance(vi, index); - return strncpy(buffer, vi->second.c_str(), buffer_len); + strncpy(buffer, vi->second.c_str(), buffer_len - 1); + buffer[buffer_len - 1] = '\0'; + return buffer; } const std::string mgHttpClient::get_header(int index) From 0f9c1fc16eef964dc60b3b085366dc86744cabb9 Mon Sep 17 00:00:00 2001 From: yazdan-iot Date: Thu, 2 Jul 2026 18:51:33 +0330 Subject: [PATCH 5/6] Use strlcpy instead of strncpy for null termination --- lib/http/fnHttpClient.cpp | 3 +-- lib/http/mgHttpClient.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/http/fnHttpClient.cpp b/lib/http/fnHttpClient.cpp index 5c3ab7c3b..1be8ed3e6 100755 --- a/lib/http/fnHttpClient.cpp +++ b/lib/http/fnHttpClient.cpp @@ -822,8 +822,7 @@ char *fnHttpClient::get_header(int index, char *buffer, int buffer_len) auto vi = _stored_headers.begin(); std::advance(vi, index); - strncpy(buffer, vi->second.c_str(), buffer_len - 1); - buffer[buffer_len - 1] = '\0'; + strlcpy(buffer, vi->second.c_str(), buffer_len); return buffer; } diff --git a/lib/http/mgHttpClient.cpp b/lib/http/mgHttpClient.cpp index b4cdc403e..41b01a091 100644 --- a/lib/http/mgHttpClient.cpp +++ b/lib/http/mgHttpClient.cpp @@ -1110,8 +1110,7 @@ char *mgHttpClient::get_header(int index, char *buffer, int buffer_len) auto vi = _stored_headers.begin(); std::advance(vi, index); - strncpy(buffer, vi->second.c_str(), buffer_len - 1); - buffer[buffer_len - 1] = '\0'; + strlcpy(buffer, vi->second.c_str(), buffer_len); return buffer; } From e19196ee1f1c778927d1f515263f4904f0c96558 Mon Sep 17 00:00:00 2001 From: yazdan-iot Date: Thu, 2 Jul 2026 19:11:11 +0330 Subject: [PATCH 6/6] Add compat_string.h include for strlcpy --- lib/http/fnHttpClient.cpp | 1 + lib/http/mgHttpClient.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/http/fnHttpClient.cpp b/lib/http/fnHttpClient.cpp index 1be8ed3e6..57b7f1ea4 100755 --- a/lib/http/fnHttpClient.cpp +++ b/lib/http/fnHttpClient.cpp @@ -5,6 +5,7 @@ #include #include "fnHttpClient.h" +#include "compat_string.h" #include "../../include/debug.h" diff --git a/lib/http/mgHttpClient.cpp b/lib/http/mgHttpClient.cpp index 41b01a091..adde998f5 100644 --- a/lib/http/mgHttpClient.cpp +++ b/lib/http/mgHttpClient.cpp @@ -14,6 +14,7 @@ #include "mongoose.h" #undef mkdir +#include "compat_string.h" #if defined(_WIN32)