Skip to contentSkip to conversation

staging: iio: meter: add check on return variables

PATCH Superseded Aya Mahfouz 2 participants 3 messages Mar 3, 2015

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 staging: iio: meter: add check on return variables

Adds checks on variables that are used to return values. If the value is less than zero, this indicates that an error occurred and hence a message is printed through dev_err. Checks are made on negative values only since spi_* functions return negative error codes.

The functions were found using the following script but the aforementioned modification was what was carried out in the end:
@@
identifier len,f;
@@

-int len;
... when != len
when strict
-len =
+return

        f(...);

-return len;

---
v1: added checks on last calls only.
v2: added checks on all calls that return values along with labels.
v3: added changelog that was missed in v2.
v4: adjusted the patch to state how the problem was discovered, simplify the if conditions and avoid redundant error messages.

 drivers/staging/iio/meter/ade7758_core.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
drivers/staging/iio/meter/ade7758_core.c +8 -14
index 70e96b2..f696188 100644
@@ -303,14 +303,11 @@ static int ade7758_reset(struct device *dev)
 	int ret;
 	u8 val;
 
-	ade7758_spi_read_reg_8(dev,
-			ADE7758_OPMODE,
-			&val);
+	ret = ade7758_spi_read_reg_8(dev, ADE7758_OPMODE, &val);
+	if (ret < 0)
+		return ret;
 	val |= 1 << 6; /* Software Chip Reset */
-	ret = ade7758_spi_write_reg_8(dev,
-			ADE7758_OPMODE,
-			val);
-
+	ret = ade7758_spi_write_reg_8(dev, ADE7758_OPMODE, val);
 	return ret;
 }
 
@@ -444,14 +441,11 @@ static int ade7758_stop_device(struct device *dev)
 	int ret;
 	u8 val;
 
-	ade7758_spi_read_reg_8(dev,
-			ADE7758_OPMODE,
-			&val);
+	ret = ade7758_spi_read_reg_8(dev, ADE7758_OPMODE, &val);
+	if (ret < 0)
+		return ret;
 	val |= 7 << 3;  /* ADE7758 powered down */
-	ret = ade7758_spi_write_reg_8(dev,
-			ADE7758_OPMODE,
-			val);
-
+	ret = ade7758_spi_write_reg_8(dev, ADE7758_OPMODE, val);
 	return ret;
 }
 
Signature · 6 lines
1.9.3


-- 
Kind Regards,
Aya Saif El-yazal Mahfouz
#1 lore raw
Quoted 20 lines · Aya Mahfouz

Adds checks on variables that are used to return values. If the value is less than zero, this indicates that an error occurred and hence a message is printed through dev_err. Checks are made on negative values only since spi_* functions return negative error codes.

The functions were found using the following script but the aforementioned modification was what was carried out in the end:
@@
identifier len,f;
@@

(quoted diff) +1 -3
-int len;
 ... when != len
     when strict
-len =
+return
        f(...);
-return len;
Quoted 1 line

Commit message looks good to me.

Quoted 15 lines

---
v1: added checks on last calls only.
v2: added checks on all calls that return values along with labels.
v3: added changelog that was missed in v2.
v4: adjusted the patch to state how the problem was discovered, simplify the if conditions and avoid redundant error messages.

 drivers/staging/iio/meter/ade7758_core.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
drivers/staging/iio/meter/ade7758_core.c +0 -0
index 70e96b2..f696188 100644

drivers/staging/iio/meter/ade7758_core.c static int ade7758_reset(struct device *dev) · line 308

Quoted 9 lines
drivers/staging/iio/meter/ade7758_core.c +3 -3
@@ -303,14 +303,11 @@ static int ade7758_reset(struct device *dev)
        int ret;
        u8 val;
 
-       ade7758_spi_read_reg_8(dev,
-                       ADE7758_OPMODE,
-                       &val);
+       ret = ade7758_spi_read_reg_8(dev, ADE7758_OPMODE, &val);
+       if (ret < 0)
+               return ret;

I think we should keep the warning messages. Here we could say something like this
dev_err( ... "Failed to read opmode reg\n");

drivers/staging/iio/meter/ade7758_core.c static int ade7758_reset(struct device *dev) · line 310

Quoted 6 lines
drivers/staging/iio/meter/ade7758_core.c +1 -4
        val |= 1 << 6; /* Software Chip Reset */
-       ret = ade7758_spi_write_reg_8(dev,
-                       ADE7758_OPMODE,
-                       val);
-
+       ret = ade7758_spi_write_reg_8(dev, ADE7758_OPMODE, val);

Then also:

if (ret)

       dev_err( ... "FAiled to write opmode reg\n");
Quoted 10 lines
drivers/staging/iio/meter/ade7758_core.c +0 -3
        return ret;
 }
 
@@ -444,14 +441,11 @@ static int ade7758_stop_device(struct device *dev)
        int ret;
        u8 val;
 
-       ade7758_spi_read_reg_8(dev,
-                       ADE7758_OPMODE,
-                       &val);

drivers/staging/iio/meter/ade7758_core.c static int ade7758_stop_device(struct device *dev) · line 449

Quoted 10 lines
drivers/staging/iio/meter/ade7758_core.c +4 -4
+       ret = ade7758_spi_read_reg_8(dev, ADE7758_OPMODE, &val);
+       if (ret < 0)
+               return ret;
        val |= 7 << 3;  /* ADE7758 powered down */
-       ret = ade7758_spi_write_reg_8(dev,
-                       ADE7758_OPMODE,
-                       val);
-
+       ret = ade7758_spi_write_reg_8(dev, ADE7758_OPMODE, val);
        return ret;

The same here.

#2 lore raw
Quoted 61 lines · Daniel Baluta
Quoted 20 lines · Aya Mahfouz

Adds checks on variables that are used to return values. If the value is less than zero, this indicates that an error occurred and hence a message is printed through dev_err. Checks are made on negative values only since spi_* functions return negative error codes.

The functions were found using the following script but the aforementioned modification was what was carried out in the end:
@@
identifier len,f;
@@

(quoted diff) +1 -3
-int len;
 ... when != len
     when strict
-len =
+return
        f(...);
-return len;
Quoted 1 line

Commit message looks good to me.

Quoted 15 lines

---
v1: added checks on last calls only.
v2: added checks on all calls that return values along with labels.
v3: added changelog that was missed in v2.
v4: adjusted the patch to state how the problem was discovered, simplify the if conditions and avoid redundant error messages.

 drivers/staging/iio/meter/ade7758_core.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
drivers/staging/iio/meter/ade7758_core.c +0 -0
index 70e96b2..f696188 100644

drivers/staging/iio/meter/ade7758_core.c static int ade7758_reset(struct device *dev) · line 308

Quoted 9 lines
drivers/staging/iio/meter/ade7758_core.c +3 -3
@@ -303,14 +303,11 @@ static int ade7758_reset(struct device *dev)
        int ret;
        u8 val;
 
-       ade7758_spi_read_reg_8(dev,
-                       ADE7758_OPMODE,
-                       &val);
+       ret = ade7758_spi_read_reg_8(dev, ADE7758_OPMODE, &val);
+       if (ret < 0)
+               return ret;

I think we should keep the warning messages. Here we could say something like this
dev_err( ... "Failed to read opmode reg\n");

drivers/staging/iio/meter/ade7758_core.c static int ade7758_reset(struct device *dev) · line 310

Quoted 6 lines
drivers/staging/iio/meter/ade7758_core.c +1 -4
        val |= 1 << 6; /* Software Chip Reset */
-       ret = ade7758_spi_write_reg_8(dev,
-                       ADE7758_OPMODE,
-                       val);
-
+       ret = ade7758_spi_write_reg_8(dev, ADE7758_OPMODE, val);
Quoted 5 lines

Then also:

if (ret)

       dev_err( ... "FAiled to write opmode reg\n");

ok, will do. Thanks Daniel!

Quoted 22 lines
Quoted 10 lines
drivers/staging/iio/meter/ade7758_core.c +0 -3
        return ret;
 }
 
@@ -444,14 +441,11 @@ static int ade7758_stop_device(struct device *dev)
        int ret;
        u8 val;
 
-       ade7758_spi_read_reg_8(dev,
-                       ADE7758_OPMODE,
-                       &val);

drivers/staging/iio/meter/ade7758_core.c static int ade7758_stop_device(struct device *dev) · line 449

Quoted 10 lines
drivers/staging/iio/meter/ade7758_core.c +4 -4
+       ret = ade7758_spi_read_reg_8(dev, ADE7758_OPMODE, &val);
+       if (ret < 0)
+               return ret;
        val |= 7 << 3;  /* ADE7758 powered down */
-       ret = ade7758_spi_write_reg_8(dev,
-                       ADE7758_OPMODE,
-                       val);
-
+       ret = ade7758_spi_write_reg_8(dev, ADE7758_OPMODE, val);
        return ret;

The same here.

Signature · 2 lines
Kind Regards,
Aya Saif El-yazal Mahfouz