[PATCH] fs: fat: check for buffer size before reading blocks

Oleksandr Suvorov oleksandr.suvorov at foundries.io
Thu Sep 16 16:09:01 CEST 2021


From: Ricardo Salveti <ricardo at foundries.io>

This patch optimizes the commit mentioned below by avoiding running
a set of commands which useless in the case when
size < mydata->sect_size and idx would be 0.

Fixes: 5b3ddb17ba ("fs/fat/fat.c: Do not perform zero block reads if there are no blocks left")

Signed-off-by: Ricardo Salveti <ricardo at foundries.io>
Co-developed-by: Oleksandr Suvorov <oleksandr.suvorov at foundries.io>
Signed-off-by: Oleksandr Suvorov <oleksandr.suvorov at foundries.io>
---

 fs/fat/fat.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index 7021138b987..4a509755442 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -275,14 +275,10 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
 			buffer += mydata->sect_size;
 			size -= mydata->sect_size;
 		}
-	} else {
+	} else if (size >= mydata->sect_size) {
 		__u32 idx;
-
 		idx = size / mydata->sect_size;
-		if (idx == 0)
-			ret = 0;
-		else
-			ret = disk_read(startsect, idx, buffer);
+		ret = disk_read(startsect, idx, buffer);
 		if (ret != idx) {
 			debug("Error reading data (got %d)\n", ret);
 			return -1;
-- 
2.31.1



More information about the U-Boot mailing list