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

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

commit 1794fbaa615439f088a3ec446ac59f570a8d675f
Author: buxiasen <[email protected]>
AuthorDate: Wed Jul 16 12:15:53 2025 +0800

    fs/semaphore/open: fix the inode information update outside of lock
    
    For re-enter case, the inode information may unlock with not filled,
    and get by other user. that lead to the error behavior.
    Especially SMP scene.
    
    Signed-off-by: buxiasen <[email protected]>
---
 fs/semaphore/sem_open.c | 48 ++++++++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 22 deletions(-)

diff --git a/fs/semaphore/sem_open.c b/fs/semaphore/sem_open.c
index d346377831b..0fdac9dd2c9 100644
--- a/fs/semaphore/sem_open.c
+++ b/fs/semaphore/sem_open.c
@@ -176,19 +176,6 @@ int nxsem_open(FAR sem_t **sem, FAR const char *name, int 
oflags, ...)
           goto errout_with_search;
         }
 
-      /* Create an inode in the pseudo-filesystem at this path.  The new
-       * inode will be created with a reference count of zero.
-       */
-
-      inode_lock();
-      ret = inode_reserve(fullpath, mode, &inode);
-      inode_unlock();
-
-      if (ret < 0)
-        {
-          goto errout_with_search;
-        }
-
       /* Allocate the semaphore structure (using the appropriate allocator
        * for the group)
        */
@@ -197,22 +184,39 @@ int nxsem_open(FAR sem_t **sem, FAR const char *name, int 
oflags, ...)
       if (!nsem)
         {
           ret = -ENOMEM;
-          goto errout_with_inode;
+          goto errout_with_search;
         }
 
-      /* Link to the inode */
+      /* Create an inode in the pseudo-filesystem at this path.  The new
+       * inode will be created with a reference count of zero.
+       */
+
+      inode_lock();
+      ret = inode_reserve(fullpath, mode, &inode);
+      if (ret >= 0)
+        {
+          /* Link to the inode */
 
-      inode->u.i_nsem = nsem;
-      nsem->ns_inode  = inode;
+          inode->u.i_nsem = nsem;
+          nsem->ns_inode  = inode;
 
-      /* Initialize the inode */
+          /* Initialize the inode */
 
-      INODE_SET_NAMEDSEM(inode);
-      atomic_fetch_add(&inode->i_crefs, 1);
+          INODE_SET_NAMEDSEM(inode);
+          atomic_fetch_add(&inode->i_crefs, 1);
 
-      /* Initialize the semaphore */
+          /* Initialize the semaphore */
 
-      nxsem_init(&nsem->ns_sem, 0, value);
+          nxsem_init(&nsem->ns_sem, 0, value);
+        }
+
+      inode_unlock();
+
+      if (ret < 0)
+        {
+          group_free(NULL, nsem);
+          goto errout_with_search;
+        }
 
       /* Return a reference to the semaphore */
 

Reply via email to