Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ void WLED::setup()
#ifndef ESP8266
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
#endif
WiFi.persistent(false);
WiFi.persistent(false); // note: on ESP32 this is only applied if WiFi.mode() is called and only if mode changes, use low level esp_wifi_set_storage() to change storage method immediately
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_STA); // enable scanning
findWiFi(true); // start scanning for available WiFi-s
Expand Down Expand Up @@ -670,6 +670,27 @@ void WLED::initAP(bool resetAP)

void WLED::initConnection()
{
#ifdef ARDUINO_ARCH_ESP32
static bool firstCall = true;
bool updateWiFiNVM = false;
if (firstCall) {
// check cached WiFi config in flash, esp_wifi_get_config still contains NVM values at this point
// note: connecting to the NVM stored wifi is much faster and prevents boot-up glitches on LEDs (there is no benefit on ESP8266)
wifi_config_t cachedConfig;
esp_err_t configResult = esp_wifi_get_config( (wifi_interface_t)ESP_IF_WIFI_STA, &cachedConfig );
if( configResult == ESP_OK ) {
if (strncmp((const char*)cachedConfig.sta.ssid, multiWiFi[0].clientSSID, 32) != 0 ||
strncmp((const char*)cachedConfig.sta.password, multiWiFi[0].clientPass, 64) != 0) {
Comment on lines +682 to +683
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find WiFiConfig struct definition
find . -name "*.h" -o -name "*.cpp" | head -20
rg -n "struct WiFiConfig" -A 20 --type cpp --type h

Repository: wled/WLED

Length of output: 2112


🏁 Script executed:

#!/bin/bash
# Search for clientSSID and clientPass declarations
rg -n "clientSSID|clientPass" --type cpp --type h -B 1 -A 1 | head -50

Repository: wled/WLED

Length of output: 3274


🏁 Script executed:

#!/bin/bash
# Look for defined constants that might correspond to 32 or 64
rg -n "#define.*32|#define.*64" --type h | head -30
rg -n "const.*32|const.*64" --type cpp --type h | head -30

Repository: wled/WLED

Length of output: 5516


🏁 Script executed:

#!/bin/bash
# Find where cachedConfig is defined to understand its structure
rg -n "cachedConfig" wled00/wled.cpp -B 2 -A 2 | head -40

Repository: wled/WLED

Length of output: 828


🏁 Script executed:

#!/bin/bash
# Verify the ESP-IDF wifi_sta_config_t sizes mentioned in the review
# Check if WLED uses ESP-IDF types or defines its own
rg -n "wifi_sta_config_t" wled00/ --type cpp -B 1 -A 1

Repository: wled/WLED

Length of output: 35


🏁 Script executed:

#!/bin/bash
# Check the actual buffer sizes used in other parts of code
rg -n "clientSSID\[|clientPass\[" wled00/ --type cpp --type h

Repository: wled/WLED

Length of output: 304


Use sizeof() instead of hardcoded buffer sizes in strncmp calls.

The hardcoded 32 and 64 at lines 682–683 correspond to the ESP-IDF wifi_sta_config_t.ssid[32] and .password[64] field sizes. Using sizeof(cachedConfig.sta.ssid) and sizeof(cachedConfig.sta.password) is more maintainable and makes the intent explicit. The WLED WiFiConfig buffers (clientSSID[33] and clientPass[65]) have sufficient capacity to hold the data.

Additionally, fix the if( spacing at line 681 (should be if () to match the style of surrounding code.

♻️ Proposed fix
-    if( configResult == ESP_OK ) {
-      if (strncmp((const char*)cachedConfig.sta.ssid, multiWiFi[0].clientSSID, 32) != 0 ||
-      strncmp((const char*)cachedConfig.sta.password, multiWiFi[0].clientPass, 64) != 0) {
+    if (configResult == ESP_OK) {
+      if (strncmp((const char*)cachedConfig.sta.ssid, multiWiFi[0].clientSSID, sizeof(cachedConfig.sta.ssid)) != 0 ||
+          strncmp((const char*)cachedConfig.sta.password, multiWiFi[0].clientPass, sizeof(cachedConfig.sta.password)) != 0) {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@wled00/wled.cpp` around lines 682 - 683, Replace hardcoded lengths in the
strncmp checks with sizeof on the ESP-IDF config fields: use
sizeof(cachedConfig.sta.ssid) and sizeof(cachedConfig.sta.password) when
comparing cachedConfig.sta.ssid/password to multiWiFi[0].clientSSID and
multiWiFi[0].clientPass via strncmp; also correct the spacing of the conditional
to "if (" to match surrounding style. Ensure you only change the arguments to
strncmp and the if( spacing—leave function names and array indexes
(cachedConfig, multiWiFi[0]) unchanged.

updateWiFiNVM = true; // SSID or pass changed, update NVM at next WiFi.begin() call
DEBUG_PRINTLN(F("WiFi config NVM update triggered"));
}
}
firstCall = false;
}
else
esp_wifi_set_storage(WIFI_STORAGE_RAM); // do not update NVM credentials while running to prevent wear on flash
#endif

DEBUG_PRINTF_P(PSTR("initConnection() called @ %lus.\n"), millis()/1000);
#ifdef WLED_ENABLE_WEBSOCKETS
ws.onEvent(wsEvent);
Expand Down Expand Up @@ -720,6 +741,11 @@ void WLED::initConnection()
char hostname[25];
prepareHostname(hostname);

#ifdef ARDUINO_ARCH_ESP32
if(updateWiFiNVM && selectedWiFi == 0) // NVM only can store one wifi: store credentials of first WiFi even if multiple are configured
esp_wifi_set_storage(WIFI_STORAGE_FLASH); // temporary override WiFi.persistent(false) to store credentials in flash (is reset to RAM on next initConnection() call)
#endif

#ifdef WLED_ENABLE_WPA_ENTERPRISE
if (multiWiFi[selectedWiFi].encryptionType == WIFI_ENCRYPTION_TYPE_PSK) {
DEBUG_PRINTLN(F("Using PSK"));
Expand Down