Skip to content

Commit

Permalink
robustness improvements
Browse files Browse the repository at this point in the history
fixing a few potential null pointer access and stack corruption cases
  • Loading branch information
softhack007 committed Mar 27, 2023
1 parent a595f77 commit 312cd94
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion usermods/usermod_v2_auto_save/usermod_v2_auto_save.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AutoSaveUsermod : public Usermod {
void inline saveSettings() {
char presetNameBuffer[PRESET_NAME_BUFFER_SIZE];
updateLocalTime();
sprintf_P(presetNameBuffer,
snprintf_P(presetNameBuffer, PRESET_NAME_BUFFER_SIZE -1,
PSTR("~ %02d-%02d %02d:%02d:%02d ~"),
month(localTime), day(localTime),
hour(localTime), minute(localTime), second(localTime));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,10 @@ class RotaryEncoderUIUsermod : public Usermod {
findCurrentEffectAndPalette();
}

if (modes_alpha_indexes[effectCurrentIndex] != effectCurrent || palettes_alpha_indexes[effectPaletteIndex] != effectPalette) {
currentEffectAndPaletteInitialized = false;
if ((modes_alpha_indexes != nullptr) && (palettes_alpha_indexes != nullptr)) { // WLEDSR bugfix
if (modes_alpha_indexes[effectCurrentIndex] != effectCurrent || palettes_alpha_indexes[effectPaletteIndex] != effectPalette) {
currentEffectAndPaletteInitialized = false;
}
}

if (currentTime >= (loopTime + 2)) // 2ms since last check of encoder = 500Hz
Expand Down Expand Up @@ -429,11 +431,13 @@ class RotaryEncoderUIUsermod : public Usermod {

void displayNetworkInfo() {
#ifdef USERMOD_FOUR_LINE_DISPLAY
display->networkOverlay(PSTR("NETWORK INFO"), 10000);
if (display != nullptr) // WLEDSR bugfix
display->networkOverlay(PSTR("NETWORK INFO"), 10000);
#endif
}

void findCurrentEffectAndPalette() {
if (modes_alpha_indexes == nullptr) return; // WLEDSR bugfix
currentEffectAndPaletteInitialized = true;
for (uint8_t i = 0; i < strip.getModeCount(); i++) {
if (modes_alpha_indexes[i] == effectCurrent) {
Expand Down Expand Up @@ -470,7 +474,8 @@ class RotaryEncoderUIUsermod : public Usermod {
// 6: fx changed 7: hue 8: preset cycle 9: blynk 10: alexa
//setValuesFromFirstSelectedSeg(); //to make transition work on main segment (should no longer be required)
stateUpdated(CALL_MODE_BUTTON);
updateInterfaces(CALL_MODE_BUTTON);
if ((millis() - lastInterfaceUpdate) > INTERFACE_UPDATE_COOLDOWN) // WLEDSR respect cooldown times, to avoid crash in AsyncWebSocketMessageBuffer
updateInterfaces(CALL_MODE_BUTTON);
}

void changeBrightness(bool increase) {
Expand Down Expand Up @@ -500,7 +505,7 @@ class RotaryEncoderUIUsermod : public Usermod {
display->updateRedrawTime();
#endif
effectCurrentIndex = max(min((increase ? effectCurrentIndex+1 : effectCurrentIndex-1), strip.getModeCount()-1), 0);
effectCurrent = modes_alpha_indexes[effectCurrentIndex];
if (modes_alpha_indexes != nullptr) effectCurrent = modes_alpha_indexes[effectCurrentIndex]; // WLEDSR bugfix
stateChanged = true;
if (applyToAll) {
for (byte i=0; i<strip.getMaxSegments(); i++) {
Expand Down
9 changes: 7 additions & 2 deletions wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ void WLED::reset()

bool oappendi(int i)
{
char s[11];
sprintf(s, "%d", i);
char s[16]; // WLEDSR max 32bit integer needs 11 chars (sign + 10) not 10
snprintf(s, 15,"%d", i); // WLEDSR protect against stack corruption
return oappend(s);
}

Expand Down Expand Up @@ -505,6 +505,11 @@ void WLED::setup()
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_DISABLE_BROWNOUT_DET)
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); //enable brownout detector
#endif
#ifndef WLED_ENABLE_ADALIGHT
if (!pinManager.isPinAllocated(1) || (pinManager.getPinOwner(1) == PinOwner::DebugOut)) {
Serial.println(F("\nWLED-SR ready.")); // WLEDSR "Ada" will not be printed without Adalight support. So tell users "all good".
}
#endif
}

void WLED::beginStrip()
Expand Down

0 comments on commit 312cd94

Please sign in to comment.