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)
This commit is contained in:
brabl2 2022-11-24 19:25:48 +01:00 committed by GitHub
parent f11c22d984
commit e47e3c73c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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