From 105f305b1b66595d369888ae1eb782cbbd3d5476 Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Tue, 18 May 2021 22:39:41 +0300 Subject: [PATCH] fs/fat/fat32util.c: Fix calculation of current sector with invalid cluster If the current cluster is invalid, don't use the error code for calculating the sector number. Instead just return the error code. Signed-off-by: Jukka Laitinen --- fs/fat/fs_fat32util.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/fs/fat/fs_fat32util.c b/fs/fat/fs_fat32util.c index c69cc9d377..8bd967ce46 100644 --- a/fs/fat/fs_fat32util.c +++ b/fs/fat/fs_fat32util.c @@ -2142,6 +2142,7 @@ int fat_currentsector(struct fat_mountpt_s *fs, struct fat_file_s *ff, off_t position) { int sectoroffset; + off_t cluster_start_sector; if (position <= ff->ff_size) { @@ -2149,12 +2150,18 @@ int fat_currentsector(struct fat_mountpt_s *fs, struct fat_file_s *ff, sectoroffset = SEC_NSECTORS(fs, position) & CLUS_NDXMASK(fs); - /* The current cluster is the first sector of the cluster plus + /* The current sector is the first sector of the cluster plus * the sector offset */ - ff->ff_currentsector = fat_cluster2sector(fs, ff->ff_currentcluster) - + sectoroffset; + cluster_start_sector = fat_cluster2sector(fs, ff->ff_currentcluster); + + if (cluster_start_sector < 0) + { + return cluster_start_sector; + } + + ff->ff_currentsector = cluster_start_sector + sectoroffset; /* The remainder is the number of sectors left in the cluster to be * read/written