This is an automated email from the ASF dual-hosted git repository.
linguini pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 1914dd08209 net/socket: Check on the end of the NIC name when binding
device
1914dd08209 is described below
commit 1914dd08209619d9ed1c68b90de8422025e343bb
Author: zhangshuai39 <[email protected]>
AuthorDate: Tue Aug 19 11:00:26 2025 +0800
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 <[email protected]>
---
net/socket/setsockopt.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/net/socket/setsockopt.c b/net/socket/setsockopt.c
index 97b039a6ede..0d0cc013850 100644
--- a/net/socket/setsockopt.c
+++ b/net/socket/setsockopt.c
@@ -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;