[PATCH RESEND] mtdpart: Avoid divide-by-zero on out-of-reach path

Atsushi Nemoto anemo at mba.ocn.ne.jp
Sat Jun 14 10:45:40 EDT 2008


On "partition is out of reach" path, i.e. slave's offset was bigger
than the master's size, slave's erasesize will not be calculated and
it leads division by zero on following boundary checking.  This patch
makes calculation of the slave's erasesize more robust.

This patch also contains some cleanups.  Do not shadow symbol 'i',
fold long lines, etc.

Signed-off-by: Atsushi Nemoto <anemo at mba.ocn.ne.jp>
---
 drivers/mtd/mtdpart.c |   25 +++++++++++++++----------
 1 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index 07c7011..ff9ea3a 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -445,17 +445,22 @@ int add_mtd_partitions(struct mtd_info *master,
 		}
 		if (master->numeraseregions>1) {
 			/* Deal with variable erase size stuff */
-			int i;
-			struct mtd_erase_region_info *regions = master->eraseregions;
-
-			/* Find the first erase regions which is part of this partition. */
-			for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
+			int j, num = master->numeraseregions;
+			struct mtd_erase_region_info *r = master->eraseregions;
+			/*
+			 * Find the first erase region which is part
+			 * of this partition.
+			 */
+			for (j = 0; j < num && slave->offset >= r[j].offset;
+				j++)
 				;
-
-			for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
-				if (slave->mtd.erasesize < regions[i].erasesize) {
-					slave->mtd.erasesize = regions[i].erasesize;
-				}
+			j--;
+			slave->mtd.erasesize = r[j].erasesize;
+			for (; j < num &&
+				slave->offset + slave->mtd.size > r[j].offset;
+				j++) {
+				if (slave->mtd.erasesize < r[j].erasesize)
+					slave->mtd.erasesize = r[j].erasesize;
 			}
 		} else {
 			/* Single erase size */



More information about the linux-mtd mailing list