Skip to content

Commit 885eb2b

Browse files
Resolve potential crashes
1 parent 990e3d5 commit 885eb2b

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

libraries/BLE/src/BLEAdvertising.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ void BLEAdvertising::start() {
196196
int numServices = m_serviceUUIDs.size();
197197
if (numServices > 0) {
198198
m_advData.service_uuid_len = 16 * numServices;
199-
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
199+
m_advData.p_service_uuid = (uint8_t *)malloc(m_advData.service_uuid_len);
200+
if(!m_advData.p_service_uuid) {
201+
log_e(">> start failed: out of memory");
202+
return;
203+
}
204+
200205
uint8_t* p = m_advData.p_service_uuid;
201206
for (int i = 0; i < numServices; i++) {
202207
log_d("- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
@@ -241,10 +246,8 @@ void BLEAdvertising::start() {
241246

242247
// If we had services to advertise then we previously allocated some storage for them.
243248
// Here we release that storage.
244-
if (m_advData.service_uuid_len > 0) {
245-
delete[] m_advData.p_service_uuid;
246-
m_advData.p_service_uuid = nullptr;
247-
}
249+
free(m_advData.p_service_uuid); //TODO change this variable to local scope?
250+
m_advData.p_service_uuid = nullptr;
248251

249252
// Start advertising.
250253
errRc = ::esp_ble_gap_start_advertising(&m_advParams);

libraries/WebServer/src/WebServer.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,26 @@ bool WebServer::authenticate(const char * username, const char * password){
134134
authReq = authReq.substring(6);
135135
authReq.trim();
136136
char toencodeLen = strlen(username)+strlen(password)+1;
137-
char *toencode = new char[toencodeLen + 1];
137+
char *toencode = (char *)malloc[toencodeLen + 1];
138138
if(toencode == NULL){
139139
authReq = "";
140140
return false;
141141
}
142-
char *encoded = new char[base64_encode_expected_len(toencodeLen)+1];
142+
char *encoded = (char *)malloc(base64_encode_expected_len(toencodeLen)+1);
143143
if(encoded == NULL){
144144
authReq = "";
145-
delete[] toencode;
145+
free(toencode);
146146
return false;
147147
}
148148
sprintf(toencode, "%s:%s", username, password);
149149
if(base64_encode_chars(toencode, toencodeLen, encoded) > 0 && authReq.equalsConstantTime(encoded)) {
150150
authReq = "";
151-
delete[] toencode;
152-
delete[] encoded;
151+
free(toencode);
152+
free(encoded);
153153
return true;
154154
}
155-
delete[] toencode;
156-
delete[] encoded;
155+
free(toencode);
156+
free(encoded);;
157157
} else if(authReq.startsWith(F("Digest"))) {
158158
authReq = authReq.substring(7);
159159
log_v("%s", authReq.c_str());

libraries/WiFi/src/WiFiUdp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ uint8_t WiFiUDP::begin(IPAddress address, uint16_t port){
4444

4545
server_port = port;
4646

47-
tx_buffer = new char[1460];
47+
tx_buffer = (char *)malloc(1460);
4848
if(!tx_buffer){
4949
log_e("could not create tx buffer: %d", errno);
5050
return 0;
5151
}
52+
tx_buffer_len = 0;
5253

5354
if ((udp_server=socket(AF_INET, SOCK_DGRAM, 0)) == -1){
5455
log_e("could not create socket: %d", errno);
@@ -100,7 +101,7 @@ uint8_t WiFiUDP::beginMulticast(IPAddress a, uint16_t p){
100101

101102
void WiFiUDP::stop(){
102103
if(tx_buffer){
103-
delete[] tx_buffer;
104+
free(tx_buffer);
104105
tx_buffer = NULL;
105106
}
106107
tx_buffer_len = 0;
@@ -136,13 +137,12 @@ int WiFiUDP::beginPacket(){
136137

137138
// allocate tx_buffer if is necessary
138139
if(!tx_buffer){
139-
tx_buffer = new char[1460];
140+
tx_buffer = (char *)malloc(1460);
140141
if(!tx_buffer){
141142
log_e("could not create tx buffer: %d", errno);
142143
return 0;
143144
}
144145
}
145-
146146
tx_buffer_len = 0;
147147

148148
// check whereas socket is already open

0 commit comments

Comments
 (0)