Skip to contentSkip to conversation

udf: in scan_anchors() sector_t last[] is unsigned and cannot be negative.

PATCH In review Roel Kluin 2 participants 2 messages Dec 2, 2008 – Dec 3, 2008 UDF filesystem

lore
Share & tools
lore

Roel Kluin

author
#0 lore raw

PATCH 1/1 udf: in scan_anchors() sector_t last[] is unsigned and cannot be negative.

sector_t last[] is unsigned and cannot be negative.

---
Maybe you can suggest a less ugly solution?

fs/udf/super.c +1 -1
index e25e701..d53c6fc 100644
@@ -721,7 +721,7 @@ static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock)
 	 *  however, if the disc isn't closed, it could be 512 */
 
 	for (i = 0; i < ARRAY_SIZE(last); i++) {
-		if (last[i] < 0)
+		if (last[i] > last[0] + 1)
 			continue;
 		if (last[i] >= sb->s_bdev->bd_inode->i_size >>
 				sb->s_blocksize_bits)
#1 lore raw

Hello,

Quoted 3 lines · Roel Kluin

sector_t last[] is unsigned and cannot be negative.

Hmm, I think it would be a violation of the UDF standard if a we got a
medium where these numbers would underflow and most of the cases would be caught by the following test. But let's make the code clean. I've written the following patch (which I think makes the underflow checks much more obvious):

Quoted 1 line

From a1ea42b47466b642c65cc6c3ca1a5a5fc18498ba Mon Sep 17 00:00:00 2001

From: Jan Kara <[email protected]>
Date: Wed, 3 Dec 2008 17:31:39 +0100
Subject: [PATCH] udf: Add checks to not underflow sector_t

---
 fs/udf/super.c |   21 +++++++++++++--------
 1 files changed, 13 insertions(+), 8 deletions(-)
fs/udf/super.c +13 -8
index b9dc6ad..3c2d35d 100644
@@ -731,13 +731,18 @@ static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock)
 	sector_t last[6];
 	int i;
 	struct udf_sb_info *sbi = UDF_SB(sb);
-
-	last[0] = lastblock;
-	last[1] = last[0] - 1;
-	last[2] = last[0] + 1;
-	last[3] = last[0] - 2;
-	last[4] = last[0] - 150;
-	last[5] = last[0] - 152;
+	int last_count = 0;
+
+	last[last_count++] = lastblock;
+	if (lastblock >= 1)
+		last[last_count++] = lastblock - 1;
+	last[last_count++] = lastblock + 1;
+	if (lastblock >= 2)
+		last[last_count++] = lastblock - 2;
+	if (lastblock >= 150)
+		last[last_count++] = lastblock - 150;
+	if (lastblock >= 152)
+		last[last_count++] = lastblock - 152;
 
 	/*  according to spec, anchor is in either:
 	 *     block 256
@@ -745,7 +750,7 @@ static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock)
 	 *     lastblock
 	 *  however, if the disc isn't closed, it could be 512 */
 
-	for (i = 0; i < ARRAY_SIZE(last); i++) {
+	for (i = 0; i < last_count; i++) {
 		if (last[i] >= sb->s_bdev->bd_inode->i_size >>
 				sb->s_blocksize_bits)
 			continue;
Signature · 1 line
1.6.0.4