Commit 6291ca7
committed
bug: Fix unsafe use of strncpy
While working on the MicroPython v1.20.0 update I came across the
following error:
```
$~/badgePython/components/pywpa2enterprise/modwpa2enterprise.c:25:5: error: 'strncpy' specified bound 32 equals destination size [-Werror=stringop-truncation]
strncpy((char*) wifi_config.sta.ssid, aSsid, 32);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
```
The error message indicates that the `strncpy()` function call in the
provided code can potentially write more than 32 bytes to the
destination buffer, which could result in a buffer overflow and
undefined behavior.
To resolve this error, you can use `strlcpy()` instead of `strncpy()`.
`strlcpy()` is a safer alternative to `strncpy()`, as it guarantees
null-termination of the destination string and does not write more than
the specified destination size.
Thus, the modified version should look like this:
```c
strlcpy((char*) wifi_config.sta.ssid, aSsid, sizeof(wifi_config.sta.ssid));
```
This will copy at most `sizeof(wifi_config.sta.ssid)` bytes from `aSsid`
to `wifi_config.sta.ssid`, ensuring that the destination buffer is not
overflowed.1 parent e667fb9 commit 6291ca7
1 file changed
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
0 commit comments