Copilot commented on code in PR #21684:
URL: https://github.com/apache/nuttx/pull/21684#discussion_r4535557643


##########
fs/romfs/fs_romfs.c:
##########
@@ -897,9 +902,30 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR 
const char *relpath,
 static int romfs_closedir(FAR struct inode *mountpt,
                           FAR struct fs_dirent_s *dir)
 {
-  DEBUGASSERT(dir);
+  FAR struct romfs_mountpt_s *rm;
+  int                         ret;
+
+  DEBUGASSERT(mountpt != NULL && dir != NULL);
+
+  /* Recover our private data from the inode instance */
+
+  rm = mountpt->i_private;
+  DEBUGASSERT(rm != NULL);
+
+  ret = nxrmutex_lock(&rm->rm_lock);
+  if (ret < 0)
+    {
+      goto errout_with_dir;
+    }
+
+  /* Give back the reference taken by romfs_opendir() */
+
+  rm->rm_refs--;
+  nxrmutex_unlock(&rm->rm_lock);
+
+errout_with_dir:
   fs_heap_free(dir);
-  return 0;
+  return ret;

Review Comment:
   If `nxrmutex_lock(&rm->rm_lock)` fails, this implementation still frees 
`dir` but does not decrement `rm->rm_refs`. That can permanently leak the mount 
reference (making future unmounts fail with EBUSY) and the caller cannot retry 
`closedir()` because the handle has been freed. Consider making the lock 
acquisition uninterruptible / retry-on-EINTR so the ref is always released, or 
if you must return an error, avoid freeing `dir` unless the reference was 
successfully dropped.



##########
fs/romfs/fs_romfs.c:
##########
@@ -897,9 +902,30 @@ static int romfs_opendir(FAR struct inode *mountpt, FAR 
const char *relpath,
 static int romfs_closedir(FAR struct inode *mountpt,
                           FAR struct fs_dirent_s *dir)
 {
-  DEBUGASSERT(dir);
+  FAR struct romfs_mountpt_s *rm;
+  int                         ret;
+
+  DEBUGASSERT(mountpt != NULL && dir != NULL);
+
+  /* Recover our private data from the inode instance */
+
+  rm = mountpt->i_private;
+  DEBUGASSERT(rm != NULL);
+
+  ret = nxrmutex_lock(&rm->rm_lock);
+  if (ret < 0)
+    {
+      goto errout_with_dir;
+    }
+
+  /* Give back the reference taken by romfs_opendir() */
+

Review Comment:
   `rm->rm_refs--` will underflow if `romfs_closedir()` is called without a 
matching successful `romfs_opendir()` increment (or if it is closed twice). 
Adding a defensive `DEBUGASSERT(rm->rm_refs > 0)` before decrementing would 
make misuse fail fast and avoid silently corrupting the reference count.



-- 
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