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


##########
fs/inode/fs_files.c:
##########
@@ -51,59 +51,118 @@
  * Private Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: files_fget_index
+ ****************************************************************************/
+
+static FAR struct file *files_fget_index(FAR struct filelist *list,
+                                         int l1, int l2)
+{
+  FAR struct file *filep;
+  irqstate_t flags;
+
+  flags = enter_critical_section();
+
+  filep = &list->fl_files[l1][l2];
+
+  leave_critical_section(flags);
+
+  return filep;
+}
+
+/****************************************************************************
+ * Name: files_fget
+ ****************************************************************************/
+
+static FAR struct file *files_fget(FAR struct filelist *list, int fd)
+{
+  return files_fget_index(list, fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK,
+                          fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK);
+}
+
 /****************************************************************************
  * Name: files_extend
  ****************************************************************************/
 
 static int files_extend(FAR struct filelist *list, size_t row)
 {
-  FAR struct file **tmp;
+  FAR struct file **files;
+  uint8_t orig_rows;
+  FAR void *tmp;
+  int flags;
   int i;
+  int j;
 
   if (row <= list->fl_rows)
     {
       return 0;
     }
 
-  if (row * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK > OPEN_MAX)
+  if (files_get_filecount(list) > OPEN_MAX)
     {
       return -EMFILE;
     }
 
-  tmp = kmm_realloc(list->fl_files, sizeof(FAR struct file *) * row);
-  DEBUGASSERT(tmp);
-  if (tmp == NULL)
+  orig_rows = list->fl_rows;
+
+  files = kmm_malloc(sizeof(FAR struct file *) * row);
+  DEBUGASSERT(files);
+  if (files == NULL)
     {
       return -ENFILE;
     }
 
   i = list->fl_rows;
   do
     {
-      tmp[i] = kmm_zalloc(sizeof(struct file) *
-                          CONFIG_NFILE_DESCRIPTORS_PER_BLOCK);
-      if (tmp[i] == NULL)
+      files[i] = kmm_zalloc(sizeof(struct file) *
+                            CONFIG_NFILE_DESCRIPTORS_PER_BLOCK);
+      if (files[i] == NULL)
         {
           while (--i >= list->fl_rows)
             {
-              kmm_free(tmp[i]);
+              kmm_free(files[i]);
             }
 
-          kmm_free(tmp);
+          kmm_free(files);
           return -ENFILE;
         }
     }
   while (++i < row);
 
-  list->fl_files = tmp;
-  list->fl_rows = row;
+  flags = enter_critical_section();
 
-  /* Note: If assertion occurs, the fl_rows has a overflow.
-   * And there may be file descriptors leak in system.
+  /* To avoid race condition, if the file list is updated by other threads
+   * and list rows is greater or equal than temp list,
+   * release the obsolete buffers
    */
 
-  DEBUGASSERT(list->fl_rows == row);
-  return 0;
+  if (orig_rows != list->fl_rows && list->fl_rows >= row)

Review Comment:
   ```suggestion
     if (orig_rows != list->fl_rows)
   ```
   and change list->fl_rows >= row to DEBUGASSERT in if block.



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