Merge fix/wifi-manager-dangling-pointer: Cache SSID to fix use-after-free
This commit is contained in:
commit
69a2e04cd2
@ -1289,9 +1289,9 @@ const char* MyMesh::getMQTTStatus() const {
|
||||
|
||||
const char* MyMesh::getWiFiStatus() const {
|
||||
switch (_wifi_mgr.getState()) {
|
||||
case WiFiState::CONNECTED: return "connected";
|
||||
case WiFiState::CONNECTING: return "connecting";
|
||||
case WiFiState::AP_MODE: return "ap_mode";
|
||||
case NetworkState::CONNECTED: return "connected";
|
||||
case NetworkState::CONNECTING: return "connecting";
|
||||
case NetworkState::AP_MODE: return "ap_mode";
|
||||
default: return "disconnected";
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ WiFiManager::WiFiManager()
|
||||
_retry_count(0),
|
||||
_initialized(false) {
|
||||
memset(&_config, 0, sizeof(_config));
|
||||
memset(_cached_ssid, 0, sizeof(_cached_ssid));
|
||||
}
|
||||
|
||||
void WiFiManager::begin(const WiFiConfig& config) {
|
||||
@ -82,6 +83,9 @@ void WiFiManager::loop() {
|
||||
_state = WiFiState::CONNECTED;
|
||||
_connected_since = millis();
|
||||
_retry_count = 0;
|
||||
// Cache SSID to avoid dangling pointer from WiFi.SSID().c_str()
|
||||
strncpy(_cached_ssid, WiFi.SSID().c_str(), sizeof(_cached_ssid) - 1);
|
||||
_cached_ssid[sizeof(_cached_ssid) - 1] = '\0';
|
||||
Serial.printf("[WiFi] Connected! IP: %s, RSSI: %d dBm\n",
|
||||
WiFi.localIP().toString().c_str(), WiFi.RSSI());
|
||||
} else if (status == WL_NO_SSID_AVAIL) {
|
||||
@ -184,6 +188,9 @@ void WiFiManager::startAPMode() {
|
||||
|
||||
if (success) {
|
||||
_state = WiFiState::AP_MODE;
|
||||
// Cache AP SSID to avoid dangling pointer
|
||||
strncpy(_cached_ssid, ap_ssid.c_str(), sizeof(_cached_ssid) - 1);
|
||||
_cached_ssid[sizeof(_cached_ssid) - 1] = '\0';
|
||||
Serial.printf("[WiFi] AP started: SSID='%s', IP=%s\n",
|
||||
ap_ssid.c_str(), WiFi.softAPIP().toString().c_str());
|
||||
} else {
|
||||
@ -221,10 +228,8 @@ int32_t WiFiManager::getRSSI() const {
|
||||
}
|
||||
|
||||
const char* WiFiManager::getSSID() const {
|
||||
if (_state == WiFiState::CONNECTED) {
|
||||
return WiFi.SSID().c_str();
|
||||
} else if (_state == WiFiState::AP_MODE) {
|
||||
return WiFi.softAPSSID().c_str();
|
||||
if (_state == WiFiState::CONNECTED || _state == WiFiState::AP_MODE) {
|
||||
return _cached_ssid;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@ -264,10 +269,8 @@ NetworkState WiFiManager::getState() const {
|
||||
}
|
||||
|
||||
const char* WiFiManager::getConnectionName() const {
|
||||
if (_state == WiFiState::CONNECTED) {
|
||||
return WiFi.SSID().c_str();
|
||||
} else if (_state == WiFiState::AP_MODE) {
|
||||
return WiFi.softAPSSID().c_str();
|
||||
if (_state == WiFiState::CONNECTED || _state == WiFiState::AP_MODE) {
|
||||
return _cached_ssid;
|
||||
}
|
||||
return "Not connected";
|
||||
}
|
||||
|
||||
@ -61,6 +61,7 @@ private:
|
||||
unsigned long _connected_since;
|
||||
uint8_t _retry_count;
|
||||
bool _initialized;
|
||||
char _cached_ssid[33]; // Cached SSID to avoid dangling pointer from WiFi.SSID().c_str()
|
||||
|
||||
void attemptConnection();
|
||||
void checkConnection();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user