From e4a9f115cb2616b814859ebd0be2adeda8cd907d Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 21 May 2023 14:33:25 +0200 Subject: [PATCH] fx functions: avoid memory corruption * PSRAM_Allocator was missing the "reallocate" method, which lead to undefined behaviour when dynamic JSON doc needed to grow/shrink * Segment::setUpLeds() quickfix for length() == 0 (should not happen, but it did happen for me once) * leds in PSRAM causes major slowdown on wrover boards - disabled. --- wled00/FX_fcn.cpp | 15 ++++++++------- wled00/wled.h | 4 ++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 08f68818..557cdd2d 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -199,13 +199,14 @@ void Segment::setUpLeds() { #else leds = &Segment::_globalLeds[start]; #endif - else if (!leds) { - #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) - if (psramFound()) - leds = (CRGB*)ps_malloc(sizeof(CRGB)*length()); - else - #endif - leds = (CRGB*)malloc(sizeof(CRGB)*length()); + else if (leds == nullptr) { + //#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) + //if (psramFound()) + // leds = (CRGB*)ps_malloc(sizeof(CRGB)*length()); // softhack007 disabled; putting leds into psram leads to horrible slowdown on WROVER boards + //else + //#endif + if (length() > 0) //softhack007 quickfix - avoid malloc(0) which is undefined behaviour (should not happen, but i've seen it) + leds = (CRGB*)malloc(sizeof(CRGB)*length()); } } diff --git a/wled00/wled.h b/wled00/wled.h index eefdf531..ad4588da 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -151,6 +151,10 @@ struct PSRAM_Allocator { if (psramFound()) return ps_malloc(size); // use PSRAM if it exists else return malloc(size); // fallback } + void* reallocate(void* ptr, size_t new_size) { + if (psramFound()) return ps_realloc(ptr, new_size); // use PSRAM if it exists + else return realloc(ptr, new_size); // fallback + } void deallocate(void* pointer) { free(pointer); }