net: mdns_responder: Use memcpy instead of strncpy for iface name

Following warning is printed if using strncpy(), so use memcpy()
instead. Note that this is false positive as there is no error here but
in order to avoid the warning, change the copy function.

subsys/net/lib/dns/mdns_responder.c:1371:25: warning:
'strncpy' output may be truncated copying 7 bytes from a string
of length 8 [-Wstringop-truncation]
 1468 | strncpy(if_req.ifr_name, name, sizeof(if_req.ifr_name) - 1);
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

subsys/net/lib/dns/mdns_responder.c:1468:25: warning:
'strncpy' output may be truncated copying 7 bytes from a string
of length 8 [-Wstringop-truncation]
 1468 | strncpy(if_req.ifr_name, name, sizeof(if_req.ifr_name) - 1);
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2025-03-19 15:21:40 +02:00 committed by Benjamin Cabé
commit cc282e56e8

View file

@ -1368,7 +1368,8 @@ static int init_listener(void)
ifindex, ret);
} else {
memset(&if_req, 0, sizeof(if_req));
strncpy(if_req.ifr_name, name, sizeof(if_req.ifr_name) - 1);
memcpy(if_req.ifr_name, name,
MIN(sizeof(name) - 1, sizeof(if_req.ifr_name) - 1));
ret = zsock_setsockopt(v6, SOL_SOCKET, SO_BINDTODEVICE,
&if_req, sizeof(if_req));
@ -1464,7 +1465,8 @@ static int init_listener(void)
ifindex, ret);
} else {
memset(&if_req, 0, sizeof(if_req));
strncpy(if_req.ifr_name, name, sizeof(if_req.ifr_name) - 1);
memcpy(if_req.ifr_name, name,
MIN(sizeof(name) - 1, sizeof(if_req.ifr_name) - 1));
ret = zsock_setsockopt(v4, SOL_SOCKET, SO_BINDTODEVICE,
&if_req, sizeof(if_req));