Skip to content

Commit

Permalink
fs/romfs: Call block_operations::close when romfs_bind fail
Browse files Browse the repository at this point in the history
to avoid the resource leak

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
  • Loading branch information
xiaoxiang781216 committed Oct 5, 2024
1 parent 0b0f535 commit 922f577
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 16 additions & 9 deletions fs/romfs/fs_romfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,12 +1090,11 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
return -ENODEV;
}

if (INODE_IS_BLOCK(blkdriver) &&
blkdriver->u.i_bops->open != NULL &&
blkdriver->u.i_bops->open(blkdriver) != OK)
if (blkdriver->u.i_bops->open != NULL &&
(ret = blkdriver->u.i_bops->open(blkdriver)) != OK)
{
ferr("ERROR: No open method\n");
return -ENODEV;
return ret;
}

/* Create an instance of the mountpt state structure */
Expand All @@ -1104,7 +1103,8 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
if (!rm)
{
ferr("ERROR: Failed to allocate mountpoint structure\n");
return -ENOMEM;
ret = -ENOMEM;
goto errout;
}

/* Initialize the allocated mountpt state structure. The filesystem is
Expand All @@ -1121,7 +1121,7 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
if (ret < 0)
{
ferr("ERROR: romfs_hwconfigure failed: %d\n", ret);
goto errout;
goto errout_with_mount;
}

/* Then complete the mount by getting the ROMFS configuratrion from
Expand All @@ -1146,9 +1146,16 @@ static int romfs_bind(FAR struct inode *blkdriver, FAR const void *data,
fs_heap_free(rm->rm_buffer);
}

errout:
errout_with_mount:
nxrmutex_destroy(&rm->rm_lock);
fs_heap_free(rm);

errout:
if (blkdriver->u.i_bops->close != NULL)
{
blkdriver->u.i_bops->close(blkdriver);
}

return ret;
}

Expand Down Expand Up @@ -1193,7 +1200,7 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
* no open file references.
*/

ret = (flags != 0) ? -ENOSYS : -EBUSY;
ret = flags ? -ENOSYS : -EBUSY;
}
else
{
Expand Down Expand Up @@ -1224,7 +1231,7 @@ static int romfs_unbind(FAR void *handle, FAR struct inode **blkdriver,

/* Release the mountpoint private data */

if (!rm->rm_xipbase && rm->rm_buffer)
if (!rm->rm_xipbase)
{
fs_heap_free(rm->rm_buffer);
}
Expand Down
4 changes: 1 addition & 3 deletions fs/romfs/fs_romfsutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,9 +536,7 @@ int romfs_hwread(FAR struct romfs_mountpt_s *rm, FAR uint8_t *buffer,
/* In non-XIP mode, we have to read the data from the device */

FAR struct inode *inode = rm->rm_blkdriver;
ssize_t nsectorsread = -ENODEV;

nsectorsread =
ssize_t nsectorsread =
inode->u.i_bops->read(inode, buffer, sector, nsectors);

if (nsectorsread == (ssize_t)nsectors)
Expand Down

0 comments on commit 922f577

Please sign in to comment.