Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "HttpClient.h"
#include "b64.h"
#include <limits.h>

// Initialize constants
const char* HttpClient::kUserAgent = "Arduino/2.2.0";
Expand Down Expand Up @@ -625,8 +626,12 @@ int HttpClient::available()
else if (isHexadecimalDigit(c))
{
char digit[2] = {c, '\0'};
int digitValue = (int)strtol(digit, NULL, 16);

iChunkLength = (iChunkLength * 16) + strtol(digit, NULL, 16);
// Only apply if the value fits in int without overflow.
if (iChunkLength <= ((INT_MAX - digitValue) / 16)) {

@andreagilardoni andreagilardoni Jul 13, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent, but I think it may be easier to read

Suggested change
if (iChunkLength <= ((INT_MAX - digitValue) / 16)) {
if ((iChunkLength * 16) + digitValue <= INT_MAX) {

iChunkLength = (iChunkLength * 16) + digitValue;
}
}
}
}
Expand Down Expand Up @@ -673,7 +678,10 @@ int HttpClient::read()

if (iState == eReadingBodyChunk)
{
iChunkLength--;
if (iChunkLength > 0)
{
iChunkLength--;
}

if (iChunkLength == 0)
{
Expand Down Expand Up @@ -819,10 +827,10 @@ int HttpClient::readHeader()
case eReadingContentLength:
if (isdigit(c))
{
long _iContentLength = iContentLength*10 + (c - '0');
// Only apply if the value didn't wrap around
if (_iContentLength > iContentLength) {
iContentLength = _iContentLength;
int digitValue = c - '0';
// Only apply if the value fits in long without overflow.
if (iContentLength <= ((LONG_MAX - digitValue) / 10)) {

@andreagilardoni andreagilardoni Jul 13, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent, but I think it may be easier to read

Suggested change
if (iContentLength <= ((LONG_MAX - digitValue) / 10)) {
if ((iContentLength * 10) + digitValue <= LONG_MAX) {

iContentLength = iContentLength * 10 + digitValue;
}
}
else
Expand Down