Skip to content
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

drivers: esp32: Fix esp_wifi_drv strncpy warning #80107

Closed
wants to merge 1 commit into from

Conversation

rruuaanng
Copy link
Contributor

@rruuaanng rruuaanng commented Oct 20, 2024

issue

#80066

Fixed '-Wstringop-truncation' warning thrown by ssid when using strncpy on esp32.

other code:

strncpy(wlan_scan_params_v2.ssid[1], params->ssids[1], WIFI_SSID_MAX_LEN);
wlan_scan_params_v2.ssid[1][WIFI_SSID_MAX_LEN - 1] = 0;

This code does not do this:

strncpy(status->ssid, data->status.ssid, WIFI_SSID_MAX_LEN);
status->ssid_len = strnlen(data->status.ssid, WIFI_SSID_MAX_LEN);
status->band = WIFI_FREQ_BAND_2_4_GHZ;
status->link_mode = WIFI_LINK_MODE_UNKNOWN;
status->mfp = WIFI_MFP_DISABLE;

Fixes: #80066

@rruuaanng rruuaanng marked this pull request as ready for review October 20, 2024 13:30
@zephyrbot zephyrbot added area: Wi-Fi Wi-Fi size: XS A PR changing only a single line of code platform: ESP32 Espressif ESP32 labels Oct 20, 2024
Fixed '-Wstringop-truncation' warning thrown by
ssid when using strncpy on esp32.

Signed-off-by: James Roy <rruuaanng@outlook.com>
@@ -734,6 +734,7 @@ static int esp32_wifi_status(const struct device *dev, struct wifi_iface_status
}

strncpy(status->ssid, data->status.ssid, WIFI_SSID_MAX_LEN);
status->ssid[WIFI_SSID_MAX_LEN] = '\0';
Copy link
Contributor

@brandon-exact brandon-exact Oct 22, 2024

Choose a reason for hiding this comment

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

@jukkar
AFAICT status->ssid is 32 bytes. currently this change gives

/home/brandon/fork/zephyr/drivers/wifi/esp32/src/esp_wifi_drv.c: In function 'esp32_wifi_status':
/home/brandon/fork/zephyr/drivers/wifi/esp32/src/esp_wifi_drv.c:737:21: warning: array subscript 32 is above array bounds of 'char[32]' [-Warray-bounds]
  737 |         status->ssid[WIFI_SSID_MAX_LEN] = '\0';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually in my previous submission, it was [WIFI_SSID_MAX_LEN-1]. But it seems the auditors had issues with it, so I changed it

Copy link
Collaborator

@sylvioalves sylvioalves Oct 22, 2024

Choose a reason for hiding this comment

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

I agree, it should be [WIFI_SSID_MAX_LEN - 1].

@jukkar @krish2718 should we change the API from
char ssid[WIFI_SSID_MAX_LEN]; to
char ssid[WIFI_SSID_MAX_LEN + 1]; as a proper solution instead?

https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/net/wifi_mgmt.h#L432
https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/net/wifi_mgmt.h#L432

If yes, I suggest closing this PR and opening another with that solution.

Copy link
Member

Choose a reason for hiding this comment

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

@jukkar @krish2718 should we change the API from char ssid[WIFI_SSID_MAX_LEN]; to char ssid[WIFI_SSID_MAX_LEN + 1]; as a proper solution instead?

https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/net/wifi_mgmt.h#L432 https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/net/wifi_mgmt.h#L432

Yes, that is correct. If we want to store null at the end, there needs to be space for it in the buffer (so +1 is needed).

Copy link
Contributor Author

@rruuaanng rruuaanng Oct 22, 2024

Choose a reason for hiding this comment

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

I think it's good to change it to WIFI_SSID_MAX_LEN-1. Instead of changing the definition, because WIFI_SSID_MAX_LEN already indicates the maximum length. If you +1 to the maximum length, it will make the code extremely difficult to understand.

Edit:
And, in the example I gave, we should keep in sync with him instead of changing the definition at will.

Copy link
Contributor

@brandon-exact brandon-exact Oct 22, 2024

Choose a reason for hiding this comment

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

The standard for SSID is 32 bytes long. The way it is now, it will only be 31 bytes due to the '\0' so it must be increased if we want null termination and 32 bytes.

I am surprised this change passed CI, the warning gets created without the CONFIG_SPEED_OPTIMIZATIONS

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it's okay, I'll reopen a PR tomorrow. It'll change the size of the ssid member (even though I’m not a big fan of this, but if the ssid is the standard 32-byte length, then the ssid member has to be 33 bytes).

@sylvioalves sylvioalves added the DNM This PR should not be merged (Do Not Merge) label Oct 22, 2024
@sylvioalves
Copy link
Collaborator

Making DNM until maintainers reply to the comments above.

@rruuaanng
Copy link
Contributor Author

I'll close this PR to apply the changes suggested by @brandon-exact.

Thank for your reviewer this :)

@rruuaanng
Copy link
Contributor Author

#80267

@rruuaanng rruuaanng closed this Oct 23, 2024
@rruuaanng rruuaanng deleted the esp32_warn branch October 23, 2024 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Wi-Fi Wi-Fi DNM This PR should not be merged (Do Not Merge) platform: ESP32 Espressif ESP32 size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

drivers: esp32: esp_wifi_drv strncpy warning
7 participants