Skip to contentSkip to conversation

iio: scd4x: check return of scd4x_write_and_fetch

PATCH Superseded [email protected] 2 participants 3 messages Feb 27, 2022 – Feb 28, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: scd4x: check return of scd4x_write_and_fetch

From: Tom Rix <[email protected]>

Clang static analysis reports this problem scd4x.c:474:10: warning: The left operand of '==' is a
garbage value

  if (val == 0xff) {
      ~~~ ^

val is only set from a successful call to scd4x_write_and_fetch() So check it's return.

Fixes 49d22b695cbb ("drivers: iio: chemical: Add support for Sensirion SCD4x CO2 sensor")
---
 drivers/iio/chemical/scd4x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/chemical/scd4x.c +1 -1
index 20d4e7584e923..b978330fb761c 100644
@@ -471,7 +471,7 @@ static ssize_t calibration_forced_value_store(struct device *dev,
 	ret = scd4x_write_and_fetch(state, CMD_FRC, arg, &val, sizeof(val));
 	mutex_unlock(&state->lock);
 
-	if (val == 0xff) {
+	if (!ret && val == 0xff) {
 		dev_err(dev, "forced calibration has failed");
 		return -EINVAL;
 	}
Signature · 1 line
2.26.3
maintainer
#1 lore raw
Quoted 5 lines · On Sun, 27 Feb 2022 07:43:31 -0800 [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this problem scd4x.c:474:10: warning: The left operand of '==' is a
garbage value

Quoted 7 lines
  if (val == 0xff) {
      ~~~ ^

val is only set from a successful call to scd4x_write_and_fetch() So check it's return.

Fixes 49d22b695cbb ("drivers: iio: chemical: Add support for Sensirion SCD4x CO2 sensor")

Good find, but I'd prefer a separate check on ret inline with what the other error checking paths in that function are doing.

Quoted 9 lines
---
 drivers/iio/chemical/scd4x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/chemical/scd4x.c +0 -0
index 20d4e7584e923..b978330fb761c 100644

drivers/iio/chemical/scd4x.c static ssize_t calibration_forced_value_store(struct device *dev, · line 477

Quoted 8 lines
drivers/iio/chemical/scd4x.c +1 -1
@@ -471,7 +471,7 @@ static ssize_t calibration_forced_value_store(struct device *dev,
 	ret = scd4x_write_and_fetch(state, CMD_FRC, arg, &val, sizeof(val));
 	mutex_unlock(&state->lock);
 
-	if (val == 0xff) {
+	if (!ret && val == 0xff) {
 		dev_err(dev, "forced calibration has failed");
 		return -EINVAL;
 	}

Prefer

	if (ret)
		return ret;

	if (val == 0xff) {
		dev_err(dev, "...
		return -EINVAL;
	}

Thanks,

Jonathan

author
#2 lore raw
Quoted 37 lines · Jonathan Cameron
Quoted 5 lines · On Sun, 27 Feb 2022 07:43:31 -0800 [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this problem scd4x.c:474:10: warning: The left operand of '==' is a
garbage value

Quoted 7 lines
   if (val == 0xff) {
       ~~~ ^

val is only set from a successful call to scd4x_write_and_fetch() So check it's return.

Fixes 49d22b695cbb ("drivers: iio: chemical: Add support for Sensirion SCD4x CO2 sensor")

Good find, but I'd prefer a separate check on ret inline with what the other error checking paths in that function are doing.

Quoted 9 lines
---
  drivers/iio/chemical/scd4x.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/chemical/scd4x.c +0 -0
index 20d4e7584e923..b978330fb761c 100644

drivers/iio/chemical/scd4x.c static ssize_t calibration_forced_value_store(struct device *dev, · line 477

Quoted 8 lines
drivers/iio/chemical/scd4x.c +1 -1
@@ -471,7 +471,7 @@ static ssize_t calibration_forced_value_store(struct device *dev,
  	ret = scd4x_write_and_fetch(state, CMD_FRC, arg, &val, sizeof(val));
  	mutex_unlock(&state->lock);
 
-	if (val == 0xff) {
+	if (!ret && val == 0xff) {
  		dev_err(dev, "forced calibration has failed");
  		return -EINVAL;
  	}

Prefer

Quoted 7 lines
	if (ret)
		return ret;

	if (val == 0xff) {
		dev_err(dev, "...
		return -EINVAL;
	}

ok and the next line can be simplified to

return len;

Tom

Quoted 4 lines

Thanks,

Jonathan