Skip to content

Commit 8e4a2d2

Browse files
committed
Fix build error because strncpy tries to write to the same size buffer
1 parent 113a043 commit 8e4a2d2

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

libraries/WiFi/src/WiFiAP.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ esp_netif_t* get_esp_interface_netif(esp_interface_t interface);
5050
esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPAddress(), IPAddress gateway=IPAddress(), IPAddress subnet=IPAddress());
5151
static bool softap_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs);
5252

53-
53+
static size_t _wifi_strncpy(char * dst, const char * src, size_t dst_len){
54+
if(!dst || !src || !dst_len){
55+
return 0;
56+
}
57+
size_t src_len = strlen(src);
58+
if(src_len >= dst_len){
59+
src_len = dst_len;
60+
} else {
61+
src_len += 1;
62+
}
63+
memcpy(dst, src, src_len);
64+
return src_len;
65+
}
5466

5567
/**
5668
* compare two AP configurations
@@ -98,12 +110,12 @@ void wifi_softap_config(wifi_config_t *wifi_config, const char * ssid=NULL, cons
98110
wifi_config->ap.password[0] = 0;
99111
wifi_config->ap.ftm_responder = ftm_responder;
100112
if(ssid != NULL && ssid[0] != 0){
101-
strncpy((char*)wifi_config->ap.ssid, ssid, 32);
113+
_wifi_strncpy((char*)wifi_config->ap.ssid, ssid, 32);
102114
wifi_config->ap.ssid_len = strlen(ssid);
103115
if(password != NULL && password[0] != 0){
104116
wifi_config->ap.authmode = authmode;
105117
wifi_config->ap.pairwise_cipher = WIFI_CIPHER_TYPE_CCMP; // Disable by default enabled insecure TKIP and use just CCMP.
106-
strncpy((char*)wifi_config->ap.password, password, 64);
118+
_wifi_strncpy((char*)wifi_config->ap.password, password, 64);
107119
}
108120
}
109121
}

libraries/WiFi/src/WiFiSTA.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ esp_err_t set_esp_interface_dns(esp_interface_t interface, IPAddress main_dns=IP
5353
esp_err_t set_esp_interface_ip(esp_interface_t interface, IPAddress local_ip=IPAddress(), IPAddress gateway=IPAddress(), IPAddress subnet=IPAddress());
5454
static bool sta_config_equal(const wifi_config_t& lhs, const wifi_config_t& rhs);
5555

56+
static size_t _wifi_strncpy(char * dst, const char * src, size_t dst_len){
57+
if(!dst || !src || !dst_len){
58+
return 0;
59+
}
60+
size_t src_len = strlen(src);
61+
if(src_len >= dst_len){
62+
src_len = dst_len;
63+
} else {
64+
src_len += 1;
65+
}
66+
memcpy(dst, src, src_len);
67+
return src_len;
68+
}
69+
5670

5771
/**
5872
* compare two STA configurations
@@ -82,10 +96,10 @@ static void wifi_sta_config(wifi_config_t * wifi_config, const char * ssid=NULL,
8296
wifi_config->sta.ssid[0] = 0;
8397
wifi_config->sta.password[0] = 0;
8498
if(ssid != NULL && ssid[0] != 0){
85-
strncpy((char*)wifi_config->sta.ssid, ssid, 32);
99+
_wifi_strncpy((char*)wifi_config->sta.ssid, ssid, 32);
86100
if(password != NULL && password[0] != 0){
87101
wifi_config->sta.threshold.authmode = WIFI_AUTH_WEP;
88-
strncpy((char*)wifi_config->sta.password, password, 64);
102+
_wifi_strncpy((char*)wifi_config->sta.password, password, 64);
89103
}
90104
if(bssid != NULL){
91105
wifi_config->sta.bssid_set = 1;
@@ -161,11 +175,11 @@ wl_status_t WiFiSTAClass::begin(const char* ssid, const char *passphrase, int32_
161175

162176
wifi_config_t conf;
163177
memset(&conf, 0, sizeof(wifi_config_t));
164-
strncpy(reinterpret_cast<char*>(conf.sta.ssid), ssid, 32);
178+
_wifi_strncpy(reinterpret_cast<char*>(conf.sta.ssid), ssid, 32);
165179
conf.sta.scan_method = WIFI_ALL_CHANNEL_SCAN; //force full scan to be able to choose the nearest / strongest AP
166180

167181
if(passphrase) {
168-
strncpy(reinterpret_cast<char*>(conf.sta.password), passphrase, 64);
182+
_wifi_strncpy(reinterpret_cast<char*>(conf.sta.password), passphrase, 64);
169183
}
170184

171185
wifi_config_t current_conf;

0 commit comments

Comments
 (0)