Skip to content

Commit 709c935

Browse files
MathewHDYTP-R-O-C-H-Yme-no-dev
authored
Fix memory leak and undefined behavour in Updater.cpp (UpdaterClass) (espressif#8671)
* Fix memory leak and undefined behavour in Updater espressif#7984 * Update error message Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> * Update error message Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> --------- Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
1 parent 9a28c93 commit 709c935

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

libraries/Update/src/Updater.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ UpdateClass& UpdateClass::onProgress(THandlerFunction_Progress fn) {
7676
}
7777

7878
void UpdateClass::_reset() {
79-
if (_buffer)
79+
if (_buffer) {
8080
delete[] _buffer;
81-
_buffer = 0;
81+
}
82+
if (_skipBuffer) {
83+
delete[] _skipBuffer;
84+
}
85+
86+
_buffer = nullptr;
87+
_skipBuffer = nullptr;
8288
_bufferLen = 0;
8389
_progress = 0;
8490
_size = 0;
@@ -159,9 +165,9 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, con
159165
}
160166

161167
//initialize
162-
_buffer = (uint8_t*)malloc(SPI_FLASH_SEC_SIZE);
163-
if(!_buffer){
164-
log_e("malloc failed");
168+
_buffer = new (std::nothrow) uint8_t[SPI_FLASH_SEC_SIZE];
169+
if (!_buffer) {
170+
log_e("_buffer allocation failed");
165171
return false;
166172
}
167173
_size = size;
@@ -193,10 +199,10 @@ bool UpdateClass::_writeBuffer(){
193199
//not written at this point so that partially written firmware
194200
//will not be bootable
195201
skip = ENCRYPTED_BLOCK_SIZE;
196-
_skipBuffer = (uint8_t*)malloc(skip);
197-
if(!_skipBuffer){
198-
log_e("malloc failed");
199-
return false;
202+
_skipBuffer = new (std::nothrow) uint8_t[skip];
203+
if (!_skipBuffer) {
204+
log_e("_skipBuffer allocation failed");
205+
return false;
200206
}
201207
memcpy(_skipBuffer, _buffer, skip);
202208
}

0 commit comments

Comments
 (0)