Skip to contentSkip to conversation

iio: adc: mt6359: fix unchecked return value in mt6358_read_imp

PATCH In review Salah Triki 3 participants 4 messages Apr 27

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

PATCH 1/1 iio: adc: mt6359: fix unchecked return value in mt6358_read_imp

In mt6358_read_imp(), the return value of regmap_read() is currently ignored. This is problematic because if the bus read fails the variable val_v remains uninitialized.

The function subsequently assigns this uninitialized stack value to *vbat, leading to incorrect measurement results being reported to the IIO subsystem without any error indication.

Update the function to check the return value of regmap_read(). Ensure that mt6358_stop_imp_conv() is still called to clean up the hardware state before returning the error code.

---
 drivers/iio/adc/mt6359-auxadc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/adc/mt6359-auxadc.c +4 -1
index 6b9ed9b1fde2..f927bff4a26a 100644
@@ -497,10 +497,13 @@ static int mt6358_read_imp(struct mt6359_auxadc *adc_dev,
 		return ret;
 
 	/* Read the params before stopping */
-	regmap_read(regmap, reg_adc0 + (cinfo->imp_adc_num << 1), &val_v);
+	ret = regmap_read(regmap, reg_adc0 + (cinfo->imp_adc_num << 1), &val_v);
 
 	mt6358_stop_imp_conv(adc_dev);
 
+	if (ret)
+		return ret;
+
 	if (vbat)
 		*vbat = val_v;
 	if (ibat)
Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Salah Triki

In mt6358_read_imp(), the return value of regmap_read() is currently ignored. This is problematic because if the bus read fails the variable val_v remains uninitialized.

The function subsequently assigns this uninitialized stack value to *vbat, leading to incorrect measurement results being reported to the IIO subsystem without any error indication.

Update the function to check the return value of regmap_read(). Ensure that mt6358_stop_imp_conv() is still called to clean up the hardware state before returning the error code.

Sounds like this deserves a Fixes tag, but the problem is that the whole driver is written like this. Why does having this fixed make it special?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Salah Triki

In mt6358_read_imp(), the return value of regmap_read() is currently ignored. This is problematic because if the bus read fails the variable val_v remains uninitialized.

The function subsequently assigns this uninitialized stack value to *vbat, leading to incorrect measurement results being reported to the IIO subsystem without any error indication.

Quoted 12 lines
Quoted 3 lines

Update the function to check the return value of regmap_read(). Ensure that mt6358_stop_imp_conv() is still called to clean up the hardware state before returning the error code.

Sounds like this deserves a Fixes tag, but the problem is that the whole driver is written like this. Why does having this fixed make it special?

Signature · 2 lines
With Best Regards,
Andy Shevchenko

You are right, Andy. I noticed that several other functions in this driver share the same issue. I will send a V2 that addresses all unchecked regmap_read() calls in this file and I will include the Fixes tag. Thanks for the feedback.

Best Regards
Salah Triki

maintainer
#3 lore raw
Quoted 23 lines · On Mon, 27 Apr 2026 12:31:34 +0100 Salah Triki <[email protected]> wrote:
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Salah Triki

In mt6358_read_imp(), the return value of regmap_read() is currently ignored. This is problematic because if the bus read fails the variable val_v remains uninitialized.

The function subsequently assigns this uninitialized stack value to *vbat, leading to incorrect measurement results being reported to the IIO subsystem without any error indication.

Quoted 12 lines
Quoted 3 lines

Update the function to check the return value of regmap_read(). Ensure that mt6358_stop_imp_conv() is still called to clean up the hardware state before returning the error code.

Sounds like this deserves a Fixes tag, but the problem is that the whole driver is written like this. Why does having this fixed make it special?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Quoted 4 lines

You are right, Andy. I noticed that several other functions in this driver share the same issue. I will send a V2 that addresses all unchecked regmap_read() calls in this file and I will include the Fixes tag. Thanks for the feedback.

What's the bus in this case?

Whilst maybe we should be checking them, it is not uncommon to forgo those checks in internal busses. In general they should be more reliable than something going over PCB tracks like spi or i2c.

I'd be tempted to fix the uninitialized variable by setting it at declaration rather than worry too much about checking all the regmap return values.

Quoted 3 lines

Best Regards
Salah Triki