UBI: don't use array index before testing if it is negative

Linux-MTD Mailing List linux-mtd at lists.infradead.org
Sun Oct 14 09:59:01 EDT 2007


Gitweb:     http://git.infradead.org/?p=mtd-2.6.git;a=commit;h=0169b49d52400a6035cd0f2ccd08bcba061a1a9b
Commit:     0169b49d52400a6035cd0f2ccd08bcba061a1a9b
Parent:     8d2d4011f1398d984819c65043abb559c451a3c8
Author:     Jesper Juhl <jesper.juhl at gmail.com>
AuthorDate: Sat Aug 4 01:25:26 2007 +0200
Committer:  Artem Bityutskiy <Artem.Bityutskiy at nokia.com>
CommitDate: Sun Oct 14 13:10:20 2007 +0300

    UBI: don't use array index before testing if it is negative
    
    I can't find anything guaranteeing that 'ubi_num' cannot be <0 in
    drivers/mtd/ubi/kapi.c::ubi_open_volume(), and in fact the code
    even tests for that and errors out if so. Unfortunately the test
    for "ubi_num < 0" happens after we've already used 'ubi_num' as
    an array index - bad thing to do if it is negative.
    This patch moves the test earlier in the function and then moves
    the indexing using that variable after the check. A bit safer :-)
    
    Signed-off-by: Jesper Juhl <jesper.juhl at gmail.com>
    Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy at nokia.com>
---
 drivers/mtd/ubi/kapi.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/ubi/kapi.c b/drivers/mtd/ubi/kapi.c
index 4a458e8..03c774f 100644
--- a/drivers/mtd/ubi/kapi.c
+++ b/drivers/mtd/ubi/kapi.c
@@ -99,16 +99,21 @@ struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
 {
 	int err;
 	struct ubi_volume_desc *desc;
-	struct ubi_device *ubi = ubi_devices[ubi_num];
+	struct ubi_device *ubi;
 	struct ubi_volume *vol;
 
 	dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
 
 	err = -ENODEV;
+	if (ubi_num < 0)
+		return ERR_PTR(err);
+
+	ubi = ubi_devices[ubi_num];
+
 	if (!try_module_get(THIS_MODULE))
 		return ERR_PTR(err);
 
-	if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi)
+	if (ubi_num >= UBI_MAX_DEVICES || !ubi)
 		goto out_put;
 
 	err = -EINVAL;



More information about the linux-mtd-cvs mailing list