My first change - #1409
Open
yazdan-iot wants to merge 6 commits into
Open
Conversation
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.
FozzTexx
reviewed
Jul 2, 2026
Comment on lines
814
to
+827
|
|
||
| char *fnHttpClient::get_header(int index, char *buffer, int buffer_len) | ||
| { | ||
| if (index < 0 || index > (_stored_headers.size() - 1)) | ||
| return nullptr; | ||
|
|
||
| if (buffer == nullptr) | ||
| return nullptr; | ||
|
|
||
| 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; |
Contributor
There was a problem hiding this comment.
It's easier to replace strncpy() with strlcpy() which will guarantee it's null terminated.
Comment on lines
26
to
29
| if (_buffer == nullptr) | ||
| { | ||
| Debug_printf("fnHttpClient::fnHttpClient() failed to allocate %d byte buffer\r\n", DEFAULT_HTTP_BUF_SIZE); | ||
| } |
Contributor
There was a problem hiding this comment.
I'm pretty sure this fix is already in master. You probably need to rebase this branch on master. First go to your fork and go to your master branch and click the Sync fork button. Then from your cloned repo:
git checkout master
git pull
git checkout my-first-change
git rebase master
git push --force-with-lease
Comment on lines
+1113
to
+1115
| strncpy(buffer, vi->second.c_str(), buffer_len - 1); | ||
| buffer[buffer_len - 1] = '\0'; | ||
| return buffer; |
Contributor
There was a problem hiding this comment.
Same here, just use strlcpy()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: 7 Bugs in lib/http/
Summary
This PR fixes 7 bugs found in the
lib/http/folder. All bugs are small and safe to merge.Bug 1: Buffer not null-terminated
Files:
lib/http/fnHttpClient.cppline 825lib/http/mgHttpClient.cppline 1113Problem:
strncpy()does not add\0at the end when the source string is longer than the buffer. Any code reading this buffer will read garbage memory.Example:
Fix:
Bug 2: url_encode() crashes on low memory
File:
lib/http/httpService.cppline 68Problem:
malloc()can returnNULLwhen memory is low. The code does not check and writes toNULLpointer, causing crash.Fix:
Bug 3: url_decode() crashes on low memory
File:
lib/http/httpService.cppline 87Problem:
Same as Bug 2.
malloc()without NULL check.Fix:
Bug 4: Off-by-one in query parsing
File:
lib/http/httpService.cppline 344Problem:
Using
path_end - 1instead ofpath_endcuts one character from the path.Example:
Fix:
Bug 5: COPY() always sends MOVE request
File:
lib/http/mgHttpClient.cppline 987Problem:
The
COPY()function ignores themoveparameter. It always setsHTTP_MOVE, even when copying.Fix:
Bug 6: strncat() can overflow buffer
File:
lib/http/httpServiceParser.cppline 655Problem:
strncat()receives the total buffer size instead of remaining space. Can write past the buffer.Fix:
Bug 7: Wrong unsigned comparison
File:
lib/http/fnHttpClient.cppline 300Problem:
size()returnsunsigned. Comparing with<= 0is meaningless because unsigned can never be negative.Fix:
Files Changed
Testing
All fixes are small and focused:
Related
All bugs are in the HTTP library (
lib/http/). They affect: