Skip to content

Commit e346d55

Browse files
Resolve potential crashes
1 parent 0d84018 commit e346d55

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

libraries/BLE/src/BLEAdvertising.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ void BLEAdvertising::start() {
193193
int numServices = m_serviceUUIDs.size();
194194
if (numServices > 0) {
195195
m_advData.service_uuid_len = 16 * numServices;
196-
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
196+
m_advData.p_service_uuid = (uint8_t *)malloc(m_advData.service_uuid_len);
197+
if(!m_advData.p_service_uuid) {
198+
log_e(">> start failed: out of memory");
199+
return;
200+
}
201+
197202
uint8_t* p = m_advData.p_service_uuid;
198203
for (int i = 0; i < numServices; i++) {
199204
log_d("- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
@@ -238,10 +243,8 @@ void BLEAdvertising::start() {
238243

239244
// If we had services to advertise then we previously allocated some storage for them.
240245
// Here we release that storage.
241-
if (m_advData.service_uuid_len > 0) {
242-
delete[] m_advData.p_service_uuid;
243-
m_advData.p_service_uuid = nullptr;
244-
}
246+
free(m_advData.p_service_uuid); //TODO change this variable to local scope?
247+
m_advData.p_service_uuid = nullptr;
245248

246249
// Start advertising.
247250
errRc = ::esp_ble_gap_start_advertising(&m_advParams);

libraries/WebServer/src/WebServer.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,26 @@ bool WebServer::authenticate(const char * username, const char * password){
145145
authReq = authReq.substring(6);
146146
authReq.trim();
147147
char toencodeLen = strlen(username)+strlen(password)+1;
148-
char *toencode = new char[toencodeLen + 1];
148+
char *toencode = (char *)malloc[toencodeLen + 1];
149149
if(toencode == NULL){
150150
authReq = "";
151151
return false;
152152
}
153-
char *encoded = new char[base64_encode_expected_len(toencodeLen)+1];
153+
char *encoded = (char *)malloc(base64_encode_expected_len(toencodeLen)+1);
154154
if(encoded == NULL){
155155
authReq = "";
156-
delete[] toencode;
156+
free(toencode);
157157
return false;
158158
}
159159
sprintf(toencode, "%s:%s", username, password);
160160
if(base64_encode_chars(toencode, toencodeLen, encoded) > 0 && authReq.equalsConstantTime(encoded)) {
161161
authReq = "";
162-
delete[] toencode;
163-
delete[] encoded;
162+
free(toencode);
163+
free(encoded);
164164
return true;
165165
}
166-
delete[] toencode;
167-
delete[] encoded;
166+
free(toencode);
167+
free(encoded);;
168168
} else if(authReq.startsWith(F("Digest"))) {
169169
authReq = authReq.substring(7);
170170
log_v("%s", authReq.c_str());

libraries/WiFi/src/WiFiUdp.cpp

+4-4
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)