Skip to content

Commit

Permalink
bugfix for MoonModules#104
Browse files Browse the repository at this point in the history
this avoids heap corruption, by double-checking that "use global leds" is not configured, before trying to free ledsrgb[].

It is still a mystery why Segment::_globalLeds == nullptr.
  • Loading branch information
softhack007 committed Jan 2, 2024
1 parent f699a56 commit 01c187f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion wled00/FX.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

bool canUseSerial(void); // WLEDMM implemented in wled_serial.cpp
void strip_wait_until_idle(String whoCalledMe); // WLEDMM implemented in FX_fcn.cpp
bool strip_uses_global_leds(void); // WLEDMM implemented in FX_fcn.cpp

#define FASTLED_INTERNAL //remove annoying pragma messages
#define USE_GET_MILLISECOND_TIMER
Expand Down Expand Up @@ -516,7 +517,11 @@ typedef struct Segment {
if (name) Serial.printf(" name=%s (%p)", name, name);
if (data) Serial.printf(" dataLen=%d (%p)", (int)_dataLen, data);
if (ledsrgb) Serial.printf(" [%sledsrgb %u bytes]", Segment::_globalLeds ? "global ":"",length()*sizeof(CRGB));
if (strip_uses_global_leds() == true) Serial.println((Segment::_globalLeds != nullptr) ? F(" using global buffer.") : F(", using global buffer but Segment::_globalLeds is NULL!!"));
Serial.println();
#ifdef ARDUINO_ARCH_ESP32
Serial.flush();
#endif
}
#endif

Expand All @@ -525,7 +530,7 @@ typedef struct Segment {
strip_wait_until_idle("~Segment()");
#endif

if (!Segment::_globalLeds && (ledsrgb != nullptr)) {free(ledsrgb); ledsrgb = nullptr;}
if ((Segment::_globalLeds == nullptr) && !strip_uses_global_leds() && (ledsrgb != nullptr)) {free(ledsrgb); ledsrgb = nullptr;} // WLEDMM we need "!strip_uses_global_leds()" to avoid crashes (#104)
if (name) { delete[] name; name = nullptr; }
if (_t) { transitional = false; delete _t; _t = nullptr; }
deallocateData();
Expand Down
5 changes: 4 additions & 1 deletion wled00/FX_fcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ void strip_wait_until_idle(String whoCalledMe) {
}
#endif
}

// WLEDMM another helper for segment class
bool strip_uses_global_leds(void) {
return strip.useLedsArray;
}

///////////////////////////////////////////////////////////////////////////////
// Segment class implementation
Expand Down

0 comments on commit 01c187f

Please sign in to comment.