Skip to content

Commit 486caf4

Browse files
Remove microscopic malloc() from WebServer (earlephilhower#809)
Don't try and heap allocate temporaty <16b chunks.
1 parent 92f2ca9 commit 486caf4

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

libraries/WebServer/src/HTTPServer.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ static String md5str(String &in) {
8787
char out[33] = {0};
8888
MD5Builder _ctx;
8989
uint8_t i;
90-
uint8_t * _buf = (uint8_t*)malloc(16);
91-
if (_buf == NULL) {
92-
return String(out);
93-
}
90+
uint8_t _buf[16];
9491
memset(_buf, 0x00, 16);
9592
_ctx.begin();
9693
_ctx.add((const uint8_t *)in.c_str(), in.length());
@@ -100,7 +97,6 @@ static String md5str(String &in) {
10097
sprintf(out + (i * 2), "%02x", _buf[i]);
10198
}
10299
out[32] = 0;
103-
free(_buf);
104100
return String(out);
105101
}
106102

@@ -443,12 +439,9 @@ void HTTPServer::sendContent(const String& content) {
443439
void HTTPServer::sendContent(const char* content, size_t contentLength) {
444440
const char * footer = "\r\n";
445441
if (_chunked) {
446-
char * chunkSize = (char *)malloc(11);
447-
if (chunkSize) {
448-
sprintf(chunkSize, "%x%s", contentLength, footer);
449-
_currentClientWrite(chunkSize, strlen(chunkSize));
450-
free(chunkSize);
451-
}
442+
char chunkSize[11];
443+
sprintf(chunkSize, "%x%s", contentLength, footer);
444+
_currentClientWrite(chunkSize, strlen(chunkSize));
452445
}
453446
_currentClientWrite(content, contentLength);
454447
if (_chunked) {
@@ -466,12 +459,9 @@ void HTTPServer::sendContent_P(PGM_P content) {
466459
void HTTPServer::sendContent_P(PGM_P content, size_t size) {
467460
const char * footer = "\r\n";
468461
if (_chunked) {
469-
char * chunkSize = (char *)malloc(11);
470-
if (chunkSize) {
471-
sprintf(chunkSize, "%x%s", size, footer);
472-
_currentClientWrite(chunkSize, strlen(chunkSize));
473-
free(chunkSize);
474-
}
462+
char chunkSize[11];
463+
sprintf(chunkSize, "%x%s", size, footer);
464+
_currentClientWrite(chunkSize, strlen(chunkSize));
475465
}
476466
_currentClientWrite_P(content, size);
477467
if (_chunked) {

0 commit comments

Comments
 (0)