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-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 2bf215bf8 testing/testsuites: harden fs test path handling
2bf215bf8 is described below

commit 2bf215bf8a0de461d31f263ad2678b8e1b7e92cb
Author: bigcat2026 <[email protected]>
AuthorDate: Wed Nov 4 16:23:18 2026 +0800

    testing/testsuites: harden fs test path handling
    
    Port the path-robustness cleanup from open-vela/tests#15 onto the
    upstream test_fs_common.c helper: bound-check snprintf() when building
    recursive-delete and test-directory paths, and bail out cleanly instead
    of truncating silently.
    
    Signed-off-by: bigcat2026 <[email protected]>
---
 .../testsuites/kernel/fs/common/test_fs_common.c   | 38 ++++++++++++++++++----
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/testing/testsuites/kernel/fs/common/test_fs_common.c 
b/testing/testsuites/kernel/fs/common/test_fs_common.c
index 911cad7e3..d2fde2cf4 100644
--- a/testing/testsuites/kernel/fs/common/test_fs_common.c
+++ b/testing/testsuites/kernel/fs/common/test_fs_common.c
@@ -111,7 +111,13 @@ int cm_unlink_recursive(FAR char *path)
           continue;
         }
 
-      snprintf(&path[len], PATH_MAX - len, "/%s", d->d_name);
+      if (snprintf(&path[len], PATH_MAX - len, "/%s",
+                   d->d_name) >= PATH_MAX - len)
+        {
+          closedir(dp);
+          return -1;
+        }
+
       ret = cm_unlink_recursive(path);
       if (ret < 0)
         {
@@ -163,7 +169,12 @@ int test_nuttx_fs_test_group_setup(void **state)
         0
       };
 
-      sprintf(testdir, "%s/%s", MOUNT_DIR, CM_FSTESTDIR);
+      if (snprintf(testdir, sizeof(testdir), "%s/%s", MOUNT_DIR,
+                   CM_FSTESTDIR) >= sizeof(testdir))
+        {
+          syslog(LOG_INFO, "ERROR: test directory path too long\n");
+          exit(1);
+        }
 
       /* Delete the existing test directory */
 
@@ -206,14 +217,29 @@ int test_nuttx_fs_test_group_teardown(void **state)
   test_state = (struct fs_testsuites_state_s *)*state;
 
   if (test_state->fd1 > 0)
-    close(test_state->fd1);
+    {
+      close(test_state->fd1);
+    }
+
   if (test_state->fd2 > 0)
-    close(test_state->fd2);
+    {
+      close(test_state->fd2);
+    }
+
   if (test_state->ptr != NULL)
-    free(test_state->ptr);
+    {
+      free(test_state->ptr);
+    }
+
   free(test_state);
 
-  sprintf(testdir, "%s/%s", MOUNT_DIR, CM_FSTESTDIR);
+  if (snprintf(testdir, sizeof(testdir), "%s/%s", MOUNT_DIR,
+               CM_FSTESTDIR) >= sizeof(testdir))
+    {
+      syslog(LOG_INFO, "ERROR: test directory path too long\n");
+      return -1;
+    }
+
   res = chdir(MOUNT_DIR);
   if (res != 0)
     {

Reply via email to