Skip to content

Commit 4941711

Browse files
devyteearlephilhower
authored andcommitted
Implement for ssid a similar approach as for passphrase (#5411)
* Implement for ssid a similar approach as for passphrase * Additional fixes for 32-char ssid
1 parent d8acfff commit 4941711

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,19 @@ void ESP8266WiFiClass::printDiag(Print& p) {
7272
struct station_config conf;
7373
wifi_station_get_config(&conf);
7474

75-
const char* ssid = reinterpret_cast<const char*>(conf.ssid);
75+
char ssid[33]; //ssid can be up to 32chars, => plus null term
76+
memcpy(ssid, conf.ssid, sizeof(conf.ssid));
77+
ssid[32] = 0; //nullterm in case of 32 char ssid
78+
7679
p.print("SSID (");
7780
p.print(strlen(ssid));
7881
p.print("): ");
7982
p.println(ssid);
8083

81-
const char* passphrase = reinterpret_cast<const char*>(conf.password);
84+
char passphrase[65];
85+
memcpy(passphrase, conf.password, sizeof(conf.password));
86+
passphrase[64] = 0;
87+
8288
p.print("Passphrase (");
8389
p.print(strlen(passphrase));
8490
p.print("): ");

libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
187187

188188
WifiAPEntry newAP;
189189

190-
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
190+
if(!ssid || *ssid == 0x00 || strlen(ssid) > 32) {
191191
// fail SSID too long or missing!
192192
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] no ssid or ssid too long\n");
193193
return false;
@@ -230,7 +230,7 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
230230
}
231231

232232
bool ESP8266WiFiMulti::APlistExists(const char* ssid, const char *passphrase) {
233-
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
233+
if(!ssid || *ssid == 0x00 || strlen(ssid) > 32) {
234234
// fail SSID too long or missing!
235235
DEBUG_WIFI_MULTI("[WIFI][APlistExists] no ssid or ssid too long\n");
236236
return false;

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh
6262
* @return equal
6363
*/
6464
static bool sta_config_equal(const station_config& lhs, const station_config& rhs) {
65-
if(strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0) {
65+
if(strncmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid), sizeof(lhs.ssid)) != 0) {
6666
return false;
6767
}
6868

@@ -108,7 +108,7 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
108108
return WL_CONNECT_FAILED;
109109
}
110110

111-
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
111+
if(!ssid || *ssid == 0x00 || strlen(ssid) > 32) {
112112
// fail SSID too long or missing!
113113
return WL_CONNECT_FAILED;
114114
}
@@ -119,10 +119,12 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
119119
}
120120

121121
struct station_config conf;
122-
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
122+
if(strlen(ssid) == 32)
123+
memcpy(reinterpret_cast<char*>(conf.ssid), ssid, 32); //copied in without null term
124+
else
125+
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
123126

124127
conf.threshold.authmode = AUTH_OPEN;
125-
126128
if(passphrase) {
127129
conf.threshold.authmode = _useInsecureWEP ? AUTH_WEP : AUTH_WPA_PSK;
128130
if (strlen(passphrase) == 64) // it's not a passphrase, is the PSK, which is copied into conf.password without null term
@@ -524,7 +526,10 @@ wl_status_t ESP8266WiFiSTAClass::status() {
524526
String ESP8266WiFiSTAClass::SSID() const {
525527
struct station_config conf;
526528
wifi_station_get_config(&conf);
527-
return String(reinterpret_cast<char*>(conf.ssid));
529+
char tmp[33]; //ssid can be up to 32chars, => plus null term
530+
memcpy(tmp, conf.ssid, sizeof(conf.ssid));
531+
tmp[32] = 0; //nullterm in case of 32 char ssid
532+
return String(reinterpret_cast<char*>(tmp));
528533
}
529534

530535
/**

libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ String ESP8266WiFiScanClass::SSID(uint8_t i) {
186186
if(!it) {
187187
return "";
188188
}
189+
char tmp[33]; //ssid can be up to 32chars, => plus null term
190+
memcpy(tmp, it->ssid, sizeof(it->ssid));
191+
tmp[32] = 0; //nullterm in case of 32 char ssid
189192

190-
return String(reinterpret_cast<const char*>(it->ssid));
193+
return String(reinterpret_cast<const char*>(tmp));
191194
}
192195

193196

0 commit comments

Comments
 (0)