Skip to content

Commit 92b2046

Browse files
committed
use strncpy and strncmp for WiFi SSID and Password in AP and STA
Fixes: #5367
1 parent 7e432b0 commit 92b2046

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

Diff for: libraries/WiFi/src/WiFiAP.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ static bool softap_config_equal(const wifi_config_t& lhs, const wifi_config_t& r
6060
*/
6161
static bool softap_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs)
6262
{
63-
if(strcmp(reinterpret_cast<const char*>(lhs.ap.ssid), reinterpret_cast<const char*>(rhs.ap.ssid)) != 0) {
63+
if(strncmp(reinterpret_cast<const char*>(lhs.ap.ssid), reinterpret_cast<const char*>(rhs.ap.ssid), 32) != 0) {
6464
return false;
6565
}
66-
if(strcmp(reinterpret_cast<const char*>(lhs.ap.password), reinterpret_cast<const char*>(rhs.ap.password)) != 0) {
66+
if(strncmp(reinterpret_cast<const char*>(lhs.ap.password), reinterpret_cast<const char*>(rhs.ap.password), 64) != 0) {
6767
return false;
6868
}
6969
if(lhs.ap.channel != rhs.ap.channel) {
@@ -98,12 +98,12 @@ void wifi_softap_config(wifi_config_t *wifi_config, const char * ssid=NULL, cons
9898
wifi_config->ap.password[0] = 0;
9999
wifi_config->ap.ftm_responder = ftm_responder;
100100
if(ssid != NULL && ssid[0] != 0){
101-
snprintf((char*)wifi_config->ap.ssid, 32, ssid);
101+
strncpy((char*)wifi_config->ap.ssid, ssid, 32);
102102
wifi_config->ap.ssid_len = strlen(ssid);
103103
if(password != NULL && password[0] != 0){
104104
wifi_config->ap.authmode = authmode;
105105
wifi_config->ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP; // Disable by default enabled insecure TKIP and use just CCMP.
106-
snprintf((char*)wifi_config->ap.password, 64, password);
106+
strncpy((char*)wifi_config->ap.password, password, 64);
107107
}
108108
}
109109
}

Diff for: libraries/WiFi/src/WiFiSTA.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,10 @@ static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL,
8282
wifi_config->sta.ssid[0] = 0;
8383
wifi_config->sta.password[0] = 0;
8484
if(ssid != NULL && ssid[0] != 0){
85-
snprintf((char*)wifi_config->sta.ssid, 32, ssid);
85+
strncpy((char*)wifi_config->sta.ssid, ssid, 32);
8686
if(password != NULL && password[0] != 0){
8787
wifi_config->sta.threshold.authmode = WIFI_AUTH_WEP;
88-
if(strlen(password) == 64){
89-
memcpy((char*)wifi_config->sta.password, password, 64);
90-
} else {
91-
snprintf((char*)wifi_config->sta.password, 64, password);
92-
}
88+
strncpy((char*)wifi_config->sta.password, password, 64);
9389
}
9490
if(bssid != NULL){
9591
wifi_config->sta.bssid_set = 1;
@@ -165,15 +161,11 @@ wl_status_t WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_
165161

166162
wifi_config_t conf;
167163
memset(&conf, 0, sizeof(wifi_config_t));
168-
strcpy(reinterpret_cast<char*>(conf.sta.ssid), ssid);
164+
strncpy(reinterpret_cast<char*>(conf.sta.ssid), ssid, 32);
169165
conf.sta.scan_method = WIFI_ALL_CHANNEL_SCAN; //force full scan to be able to choose the nearest / strongest AP
170166

171167
if(passphrase) {
172-
if (strlen(passphrase) == 64){ // it's not a passphrase, is the PSK
173-
memcpy(reinterpret_cast<char*>(conf.sta.password), passphrase, 64);
174-
} else {
175-
strcpy(reinterpret_cast<char*>(conf.sta.password), passphrase);
176-
}
168+
strncpy(reinterpret_cast<char*>(conf.sta.password), passphrase, 64);
177169
}
178170

179171
wifi_config_t current_conf;

0 commit comments

Comments
 (0)