This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch releases/12.12
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/releases/12.12 by this push:
     new a3c914c50d3 net/socket: Check on the end of the NIC name when binding 
device
a3c914c50d3 is described below

commit a3c914c50d31f6e9eb99166d4e547248dd3b238a
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 5e3a59157a8..8bbfc038b48 100644
--- a/net/socket/setsockopt.c
+++ b/net/socket/setsockopt.c
@@ -202,11 +202,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;

Reply via email to