Skip to content

Implement for ssid a similar approach as for passphrase #5411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ void ESP8266WiFiClass::printDiag(Print& p) {
struct station_config conf;
wifi_station_get_config(&conf);

const char* ssid = reinterpret_cast<const char*>(conf.ssid);
char ssid[33]; //ssid can be up to 32chars, => plus null term
memcpy(ssid, conf.ssid, sizeof(conf.ssid));
ssid[32] = 0; //nullterm in case of 32 char ssid

p.print("SSID (");
p.print(strlen(ssid));
p.print("): ");
p.println(ssid);

const char* passphrase = reinterpret_cast<const char*>(conf.password);
char passphrase[65];
memcpy(passphrase, conf.password, sizeof(conf.password));
passphrase[64] = 0;

p.print("Passphrase (");
p.print(strlen(passphrase));
p.print("): ");
Expand Down
4 changes: 2 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {

WifiAPEntry newAP;

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

bool ESP8266WiFiMulti::APlistExists(const char* ssid, const char *passphrase) {
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
if(!ssid || *ssid == 0x00 || strlen(ssid) > 32) {
// fail SSID too long or missing!
DEBUG_WIFI_MULTI("[WIFI][APlistExists] no ssid or ssid too long\n");
return false;
Expand Down
15 changes: 10 additions & 5 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh
* @return equal
*/
static bool sta_config_equal(const station_config& lhs, const station_config& rhs) {
if(strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0) {
if(strncmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid), sizeof(lhs.ssid)) != 0) {
return false;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
return WL_CONNECT_FAILED;
}

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

struct station_config conf;
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
if(strlen(ssid) == 32)
Copy link
Collaborator

Choose a reason for hiding this comment

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

strncpy(reinterpret_cast<char*>(conf.ssid), ssid, 32); will do the same w/o any if required.

From the man page, this is one of the intended uses:

   One  valid  (and  intended) use of strncpy() is to copy a C string to a fixed-length buffer while ensuring both that the buffer is not overflowed and that unused bytes in the target buffer are zeroed out (perhaps to prevent information leaks if the buffer is to be written to media or transmitted to another process via an interprocess communication technique).

memcpy(reinterpret_cast<char*>(conf.ssid), ssid, 32); //copied in without null term
else
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);

conf.threshold.authmode = AUTH_OPEN;

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

/**
Expand Down
5 changes: 4 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ String ESP8266WiFiScanClass::SSID(uint8_t i) {
if(!it) {
return "";
}
char tmp[33]; //ssid can be up to 32chars, => plus null term
memcpy(tmp, it->ssid, sizeof(it->ssid));
tmp[32] = 0; //nullterm in case of 32 char ssid

return String(reinterpret_cast<const char*>(it->ssid));
return String(reinterpret_cast<const char*>(tmp));
}


Expand Down