Skip to content

Commit

Permalink
Merge pull request #57 from ESP32Async/fix/allocation
Browse files Browse the repository at this point in the history
fix(mem): allocation checks and request abort on failed allocation
  • Loading branch information
mathieucarbou authored Feb 6, 2025
2 parents 4e9a7ba + 18cb706 commit 7a42cb2
Show file tree
Hide file tree
Showing 32 changed files with 135 additions and 74 deletions.
4 changes: 3 additions & 1 deletion examples/Auth/Auth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/CORS/CORS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/CatchAllHandler/CatchAllHandler.ino
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/ChunkResponse/ChunkResponse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,6 @@ void setup() {
server.begin();
}

void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/EndBegin/EndBegin.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/Filters/Filters.ino
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ void setup() {
// ESP.restart();
}

void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/FlashResponse/FlashResponse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/HeaderManipulation/HeaderManipulation.ino
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/Json/Json.ino
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/Logging/Logging.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/MessagePack/MessagePack.ino
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/Middleware/Middleware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/Params/Params.ino
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/RateLimit/RateLimit.ino
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/Redirect/Redirect.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/ResumableDownload/ResumableDownload.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ void setup() {
server.begin();
}

void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/Rewrite/Rewrite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/SkipServerMiddleware/SkipServerMiddleware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/StaticFile/StaticFile.ino
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
4 changes: 3 additions & 1 deletion examples/Templates/Templates.ino
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ void setup() {
}

// not needed
void loop() {}
void loop() {
delay(100);
}
2 changes: 1 addition & 1 deletion src/AsyncEventSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static String generateEventMessage(const char *message, const char *event, uint3

if (!str.reserve(len)) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
return emptyString;
}
Expand Down
3 changes: 2 additions & 1 deletion src/AsyncJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ void AsyncCallbackJsonWebHandler::handleBody(AsyncWebServerRequest *request, uin
request->_tempObject = malloc(total);
if (request->_tempObject == NULL) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
request->abort();
return;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/AsyncMessagePack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ void AsyncCallbackMessagePackWebHandler::handleBody(AsyncWebServerRequest *reque
request->_tempObject = malloc(total);
if (request->_tempObject == NULL) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
request->abort();
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/AsyncWebHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ String AsyncWebHeader::toString() const {
str.concat(asyncsrv::T_rn);
} else {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
}
return str;
Expand Down
33 changes: 15 additions & 18 deletions src/AsyncWebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ size_t webSocketSendFrame(AsyncClient *client, bool final, uint8_t opcode, bool
uint8_t *buf = (uint8_t *)malloc(headLen);
if (buf == NULL) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
client->abort();
return 0;
}

Expand Down Expand Up @@ -170,7 +171,7 @@ class AsyncWebSocketControl {

if (_data == NULL) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
_len = 0;
} else {
Expand Down Expand Up @@ -522,7 +523,8 @@ void AsyncWebSocketClient::close(uint16_t code, const char *message) {
return;
} else {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
_client->abort();
#endif
}
}
Expand Down Expand Up @@ -1252,6 +1254,13 @@ void AsyncWebSocket::handleRequest(AsyncWebServerRequest *request) {
}
const AsyncWebHeader *key = request->getHeader(WS_STR_KEY);
AsyncWebServerResponse *response = new AsyncWebSocketResponse(key->value(), this);
if (response == NULL) {
#ifdef ESP32
log_e("Failed to allocate");
#endif
request->abort();
return;
}
if (request->hasHeader(WS_STR_PROTOCOL)) {
const AsyncWebHeader *protocol = request->getHeader(WS_STR_PROTOCOL);
// ToDo: check protocol
Expand All @@ -1261,23 +1270,11 @@ void AsyncWebSocket::handleRequest(AsyncWebServerRequest *request) {
}

AsyncWebSocketMessageBuffer *AsyncWebSocket::makeBuffer(size_t size) {
AsyncWebSocketMessageBuffer *buffer = new AsyncWebSocketMessageBuffer(size);
if (buffer->length() != size) {
delete buffer;
return nullptr;
} else {
return buffer;
}
return new AsyncWebSocketMessageBuffer(size);
}

AsyncWebSocketMessageBuffer *AsyncWebSocket::makeBuffer(const uint8_t *data, size_t size) {
AsyncWebSocketMessageBuffer *buffer = new AsyncWebSocketMessageBuffer(data, size);
if (buffer->length() != size) {
delete buffer;
return nullptr;
} else {
return buffer;
}
return new AsyncWebSocketMessageBuffer(data, size);
}

/*
Expand All @@ -1298,7 +1295,7 @@ AsyncWebSocketResponse::AsyncWebSocketResponse(const String &key, AsyncWebSocket
#else
String k;
if (!k.reserve(key.length() + WS_STR_UUID_LEN)) {
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
return;
}
k.concat(key);
Expand Down
10 changes: 5 additions & 5 deletions src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class AsyncWebServerRequest {

std::list<AsyncWebHeader> _headers;
std::list<AsyncWebParameter> _params;
std::vector<String> _pathParams;
std::list<String> _pathParams;

std::unordered_map<const char *, String, std::hash<const char *>, std::equal_to<const char *>> _attributes;

Expand Down Expand Up @@ -790,13 +790,13 @@ class AsyncHeaderFreeMiddleware : public AsyncMiddleware {
_toKeep.push_back(name);
}
void unKeep(const char *name) {
_toKeep.erase(std::remove(_toKeep.begin(), _toKeep.end(), name), _toKeep.end());
_toKeep.remove(name);
}

void run(AsyncWebServerRequest *request, ArMiddlewareNext next);

private:
std::vector<const char *> _toKeep;
std::list<const char *> _toKeep;
};

// filter out specific headers from the incoming request
Expand All @@ -806,13 +806,13 @@ class AsyncHeaderFilterMiddleware : public AsyncMiddleware {
_toRemove.push_back(name);
}
void unFilter(const char *name) {
_toRemove.erase(std::remove(_toRemove.begin(), _toRemove.end(), name), _toRemove.end());
_toRemove.remove(name);
}

void run(AsyncWebServerRequest *request, ArMiddlewareNext next);

private:
std::vector<const char *> _toRemove;
std::list<const char *> _toRemove;
};

// curl-like logging of incoming requests
Expand Down
12 changes: 7 additions & 5 deletions src/Middleware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,22 @@ void AsyncAuthenticationMiddleware::run(AsyncWebServerRequest *request, ArMiddle
}

void AsyncHeaderFreeMiddleware::run(AsyncWebServerRequest *request, ArMiddlewareNext next) {
std::vector<const char *> reqHeaders;
request->getHeaderNames(reqHeaders);
for (const char *h : reqHeaders) {
std::list<const char *> toRemove;
for (auto &h : request->getHeaders()) {
bool keep = false;
for (const char *k : _toKeep) {
if (strcasecmp(h, k) == 0) {
if (strcasecmp(h.name().c_str(), k) == 0) {
keep = true;
break;
}
}
if (!keep) {
request->removeHeader(h);
toRemove.push_back(h.name().c_str());
}
}
for (const char *h : toRemove) {
request->removeHeader(h);
}
next();
}

Expand Down
10 changes: 5 additions & 5 deletions src/WebAuthentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ String genRandomMD5() {
char *out = (char *)malloc(33);
if (out == NULL || !getMD5((uint8_t *)(&r), 4, out)) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
return emptyString;
}
Expand All @@ -100,7 +100,7 @@ static String stringMD5(const String &in) {
char *out = (char *)malloc(33);
if (out == NULL || !getMD5((uint8_t *)(in.c_str()), in.length(), out)) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
return emptyString;
}
Expand All @@ -116,15 +116,15 @@ String generateDigestHash(const char *username, const char *password, const char
char *out = (char *)malloc(33);
if (out == NULL) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
return emptyString;
}

String in;
if (!in.reserve(strlen(username) + strlen(realm) + strlen(password) + 2)) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
free(out);
return emptyString;
Expand All @@ -138,7 +138,7 @@ String generateDigestHash(const char *username, const char *password, const char

if (!getMD5((uint8_t *)(in.c_str()), in.length(), out)) {
#ifdef ESP32
log_e("Failed to allocate buffer");
log_e("Failed to allocate");
#endif
free(out);
return emptyString;
Expand Down
Loading

0 comments on commit 7a42cb2

Please sign in to comment.