mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-09-06 17:14:02 +00:00
![]() nm_strndup_a() uses strncpy() because we want the behavior of clearing out the memory after the first NUL byte. But that can cause a compiler warning: CC src/settings/plugins/keyfile/libNetworkManager_la-nms-keyfile-utils.lo In file included from ../../shared/nm-default.h:279, from ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:20: In function ‘_nm_strndup_a_step’, inlined from ‘nms_keyfile_loaded_uuid_is_filename’ at ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:65:9: ../../shared/nm-glib-aux/nm-macros-internal.h:1661:3: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=] 1661 | strncpy (s, str, len); | ^~~~~~~~~~~~~~~~~~~~~ ../../src/settings/plugins/keyfile/nms-keyfile-utils.c: In function ‘nms_keyfile_loaded_uuid_is_filename’: ../../src/settings/plugins/keyfile/nms-keyfile-utils.c:48:8: note: length computed here 48 | len = strlen (filename); | ^~~~~~~~~~~~~~~~~ It's true that the len argument of _nm_strndup_a_step() depends on the string length of the source string. But in this case it's safe, because we checked that the destination buffer is exactly the right size too. By that reasoning we should use memcpy() or strcpy(), but both are unsuitable. That is because we want nm_strndup_a() to behave like strndup(), which means we need to handle cases where the len argument is larger than the string length of the source string. That is, we want always to return a buffer of size len+1, but we want to copy only the characters up to the first NUL byte, and clear out the rest. That's what strncpy() does for us. Silence the warning. |
||
---|---|---|
.. | ||
c-list | ||
c-rbtree | ||
c-siphash | ||
c-stdaux | ||
n-acd | ||
n-dhcp4 | ||
nm-glib-aux | ||
nm-libnm-core-aux | ||
nm-libnm-core-intern | ||
nm-std-aux | ||
nm-udev-aux | ||
nm-utils | ||
systemd | ||
meson.build | ||
nm-default.h | ||
nm-meta-setting.c | ||
nm-meta-setting.h | ||
nm-test-libnm-utils.h | ||
nm-test-utils-impl.c | ||
nm-version-macros.h.in |