Skip to content

Commit 02b384a

Browse files
Replace new with malloc for non-class calls (#7868)
* Resolve potential crashes * Update Esp.cpp Resolved possible crash in EspClass::getSketchMD5().
1 parent 8d1a845 commit 02b384a

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

Diff for: cores/esp32/Esp.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -225,30 +225,31 @@ String EspClass::getSketchMD5()
225225
const esp_partition_t *running = esp_ota_get_running_partition();
226226
if (!running) {
227227
log_e("Partition could not be found");
228-
229228
return String();
230229
}
230+
231231
const size_t bufSize = SPI_FLASH_SEC_SIZE;
232-
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
233-
uint32_t offset = 0;
234-
if(!buf.get()) {
232+
uint8_t *pb = (uint8_t *)malloc(bufSize);
233+
if(!pb) {
235234
log_e("Not enough memory to allocate buffer");
236-
237235
return String();
238236
}
237+
uint32_t offset = 0;
238+
239239
MD5Builder md5;
240240
md5.begin();
241-
while( lengthLeft > 0) {
241+
while(lengthLeft > 0) {
242242
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
243-
if (!ESP.flashRead(running->address + offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
243+
if (!ESP.flashRead(running->address + offset, (uint32_t *)pb, (readBytes + 3) & ~3)) {
244+
free(pb);
244245
log_e("Could not read buffer from flash");
245-
246246
return String();
247247
}
248-
md5.add(buf.get(), readBytes);
248+
md5.add(pb, readBytes);
249249
lengthLeft -= readBytes;
250250
offset += readBytes;
251251
}
252+
free(pb);
252253
md5.calculate();
253254
result = md5.toString();
254255
return result;

Diff for: libraries/BLE/src/BLEAdvertising.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,12 @@ void BLEAdvertising::start() {
231231
int numServices = m_serviceUUIDs.size();
232232
if (numServices > 0) {
233233
m_advData.service_uuid_len = 16 * numServices;
234-
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
234+
m_advData.p_service_uuid = (uint8_t *)malloc(m_advData.service_uuid_len);
235+
if(!m_advData.p_service_uuid) {
236+
log_e(">> start failed: out of memory");
237+
return;
238+
}
239+
235240
uint8_t* p = m_advData.p_service_uuid;
236241
for (int i = 0; i < numServices; i++) {
237242
log_d("- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
@@ -276,10 +281,8 @@ void BLEAdvertising::start() {
276281

277282
// If we had services to advertise then we previously allocated some storage for them.
278283
// Here we release that storage.
279-
if (m_advData.service_uuid_len > 0) {
280-
delete[] m_advData.p_service_uuid;
281-
m_advData.p_service_uuid = nullptr;
282-
}
284+
free(m_advData.p_service_uuid); //TODO change this variable to local scope?
285+
m_advData.p_service_uuid = nullptr;
283286

284287
// Start advertising.
285288
errRc = ::esp_ble_gap_start_advertising(&m_advParams);

Diff for: libraries/WebServer/src/WebServer.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,26 @@ bool WebServer::authenticate(const char * username, const char * password){
135135
authReq = authReq.substring(6);
136136
authReq.trim();
137137
char toencodeLen = strlen(username)+strlen(password)+1;
138-
char *toencode = new char[toencodeLen + 1];
138+
char *toencode = (char *)malloc[toencodeLen + 1];
139139
if(toencode == NULL){
140140
authReq = "";
141141
return false;
142142
}
143-
char *encoded = new char[base64_encode_expected_len(toencodeLen)+1];
143+
char *encoded = (char *)malloc(base64_encode_expected_len(toencodeLen)+1);
144144
if(encoded == NULL){
145145
authReq = "";
146-
delete[] toencode;
146+
free(toencode);
147147
return false;
148148
}
149149
sprintf(toencode, "%s:%s", username, password);
150150
if(base64_encode_chars(toencode, toencodeLen, encoded) > 0 && authReq.equalsConstantTime(encoded)) {
151151
authReq = "";
152-
delete[] toencode;
153-
delete[] encoded;
152+
free(toencode);
153+
free(encoded);
154154
return true;
155155
}
156-
delete[] toencode;
157-
delete[] encoded;
156+
free(toencode);
157+
free(encoded);;
158158
} else if(authReq.startsWith(F("Digest"))) {
159159
authReq = authReq.substring(7);
160160
log_v("%s", authReq.c_str());

Diff for: 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)