xiaoxiang781216 commented on code in PR #21029:
URL: https://github.com/apache/nuttx/pull/21029#discussion_r4387329309


##########
fs/inode/fs_inodesearch.c:
##########
@@ -325,39 +325,99 @@ static int _inode_canonicalize(FAR char *path, FAR char 
*dst_min)
 
 /****************************************************************************
  * Name: _inode_checkpath
+ *
+ * Description:
+ *   Check path length constraints.  Returns 1 if the path is already a
+ *   simple absolute path that needs no canonicalization.
  ****************************************************************************/
 
-static int _inode_checkpath(const char *path)
+static int _inode_checkpath(FAR const char *path)
 {
+  bool is_simple = *path == '/';
   int namelen = 0;
   int pathlen = 0;
+  bool dot = false;
+  bool dotdot = false;
 
   if (*path == '\0')
     {
       return -ENOENT;
     }
 
+  if (is_simple)

Review Comment:
   why need handle the first char specially?



##########
fs/inode/fs_inodesearch.c:
##########
@@ -325,39 +325,99 @@ static int _inode_canonicalize(FAR char *path, FAR char 
*dst_min)
 
 /****************************************************************************
  * Name: _inode_checkpath
+ *
+ * Description:
+ *   Check path length constraints.  Returns 1 if the path is already a
+ *   simple absolute path that needs no canonicalization.
  ****************************************************************************/
 
-static int _inode_checkpath(const char *path)
+static int _inode_checkpath(FAR const char *path)
 {
+  bool is_simple = *path == '/';
   int namelen = 0;
   int pathlen = 0;
+  bool dot = false;
+  bool dotdot = false;
 
   if (*path == '\0')
     {
       return -ENOENT;
     }
 
+  if (is_simple)
+    {
+      path++;
+      pathlen++;
+
+      if (*path == '\0')
+        {
+          return 1;
+        }
+
+      if (*path == '/')
+        {
+          is_simple = false;
+        }
+    }
+
   /* Check each segment of the path */
 
-  while (*path != '\0' && pathlen < PATH_MAX)
+  while (pathlen < PATH_MAX)
     {
-      if (*path == '/')
+      if (*path == '/' || *path == '\0')
         {
+          if (is_simple)
+            {
+              if (namelen == 0 || dot || dotdot ||
+                  (*path == '/' && (path[1] == '\0' || path[1] == '/')))
+                {
+                  is_simple = false;
+                }
+            }
+
+          if (*path == '\0')
+            {
+              break;
+            }
+
           namelen = 0;
+          dot = false;
+          dotdot = false;
         }
       else
         {
           if (++namelen > NAME_MAX)
             {
               return -ENAMETOOLONG;
             }
+
+          if (namelen == 1)
+            {
+              dot = *path == '.';
+              dotdot = dot;
+            }
+          else if (namelen == 2)
+            {
+              dot = false;
+              dotdot = dotdot && *path == '.';
+            }
+          else
+            {
+              dot = false;
+              dotdot = false;
+            }
         }
 
       path++;
       pathlen++;
     }
 
-  return pathlen >= PATH_MAX ? -ENAMETOOLONG : OK;
+  if (pathlen >= PATH_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  return is_simple ? 1 : OK;

Review Comment:
   ```suggestion
     return is_simple;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to