mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-17 23:28:15 +00:00
provisioning: use memcpy instead of strncpy for copying SSID
Per WiFi library requirement, SSID can be non-null terminated string if its length goes to 32 bytes (maximum). Use of strncpy in this case, along with compiler optimization level -O2 results in some warnings for potential use of non-null terminated strings. Fix here ensures use of memcpy to copy SSID string upto appropriate desired length. This helps to avoid compiler specific workaround flags added earlier. Closes https://github.com/espressif/esp-idf/issues/5866 Closes IDFGH-3983
This commit is contained in:
@@ -105,10 +105,15 @@ static esp_err_t set_config_handler(const wifi_prov_config_set_data_t *req_data,
|
||||
|
||||
ESP_LOGD(TAG, "Wi-Fi Credentials Received");
|
||||
|
||||
/* Using strncpy allows the max SSID length to be 32 bytes (as per 802.11 standard).
|
||||
/* Using memcpy allows the max SSID length to be 32 bytes (as per 802.11 standard).
|
||||
* But this doesn't guarantee that the saved SSID will be null terminated, because
|
||||
* wifi_cfg->sta.ssid is also 32 bytes long (without extra 1 byte for null character) */
|
||||
strncpy((char *) wifi_cfg->sta.ssid, req_data->ssid, sizeof(wifi_cfg->sta.ssid));
|
||||
* wifi_cfg->sta.ssid is also 32 bytes long (without extra 1 byte for null character).
|
||||
* Although, this is not a matter for concern because esp_wifi library reads the SSID
|
||||
* upto 32 bytes in absence of null termination */
|
||||
const size_t ssid_len = strnlen(req_data->ssid, sizeof(wifi_cfg->sta.ssid));
|
||||
/* Ensure SSID less than 32 bytes is null terminated */
|
||||
memset(wifi_cfg->sta.ssid, 0, sizeof(wifi_cfg->sta.ssid));
|
||||
memcpy(wifi_cfg->sta.ssid, req_data->ssid, ssid_len);
|
||||
|
||||
/* Using strlcpy allows both max passphrase length (63 bytes) and ensures null termination
|
||||
* because size of wifi_cfg->sta.password is 64 bytes (1 extra byte for null character) */
|
||||
|
Reference in New Issue
Block a user