-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Conversation
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'; |
There was a problem hiding this comment.
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';
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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];
tochar 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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
Making DNM until maintainers reply to the comments above. |
I'll close this PR to apply the changes suggested by @brandon-exact. Thank for your reviewer this :) |
issue
#80066
Fixed '-Wstringop-truncation' warning thrown by ssid when using strncpy on esp32.
other code:
zephyr/drivers/wifi/nxp/nxp_wifi_drv.c
Lines 696 to 697 in 6f64f77
This code does not do this:
zephyr/drivers/wifi/esp32/src/esp_wifi_drv.c
Lines 736 to 740 in 6f64f77
Fixes: #80066