Blob Blame History Raw
From 39ce90fe75661ed8842551cd44ea7fec278a60a1 Mon Sep 17 00:00:00 2001
From: Andreas Bombe <aeb@debian.org>
Date: Fri, 11 Sep 2015 19:34:10 +0200
Subject: [PATCH] set_fat(): Move FAT12 next cluster check up

In FAT12 two 12 bit entries are combined to a 24 bit value (three
bytes). Therefore, when an even numbered FAT entry is set in FAT12, it
must be be combined with the following entry. To prevent accessing
beyond the end of the FAT array, it must be checked that the cluster is
not the last one.

This check was broken in ff1b24e9 (first included in 3.0.3) as the
lookup was done unconditionally and the check influenced only using the
looked up value.

Move the check up to fix.

Signed-off-by: Andreas Bombe <aeb@debian.org>
---
 src/fat.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/fat.c b/src/fat.c
index 5a92f56..0c19184 100644
--- a/src/fat.c
+++ b/src/fat.c
@@ -205,10 +205,12 @@ void set_fat(DOS_FS * fs, uint32_t cluster, int32_t new)
 	    data[1] = new >> 4;
 	} else {
 	    FAT_ENTRY subseqEntry;
-	    get_fat(&subseqEntry, fs->fat, cluster + 1, fs);
+	    if (cluster != fs->clusters - 1)
+		get_fat(&subseqEntry, fs->fat, cluster + 1, fs);
+	    else
+		subseqEntry.value = 0;
 	    data[0] = new & 0xff;
-	    data[1] = (new >> 8) | (cluster == fs->clusters - 1 ? 0 :
-				    (0xff & subseqEntry.value) << 4);
+	    data[1] = (new >> 8) | ((0xff & subseqEntry.value) << 4);
 	}
 	size = 2;
 	break;
-- 
2.5.5