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 <jukkax@ssrc.tii.ae>
This commit is contained in:
Jukka Laitinen 2021-05-18 22:39:41 +03:00 committed by Xiang Xiao
parent 65e9ff5a48
commit 105f305b1b
1 changed files with 10 additions and 3 deletions

View File

@ -2142,6 +2142,7 @@ int fat_currentsector(struct fat_mountpt_s *fs, struct fat_file_s *ff,
off_t position) off_t position)
{ {
int sectoroffset; int sectoroffset;
off_t cluster_start_sector;
if (position <= ff->ff_size) 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); 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 * the sector offset
*/ */
ff->ff_currentsector = fat_cluster2sector(fs, ff->ff_currentcluster) cluster_start_sector = fat_cluster2sector(fs, ff->ff_currentcluster);
+ sectoroffset;
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 /* The remainder is the number of sectors left in the cluster to be
* read/written * read/written