Skip to content

Commit 0169b49

Browse files
Jesper JuhlArtem Bityutskiy
authored andcommitted
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@gmail.com> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
1 parent 8d2d401 commit 0169b49

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎drivers/mtd/ubi/kapi.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,21 @@ struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode)
9999
{
100100
int err;
101101
struct ubi_volume_desc *desc;
102-
struct ubi_device *ubi = ubi_devices[ubi_num];
102+
struct ubi_device *ubi;
103103
struct ubi_volume *vol;
104104

105105
dbg_msg("open device %d volume %d, mode %d", ubi_num, vol_id, mode);
106106

107107
err = -ENODEV;
108+
if (ubi_num < 0)
109+
return ERR_PTR(err);
110+
111+
ubi = ubi_devices[ubi_num];
112+
108113
if (!try_module_get(THIS_MODULE))
109114
return ERR_PTR(err);
110115

111-
if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || !ubi)
116+
if (ubi_num >= UBI_MAX_DEVICES || !ubi)
112117
goto out_put;
113118

114119
err = -EINVAL;

0 commit comments

Comments
 (0)