diff mbox series

sandbox: Close file after mmaping it

Message ID 20231104195733.1576679-1-seanga2@gmail.com
State Accepted
Commit f6d76e68784591ee32c173a08535c4dff4a76a1e
Delegated to: Simon Glass
Headers show
Series sandbox: Close file after mmaping it | expand

Commit Message

Sean Anderson Nov. 4, 2023, 7:57 p.m. UTC
After opening pathname, we must close ifd once we are done with it.

Fixes: b9274095c2c ("sandbox: Add a way to map a file into memory")
Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

 arch/sandbox/cpu/os.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

Comments

Simon Glass Nov. 4, 2023, 10:57 p.m. UTC | #1
On Sat, 4 Nov 2023 at 19:57, Sean Anderson <seanga2@gmail.com> wrote:
>
> After opening pathname, we must close ifd once we are done with it.
>
> Fixes: b9274095c2c ("sandbox: Add a way to map a file into memory")
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  arch/sandbox/cpu/os.c | 15 ++++++++++-----
>  1 file changed, 10 insertions(+), 5 deletions(-)

Reviewed-by: Simon Glass <sjg@chromium.org>
Simon Glass Nov. 15, 2023, 1:16 p.m. UTC | #2
On Sat, 4 Nov 2023 at 19:57, Sean Anderson <seanga2@gmail.com> wrote:
>
> After opening pathname, we must close ifd once we are done with it.
>
> Fixes: b9274095c2c ("sandbox: Add a way to map a file into memory")
> Signed-off-by: Sean Anderson <seanga2@gmail.com>
> ---
>
>  arch/sandbox/cpu/os.c | 15 ++++++++++-----
>  1 file changed, 10 insertions(+), 5 deletions(-)

Reviewed-by: Simon Glass <sjg@chromium.org>

Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 85d0d6a1703..95c26d855ab 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -219,7 +219,7 @@  int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep)
 {
 	void *ptr;
 	off_t size;
-	int ifd;
+	int ifd, ret = 0;
 
 	ifd = os_open(pathname, os_flags);
 	if (ifd < 0) {
@@ -229,23 +229,28 @@  int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep)
 	size = os_filesize(ifd);
 	if (size < 0) {
 		printf("Cannot get file size of '%s'\n", pathname);
-		return -EIO;
+		ret = -EIO;
+		goto out;
 	}
 	if ((unsigned long long)size > (unsigned long long)SIZE_MAX) {
 		printf("File '%s' too large to map\n", pathname);
-		return -EIO;
+		ret = -EIO;
+		goto out;
 	}
 
 	ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
 	if (ptr == MAP_FAILED) {
 		printf("Can't map file '%s': %s\n", pathname, strerror(errno));
-		return -EPERM;
+		ret = -EPERM;
+		goto out;
 	}
 
 	*bufp = ptr;
 	*sizep = size;
 
-	return 0;
+out:
+	os_close(ifd);
+	return ret;
 }
 
 int os_unmap(void *buf, int size)