Skip to content

Commit e20bcfa

Browse files
Update Esp.cpp
Resolved possible crash in EspClass::getSketchMD5().
1 parent e346d55 commit e20bcfa

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

cores/esp32/Esp.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -217,30 +217,31 @@ String EspClass::getSketchMD5()
217217
const esp_partition_t *running = esp_ota_get_running_partition();
218218
if (!running) {
219219
log_e("Partition could not be found");
220-
221220
return String();
222221
}
222+
223223
const size_t bufSize = SPI_FLASH_SEC_SIZE;
224-
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
225-
uint32_t offset = 0;
226-
if(!buf.get()) {
224+
uint8_t *pb = (uint8_t *)malloc(bufSize);
225+
if(!pb) {
227226
log_e("Not enough memory to allocate buffer");
228-
229227
return String();
230228
}
229+
uint32_t offset = 0;
230+
231231
MD5Builder md5;
232232
md5.begin();
233-
while( lengthLeft > 0) {
233+
while(lengthLeft > 0) {
234234
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
235-
if (!ESP.flashRead(running->address + offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
235+
if (!ESP.flashRead(running->address + offset, (uint32_t *)pb, (readBytes + 3) & ~3)) {
236+
free(pb);
236237
log_e("Could not read buffer from flash");
237-
238238
return String();
239239
}
240-
md5.add(buf.get(), readBytes);
240+
md5.add(pb, readBytes);
241241
lengthLeft -= readBytes;
242242
offset += readBytes;
243243
}
244+
free(pb);
244245
md5.calculate();
245246
result = md5.toString();
246247
return result;

0 commit comments

Comments
 (0)