Skip to contentSkip to conversation

iio: chemical: set val parameter only on success

PATCH In review [email protected] 2 participants 2 messages Dec 24, 2021 – Dec 28, 2021

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

PATCH 1/1 iio: chemical: set val parameter only on success

From: Tom Rix <[email protected]>

Clang static analysis reports this representative warning

sunrise_co2.c:410:9: warning: Assigned value is garbage or undefined

  *val = value;
       ^ ~~~~~

The ealier call to sunrise_read_word can fail without setting value. So defer setting val until we know the read was successful.

Fixes c397894e24f1 ("iio: chemical: Add Senseair Sunrise 006-0-007 driver")
---
 drivers/iio/chemical/sunrise_co2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/chemical/sunrise_co2.c +2 -2
index 233bd0f379c93..8440dc0c77cfe 100644
@@ -407,24 +407,24 @@ static int sunrise_read_raw(struct iio_dev *iio_dev,
 			mutex_lock(&sunrise->lock);
 			ret = sunrise_read_word(sunrise, SUNRISE_CO2_FILTERED_COMP_REG,
 						&value);
-			*val = value;
 			mutex_unlock(&sunrise->lock);
 
 			if (ret)
 				return ret;
 
+			*val = value;
 			return IIO_VAL_INT;
 
 		case IIO_TEMP:
 			mutex_lock(&sunrise->lock);
 			ret = sunrise_read_word(sunrise, SUNRISE_CHIP_TEMPERATURE_REG,
 						&value);
-			*val = value;
 			mutex_unlock(&sunrise->lock);
 
 			if (ret)
 				return ret;
 
+			*val = value;
 			return IIO_VAL_INT;
 
 		default:
Signature · 1 line
2.26.3
maintainer
#1 lore raw
Quoted 5 lines · On Fri, 24 Dec 2021 07:08:33 -0800 [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this representative warning

sunrise_co2.c:410:9: warning: Assigned value is garbage or undefined

Quoted 8 lines
  *val = value;
       ^ ~~~~~

The ealier call to sunrise_read_word can fail without setting value. So defer setting val until we know the read was successful.

Fixes c397894e24f1 ("iio: chemical: Add Senseair Sunrise 006-0-007 driver")

Hi Tom,

This won't have any actual affect as if the read_raw callback returns a negative error code we won't use the value. However, good to clean it up so applied to the togreg branch of iio.git.

Thanks,

Jonathan

Quoted 35 lines
---
 drivers/iio/chemical/sunrise_co2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/chemical/sunrise_co2.c +2 -2
index 233bd0f379c93..8440dc0c77cfe 100644
@@ -407,24 +407,24 @@ static int sunrise_read_raw(struct iio_dev *iio_dev,
 			mutex_lock(&sunrise->lock);
 			ret = sunrise_read_word(sunrise, SUNRISE_CO2_FILTERED_COMP_REG,
 						&value);
-			*val = value;
 			mutex_unlock(&sunrise->lock);
 
 			if (ret)
 				return ret;
 
+			*val = value;
 			return IIO_VAL_INT;
 
 		case IIO_TEMP:
 			mutex_lock(&sunrise->lock);
 			ret = sunrise_read_word(sunrise, SUNRISE_CHIP_TEMPERATURE_REG,
 						&value);
-			*val = value;
 			mutex_unlock(&sunrise->lock);
 
 			if (ret)
 				return ret;
 
+			*val = value;
 			return IIO_VAL_INT;
 
 		default: