net/socket: Check on the end of the NIC name when binding device

When using usrsock to pass the network interface name, omitting "\0" will cause the host to parse extra characters. therefore, the tail section should be inspected during device binding.

Signed-off-by: zhangshuai39 <zhangshuai39@xiaomi.com>
This commit is contained in:
zhangshuai39
2025-08-19 11:00:26 +08:00
committed by Matteo Golin
parent d5633f75a8
commit 1914dd0820
+20 -4
View File
@@ -203,11 +203,27 @@ static int psock_socketlevel_option(FAR struct socket *psock, int option,
break;
}
/* No, we are binding a socket to the interface
* Find the interface device with this name.
*/
/* Check if the value is already null-terminated */
if (((FAR char *)value)[value_len - 1] != '\0')
{
char ifname[IFNAMSIZ];
socklen_t len = MIN(IFNAMSIZ - 1, value_len);
/* Copy the data and add null terminator */
memcpy(ifname, value, len);
ifname[len] = '\0';
dev = netdev_findbyname(ifname);
}
else
{
/* Value is already null-terminated, use it directly */
dev = netdev_findbyname(value);
}
dev = netdev_findbyname(value);
if (dev == NULL)
{
return -ENODEV;