Skip to contentSkip to conversation

ds3000: off by one in ds3000_read_snr()

PATCH In review Dan Carpenter 2 participants 6 messages Jan 17, 2012 – Jan 21, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 ds3000: off by one in ds3000_read_snr()

This is a static checker patch and I don't have the hardware to test this, so please review it carefully. The dvbs2_snr_tab[] array has 80 elements so when we cap it at 80, that's off by one. I would have assumed that the test was wrong but in the lines right before we have the same test but use "snr_reading - 1" as the array offset. I've done the same thing here.

drivers/media/dvb/frontends/ds3000.c +1 -1
index af65d01..3f5ae0a 100644
@@ -681,7 +681,7 @@ static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr)
 			snr_reading = dvbs2_noise_reading / tmp;
 			if (snr_reading > 80)
 				snr_reading = 80;
-			*snr = -(dvbs2_snr_tab[snr_reading] / 1000);
+			*snr = -(dvbs2_snr_tab[snr_reading - 1] / 1000);
 		}
 		dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
 				snr_reading, *snr);
#1 lore raw

Am 17.01.2012 08:30, schrieb Dan Carpenter:

Quoted 14 lines

This is a static checker patch and I don't have the hardware to test this, so please review it carefully. The dvbs2_snr_tab[] array has 80 elements so when we cap it at 80, that's off by one. I would have assumed that the test was wrong but in the lines right before we have the same test but use "snr_reading - 1" as the array offset. I've done the same thing here.

drivers/media/dvb/frontends/ds3000.c +0 -0
index af65d01..3f5ae0a 100644

drivers/media/dvb/frontends/ds3000.c static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr) · line 687

Quoted 8 lines
drivers/media/dvb/frontends/ds3000.c +1 -1
@@ -681,7 +681,7 @@ static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr)
 			snr_reading = dvbs2_noise_reading / tmp;
 			if (snr_reading > 80)
 				snr_reading = 80;
-			*snr = -(dvbs2_snr_tab[snr_reading] / 1000);
+			*snr = -(dvbs2_snr_tab[snr_reading - 1] / 1000);
 		}
 		dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
 				snr_reading, *snr);

hi dan,

perhaps it is more useful to do it in the check above ? thinking about that why not replace the number (80) with ARRAY_SIZE() ?

re,
wh

#2 lore raw
Quoted 28 lines · walter harms

Am 17.01.2012 08:30, schrieb Dan Carpenter:

Quoted 14 lines

This is a static checker patch and I don't have the hardware to test this, so please review it carefully. The dvbs2_snr_tab[] array has 80 elements so when we cap it at 80, that's off by one. I would have assumed that the test was wrong but in the lines right before we have the same test but use "snr_reading - 1" as the array offset. I've done the same thing here.

drivers/media/dvb/frontends/ds3000.c +0 -0
index af65d01..3f5ae0a 100644

drivers/media/dvb/frontends/ds3000.c static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr) · line 687

Quoted 8 lines
drivers/media/dvb/frontends/ds3000.c +1 -1
@@ -681,7 +681,7 @@ static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr)
 			snr_reading = dvbs2_noise_reading / tmp;
 			if (snr_reading > 80)
 				snr_reading = 80;
-			*snr = -(dvbs2_snr_tab[snr_reading] / 1000);
+			*snr = -(dvbs2_snr_tab[snr_reading - 1] / 1000);
 		}
 		dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
 				snr_reading, *snr);

hi dan,

Quoted 1 line

perhaps it is more useful to do it in the check above ?

It looks like the check is correct but we need to shift all the values by one. Again, I don't have this hardware, I'm just going by the context.

Quoted 1 line

thinking about that why not replace the number (80) with ARRAY_SIZE() ?

That would be a cleanup, yes but it could go in a separate patch.

regards,
dan carpenter

#3 lore raw

Am 19.01.2012 10:33, schrieb Dan Carpenter:

Quoted 29 lines · walter harms
Quoted 28 lines

Am 17.01.2012 08:30, schrieb Dan Carpenter:

Quoted 14 lines

This is a static checker patch and I don't have the hardware to test this, so please review it carefully. The dvbs2_snr_tab[] array has 80 elements so when we cap it at 80, that's off by one. I would have assumed that the test was wrong but in the lines right before we have the same test but use "snr_reading - 1" as the array offset. I've done the same thing here.

drivers/media/dvb/frontends/ds3000.c +0 -0
index af65d01..3f5ae0a 100644

drivers/media/dvb/frontends/ds3000.c static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr) · line 687

Quoted 8 lines
drivers/media/dvb/frontends/ds3000.c +1 -1
@@ -681,7 +681,7 @@ static int ds3000_read_snr(struct dvb_frontend *fe, u16 *snr)
 			snr_reading = dvbs2_noise_reading / tmp;
 			if (snr_reading > 80)
 				snr_reading = 80;
-			*snr = -(dvbs2_snr_tab[snr_reading] / 1000);
+			*snr = -(dvbs2_snr_tab[snr_reading - 1] / 1000);
 		}
 		dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
 				snr_reading, *snr);

hi dan,

Quoted 6 lines
Quoted 1 line

perhaps it is more useful to do it in the check above ?

It looks like the check is correct but we need to shift all the values by one. Again, I don't have this hardware, I'm just going by the context.

I do not have the hardware either so this is pure theoretical.

Access to the data field depends on the value of dvbs2_noise_reading/tmp even when the data are reasonable like 50/100 snr_reading would become 0 and the index suddenly is -1.

just my 2 cents.

re,
wh

Quoted 7 lines
Quoted 1 line

thinking about that why not replace the number (80) with ARRAY_SIZE() ?

That would be a cleanup, yes but it could go in a separate patch.

regards,
dan carpenter

#4 lore raw
Quoted 6 lines · walter harms
Quoted 6 lines
Quoted 1 line

perhaps it is more useful to do it in the check above ?

It looks like the check is correct but we need to shift all the values by one. Again, I don't have this hardware, I'm just going by the context.

Quoted 6 lines

I do not have the hardware either so this is pure theoretical.

Access to the data field depends on the value of dvbs2_noise_reading/tmp even when the data are reasonable like 50/100 snr_reading would become 0 and the index suddenly is -1.

It's a good point. I will redo the patch.

regards,
dan carpenter

#5 lore raw
Quoted 1 line · Dan Carpenter

It's a good point. I will redo the patch.

Sorry, I've decided to bail on this. The original code is buggy but I don't know what's going on well enough to fix it with any confidence.

regards,
dan carpenter