Skip to content

Commit 5316d21

Browse files
committed
Replace small local mallocs with stack buffers
1 parent 2f6729b commit 5316d21

File tree

2 files changed

+20
-46
lines changed

2 files changed

+20
-46
lines changed

src/AsyncWebSocket.cpp

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,7 @@
2424
#include <libb64/cencode.h>
2525

2626
#ifndef ESP8266
27-
extern "C" {
28-
typedef struct {
29-
uint32_t state[5];
30-
uint32_t count[2];
31-
unsigned char buffer[64];
32-
} SHA1_CTX;
33-
34-
void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]);
35-
void SHA1Init(SHA1_CTX* context);
36-
void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len);
37-
void SHA1Final(unsigned char digest[20], SHA1_CTX* context);
38-
}
27+
#include "mbedtls/sha1.h"
3928
#else
4029
#include <Hash.h>
4130
#endif
@@ -1252,25 +1241,19 @@ AsyncWebSocketResponse::AsyncWebSocketResponse(const String& key, AsyncWebSocket
12521241
_code = 101;
12531242
_sendContentLength = false;
12541243

1255-
uint8_t * hash = (uint8_t*)malloc(20);
1256-
if(hash == NULL){
1257-
_state = RESPONSE_FAILED;
1258-
return;
1259-
}
1260-
char * buffer = (char *) malloc(33);
1261-
if(buffer == NULL){
1262-
free(hash);
1263-
_state = RESPONSE_FAILED;
1264-
return;
1265-
}
1244+
uint8_t * hash[20];
1245+
char * buffer[33];
1246+
12661247
#ifdef ESP8266
12671248
sha1(key + WS_STR_UUID, hash);
12681249
#else
1269-
(String&)key += WS_STR_UUID;
1270-
SHA1_CTX ctx;
1271-
SHA1Init(&ctx);
1272-
SHA1Update(&ctx, (const unsigned char*)key.c_str(), key.length());
1273-
SHA1Final(hash, &ctx);
1250+
(String&)key += WS_STR_UUID;
1251+
mbedtls_sha1_context ctx;
1252+
mbedtls_sha1_init(&ctx);
1253+
mbedtls_sha1_starts_ret(&ctx);
1254+
mbedtls_sha1_update_ret(&ctx, (const unsigned char*)key.c_str(), key.length());
1255+
mbedtls_sha1_finish_ret(&ctx, hash);
1256+
mbedtls_sha1_free(&ctx);
12741257
#endif
12751258
base64_encodestate _state;
12761259
base64_init_encodestate(&_state);
@@ -1279,8 +1262,6 @@ AsyncWebSocketResponse::AsyncWebSocketResponse(const String& key, AsyncWebSocket
12791262
addHeader(WS_STR_CONNECTION, WS_STR_UPGRADE);
12801263
addHeader(WS_STR_UPGRADE, "websocket");
12811264
addHeader(WS_STR_ACCEPT,buffer);
1282-
free(buffer);
1283-
free(hash);
12841265
}
12851266

12861267
void AsyncWebSocketResponse::_respond(AsyncWebServerRequest *request){

src/WebAuthentication.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,12 @@ static bool getMD5(uint8_t * data, uint16_t len, char * output){//33 bytes or mo
6565
md5_context_t _ctx;
6666
#endif
6767
uint8_t i;
68-
uint8_t * _buf = (uint8_t*)malloc(16);
69-
if(_buf == NULL)
70-
return false;
71-
memset(_buf, 0x00, 16);
68+
uint8_t * _buf[16] = {0};
7269
#ifdef ESP32
7370
mbedtls_md5_init(&_ctx);
74-
mbedtls_md5_starts(&_ctx);
75-
mbedtls_md5_update(&_ctx, data, len);
76-
mbedtls_md5_finish(&_ctx, _buf);
71+
mbedtls_md5_starts_ret(&_ctx);
72+
mbedtls_md5_update_ret(&_ctx, data, len);
73+
mbedtls_md5_finish_ret(&_ctx, _buf);
7774
#else
7875
MD5Init(&_ctx);
7976
MD5Update(&_ctx, data, len);
@@ -82,7 +79,6 @@ static bool getMD5(uint8_t * data, uint16_t len, char * output){//33 bytes or mo
8279
for(i = 0; i < 16; i++) {
8380
sprintf(output + (i * 2), "%02x", _buf[i]);
8481
}
85-
free(_buf);
8682
return true;
8783
}
8884

@@ -92,38 +88,35 @@ static String genRandomMD5(){
9288
#else
9389
uint32_t r = rand();
9490
#endif
95-
char * out = (char*)malloc(33);
91+
char * out[33];
9692
if(out == NULL || !getMD5((uint8_t*)(&r), 4, out))
9793
return "";
9894
String res = String(out);
99-
free(out);
10095
return res;
10196
}
10297

10398
static String stringMD5(const String& in){
104-
char * out = (char*)malloc(33);
105-
if(out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out))
99+
char * out[33];
100+
if(!getMD5((uint8_t*)(in.c_str()), in.length(), out))
106101
return "";
107102
String res = String(out);
108-
free(out);
109103
return res;
110104
}
111105

112106
String generateDigestHash(const char * username, const char * password, const char * realm){
113107
if(username == NULL || password == NULL || realm == NULL){
114108
return "";
115109
}
116-
char * out = (char*)malloc(33);
110+
char * out[33];
117111
String res = String(username);
118112
res.concat(":");
119113
res.concat(realm);
120114
res.concat(":");
121115
String in = res;
122116
in.concat(password);
123-
if(out == NULL || !getMD5((uint8_t*)(in.c_str()), in.length(), out))
117+
if(!getMD5((uint8_t*)(in.c_str()), in.length(), out))
124118
return "";
125119
res.concat(out);
126-
free(out);
127120
return res;
128121
}
129122

0 commit comments

Comments
 (0)