[PATCH 1/1] sandbox: fix return type of os_filesize()

Heinrich Schuchardt heinrich.schuchardt at canonical.com
Wed Apr 5 10:59:41 CEST 2023


Given a file ../img of size 4294967296 with GPT partition table and
partitions:

=> host bind 0 ../img
=> part list host 0
Disk host-0.blk not ready

The cause is os_filesize() returning int. 4294967296 converted to int32_t
gives 0. File sizes must use off_t.

Correct all uses of os_filesize() too.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
---
 arch/sandbox/cpu/os.c    | 8 ++++++--
 drivers/block/host_dev.c | 3 ++-
 include/os.h             | 2 +-
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 5e66304e2b..f8ad208e46 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -166,7 +166,7 @@ int os_write_file(const char *fname, const void *buf, int size)
 	return 0;
 }
 
-int os_filesize(int fd)
+off_t os_filesize(int fd)
 {
 	off_t size;
 
@@ -218,7 +218,7 @@ err:
 int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep)
 {
 	void *ptr;
-	int size;
+	off_t size;
 	int ifd;
 
 	ifd = os_open(pathname, os_flags);
@@ -231,6 +231,10 @@ int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep)
 		printf("Cannot get file size of '%s'\n", pathname);
 		return -EIO;
 	}
+	if (size > SIZE_MAX) {
+		printf("File '%s' too large to map\n", pathname);
+		return -EIO;
+	}
 
 	ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
 	if (ptr == MAP_FAILED) {
diff --git a/drivers/block/host_dev.c b/drivers/block/host_dev.c
index 5885fc358a..64422417b7 100644
--- a/drivers/block/host_dev.c
+++ b/drivers/block/host_dev.c
@@ -24,7 +24,8 @@ static int host_sb_attach_file(struct udevice *dev, const char *filename)
 	struct host_sb_plat *plat = dev_get_plat(dev);
 	struct blk_desc *desc;
 	struct udevice *blk;
-	int ret, fd, size;
+	int ret, fd;
+	off_t size;
 	char *fname;
 
 	if (!filename)
diff --git a/include/os.h b/include/os.h
index 0415f0f0e7..968412b0a8 100644
--- a/include/os.h
+++ b/include/os.h
@@ -64,7 +64,7 @@ off_t os_lseek(int fd, off_t offset, int whence);
  * @fd:		File descriptor as returned by os_open()
  * Return:	file size or negative error code
  */
-int os_filesize(int fd);
+off_t os_filesize(int fd);
 
 /**
  * Access to the OS open() system call
-- 
2.39.2



More information about the U-Boot mailing list