iio: adc: mt6359: fix unchecked return value in mt6358_read_imp
Subsystems (2)
PATCH v3 1/1 iio: adc: mt6359: fix unchecked return value in mt6358_read_imp
In mt6358_read_imp(), the variable val_v is passed to regmap_read() but the return value is not checked. If the read fails, val_v remains uninitialized and its random stack content is subsequently reported as a measurement result.
Initialize val_v to zero to ensure a predictable value is reported in case of bus failure and to prevent potential stack data leakage. This also satisfies static analyzers that might otherwise flag the variable as used uninitialized.
---
Changes in v3:
- Initialize val_v to zero at declaration instead of checking regmap_read() return value, as suggested by Jonathan Cameron and Andy Shevchenko.
- Update commit message to reflect this new approach
Changes in v2:
- Added Fixes tag.
- Re-examined the entire driver for unchecked regmap operations. While several regmap_write() and regmap_set_bits() calls also ignore return values, I focused on this specific regmap_read() in mt6358_read_imp() because it leads to an uninitialized variable usage (val_v). This makes this fix critical for reporting correct data to userspace
drivers/iio/adc/mt6359-auxadc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/mt6359-auxadc.c +1 -1
@@ -489,7 +489,7 @@ static int mt6358_read_imp(struct mt6359_auxadc *adc_dev,const struct mtk_pmic_auxadc_info *cinfo = adc_dev->chip_info;struct regmap *regmap = adc_dev->regmap;u16 reg_adc0 = cinfo->regs[PMIC_AUXADC_ADC0 ]; -u32 val_v; +u32 val_v =0 ;int ret; ret =mt6358_start_imp_conv (adc_dev, chan);
Signature · 1 line
2.43.0
Quoted 9 lines · Salah Triki
In
mt6358_read_imp(), the variableval_vis passed toregmap_read()but the return value is not checked. If the read fails,val_vremains uninitialized and its random stack content is subsequently reported as a measurement result.Initialize
val_vto zero to ensure a predictable value is reported in case of bus failure and to prevent potential stack data leakage. This also satisfies static analyzers that might otherwise flag the variable as used uninitialized.
...
drivers/iio/adc/mt6359-auxadc.c static int mt6358_read_imp(struct mt6359_auxadc *adc_dev, · line 492
Quoted 5 lines
drivers/iio/adc/mt6359-auxadc.c +1
-1const struct mtk_pmic_auxadc_info *cinfo = adc_dev->chip_info;struct regmap *regmap = adc_dev->regmap;u16 reg_adc0 = cinfo->regs[PMIC_AUXADC_ADC0 ]; -u32 val_v; +u32 val_v =0 ;
This is hard to maintain. Better to assign it just immediately before the first use.
Quoted 1 line
int ret;
Otherwise, LGTM.
Signature · 2 lines
With Best Regards, Andy Shevchenko