Skip to content

Commit

Permalink
UBI: don't use array index before testing if it is negative
Browse files Browse the repository at this point in the history
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@gmail.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
  • Loading branch information
Jesper Juhl authored and Artem Bityutskiy committed Oct 14, 2007
1 parent 8d2d401 commit 0169b49
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions drivers/mtd/ubi/kapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0169b49

Please sign in to comment.