From e47e3c73c25eed2ee4190f776d82b69c49f748bd Mon Sep 17 00:00:00 2001 From: brabl2 Date: Thu, 24 Nov 2022 19:25:48 +0100 Subject: [PATCH] Fix memcpy to unallocated memory in EEPROM.cpp (#1000) The memcpy to unallocated memory was done in two cases: 1) The 'size' parameter was not multiple of 256. 2) The begin() was called two times and the 2nd call 'size' was greater than 1st time 'size'. (e.g. 1st size=256, 2nd size=512) --- libraries/EEPROM/src/EEPROM.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 95e7d0c..06e6a9c 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -45,7 +45,7 @@ void EEPROMClass::begin(size_t size) { size = 4096; } - _size = (size + 255) & (~255); // Flash writes limited to 256 byte boundaries + size = (size + 255) & (~255); // Flash writes limited to 256 byte boundaries // In case begin() is called a 2nd+ time, don't reallocate if size is the same if (_data && size != _size) { @@ -55,6 +55,8 @@ void EEPROMClass::begin(size_t size) { _data = new uint8_t[size]; } + _size = size; + memcpy(_data, _sector, _size); _dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time