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

xiaoxiang781216 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 0e350f26fb6 net: avoid temporary allocation in procfs device lookup
0e350f26fb6 is described below

commit 0e350f26fb602074f32b7fe1efa5598df8a45b29
Author: bigcat2026 <[email protected]>
AuthorDate: Fri Nov 20 15:32:22 2026 +0800

    net: avoid temporary allocation in procfs device lookup
    
    The procfs network layer used strdup()/basename() on every open and
    stat lookup, allocating and freeing temporary buffers.  Use a small
    stack buffer and a local helper to extract the device name instead.
    This avoids a heap allocation failure path and reduces churn during
    procfs network device lookups.
    
    Signed-off-by: bigcat2026 <[email protected]>
---
 net/procfs/net_procfs.c | 69 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 49 insertions(+), 20 deletions(-)

diff --git a/net/procfs/net_procfs.c b/net/procfs/net_procfs.c
index 00d21db3a76..a12c97192a7 100644
--- a/net/procfs/net_procfs.c
+++ b/net/procfs/net_procfs.c
@@ -29,19 +29,18 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include <net/if.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <string.h>
 #include <fcntl.h>
 #include <fnmatch.h>
-#include <libgen.h>
 #include <assert.h>
 #include <errno.h>
 #include <nuttx/debug.h>
 
 #include <sys/param.h>
 
-#include <nuttx/lib/lib.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/fs/fs.h>
 #include <nuttx/fs/procfs.h>
@@ -97,6 +96,8 @@ static int     netprocfs_readdir(FAR struct fs_dirent_s *dir,
 static int     netprocfs_rewinddir(FAR struct fs_dirent_s *dir);
 
 static int     netprocfs_stat(FAR const char *relpath, FAR struct stat *buf);
+static FAR const char *netprocfs_devname(FAR const char *relpath,
+                                         FAR char *buffer, size_t buflen);
 
 /****************************************************************************
  * Private Data
@@ -232,23 +233,20 @@ static int netprocfs_open(FAR struct file *filep, FAR 
const char *relpath,
 
   if (i == nitems(g_net_entries) - 1)
     {
-      FAR char *devname;
-      FAR char *copy;
+      char devname[IFNAMSIZ];
+      FAR const char *name;
 
       /* Otherwise, we need to search the list of registered network devices
        * to determine if the name corresponds to a network device.
        */
 
-      copy = strdup(relpath);
-      if (copy == NULL)
+      name = netprocfs_devname(relpath, devname, sizeof(devname));
+      if (name == NULL)
         {
-          ferr("ERROR: strdup failed\n");
-          return -ENOMEM;
+          return -ENOENT;
         }
 
-      devname = basename(copy);
-      dev     = netdev_findbyname(devname);
-      lib_free(copy);
+      dev = netdev_findbyname(name);
 
       if (dev == NULL)
         {
@@ -280,6 +278,40 @@ static int netprocfs_open(FAR struct file *filep, FAR 
const char *relpath,
   return OK;
 }
 
+/****************************************************************************
+ * Name: netprocfs_devname
+ ****************************************************************************/
+
+static FAR const char *netprocfs_devname(FAR const char *relpath,
+                                         FAR char *buffer, size_t buflen)
+{
+  FAR const char *start;
+  FAR const char *end;
+  size_t length;
+
+  end = relpath + strlen(relpath);
+  while (end > relpath && *(end - 1) == '/')
+    {
+      end--;
+    }
+
+  start = end;
+  while (start > relpath && *(start - 1) != '/')
+    {
+      start--;
+    }
+
+  length = end - start;
+  if (length == 0 || length >= buflen)
+    {
+      return NULL;
+    }
+
+  memcpy(buffer, start, length);
+  buffer[length] = '\0';
+  return buffer;
+}
+
 /****************************************************************************
  * Name: netprocfs_close
  ****************************************************************************/
@@ -658,23 +690,20 @@ static int netprocfs_stat(FAR const char *relpath, FAR 
struct stat *buf)
   if (buf->st_mode == 0)
     {
       FAR struct net_driver_s *dev;
-      FAR char *devname;
-      FAR char *copy;
+      char devname[IFNAMSIZ];
+      FAR const char *name;
 
       /* Otherwise, we need to search the list of registered network devices
        * to determine if the name corresponds to a network device.
        */
 
-      copy = strdup(relpath);
-      if (copy == NULL)
+      name = netprocfs_devname(relpath, devname, sizeof(devname));
+      if (name == NULL)
         {
-          ferr("ERROR: strdup failed\n");
-          return -ENOMEM;
+          return -ENOENT;
         }
 
-      devname = basename(copy);
-      dev     = netdev_findbyname(devname);
-      lib_free(copy);
+      dev = netdev_findbyname(name);
 
       if (dev == NULL)
         {

Reply via email to