From f838987e449e0bfecaac597d7990fc7dc6970df3 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 4 Feb 2024 06:22:20 +0800 Subject: [PATCH] fs/romfs: Call block_operations::close when romfs_bind fail to avoid the resource leak Signed-off-by: Xiang Xiao --- fs/romfs/fs_romfs.c | 25 ++++++++++++++++--------- fs/romfs/fs_romfsutil.c | 4 +--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/fs/romfs/fs_romfs.c b/fs/romfs/fs_romfs.c index 662d3d9550..463852b379 100644 --- a/fs/romfs/fs_romfs.c +++ b/fs/romfs/fs_romfs.c @@ -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 */ @@ -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 @@ -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 @@ -1146,9 +1146,16 @@ errout_with_buffer: 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; } @@ -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 { @@ -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); } diff --git a/fs/romfs/fs_romfsutil.c b/fs/romfs/fs_romfsutil.c index 5b73cfe124..16146c567d 100644 --- a/fs/romfs/fs_romfsutil.c +++ b/fs/romfs/fs_romfsutil.c @@ -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)