Skip to contentSkip to conversation

staging: iio: meter: add check on return variables

PATCH Superseded Aya Mahfouz 4 participants 7 messages Mar 3, 2015 1 message not in the archive

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 occured and hence a message is printed through dev_err. Labels have been added too, since the check is done more than once.

---
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.

 drivers/staging/iio/meter/ade7758_core.c | 35 ++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/staging/iio/meter/ade7758_core.c +22 -13
index 70e96b2..cab392f 100644
@@ -303,14 +303,18 @@ 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) {
+		dev_err(dev, "failed to read from device");
+		goto error_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);
+	if (ret < 0) {
+		dev_err(dev, "failed to reset device");
+		goto error_ret;
+	}
+error_ret:
 	return ret;
 }
 
@@ -444,14 +448,19 @@ 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) {
+		dev_err(dev, "failed to read from device");
+		goto error_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);
+	if (ret < 0) {
+		dev_err(dev, "failed to stop device");
+		goto error_ret;
+	}
 
+error_ret:
 	return ret;
 }
 
Signature · 6 lines
1.9.3


-- 
Kind Regards,
Aya Saif El-yazal Mahfouz
#2 lore raw
In reply to a message not in the archive

+ list :)

Quoted 76 lines · Daniel Baluta
Quoted 59 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 occured and hence a message is printed through dev_err. Labels have been added too, since the check is done more than once.

---
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.

 drivers/staging/iio/meter/ade7758_core.c | 35 ++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/staging/iio/meter/ade7758_core.c +16 -12
index 70e96b2..cab392f 100644
@@ -303,14 +303,18 @@ 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) {
+               dev_err(dev, "failed to read from device");
+               goto error_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);
+       if (ret < 0) {
+               dev_err(dev, "failed to reset device");
+               goto error_ret;
+       }
+error_ret:
        return ret;
 }
 
@@ -444,14 +448,19 @@ 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) {
+               dev_err(dev, "failed to read from device");
+               goto error_ret;
+       }
        val |= 7 << 3;  /* ADE7758 powered down */
-       ret = ade7758_spi_write_reg_8(dev,
-                       ADE7758_OPMODE,

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

Quoted 10 lines
drivers/staging/iio/meter/ade7758_core.c +6 -1
-                       val);
+       ret = ade7758_spi_write_reg_8(dev, ADE7758_OPMODE, val);
+       if (ret < 0) {
+               dev_err(dev, "failed to stop device");
+               goto error_ret;
+       }
 
+error_ret:
        return ret;
 }

Is there any particular reason for which you preferred gotos here? I don't think we need them. Just directly return ret;.

Daniel.

#3 lore raw
In reply to Aya Mahfouz
Quoted 4 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 occured and hence a message is printed through dev_err. Labels have been added too, since the check is done more than once.

How did you find the functions that have this property?

julia

Quoted 80 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.

 drivers/staging/iio/meter/ade7758_core.c | 35 ++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/staging/iio/meter/ade7758_core.c +22 -14
index 70e96b2..cab392f 100644
@@ -303,14 +303,18 @@ 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) {
+		dev_err(dev, "failed to read from device");
+		goto error_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);
+	if (ret < 0) {
+		dev_err(dev, "failed to reset device");
+		goto error_ret;
+	}
+error_ret:
 	return ret;
 }
 
@@ -444,14 +448,19 @@ 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) {
+		dev_err(dev, "failed to read from device");
+		goto error_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);
+	if (ret < 0) {
+		dev_err(dev, "failed to stop device");
+		goto error_ret;
+	}
 
+error_ret:
 	return ret;
 }
 
--

1.9.3

--
Kind Regards,
Aya Saif El-yazal Mahfouz

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20150303112705.GA11879%40waves.
For more options, visit https://groups.google.com/d/optout.

#4 lore raw
Quoted 9 lines · On Tuesday 03 March 2015 07:01:47 Julia Lawall wrote:

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

Quoted 8 lines
drivers/staging/iio/meter/ade7758_core.c +5 -3
-     ade7758_spi_read_reg_8(dev,
-                     ADE7758_OPMODE,
-                     &val);
+     ret = ade7758_spi_read_reg_8(dev, ADE7758_OPMODE, &val);
+     if (ret < 0) {
+             dev_err(dev, "failed to read from device");
+             goto error_ret;
+     }

The ade7758_spi_read_reg_8 already prints an error message, I don't think we want to see two messages about the same error.

Arnd

#6 lore raw
In reply to Julia Lawall
Quoted 9 lines · Julia Lawall
Quoted 4 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 occured and hence a message is printed through dev_err. Labels have been added too, since the check is done more than once.

How did you find the functions that have this property?

Do you mean that I should state that I found it using coccinelle?

Quoted 82 lines

julia

Quoted 80 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.

 drivers/staging/iio/meter/ade7758_core.c | 35 ++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/staging/iio/meter/ade7758_core.c +22 -14
index 70e96b2..cab392f 100644
@@ -303,14 +303,18 @@ 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) {
+		dev_err(dev, "failed to read from device");
+		goto error_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);
+	if (ret < 0) {
+		dev_err(dev, "failed to reset device");
+		goto error_ret;
+	}
+error_ret:
 	return ret;
 }
 
@@ -444,14 +448,19 @@ 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) {
+		dev_err(dev, "failed to read from device");
+		goto error_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);
+	if (ret < 0) {
+		dev_err(dev, "failed to stop device");
+		goto error_ret;
+	}
 
+error_ret:
 	return ret;
 }
 
--

1.9.3

--
Kind Regards,
Aya Saif El-yazal Mahfouz

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20150303112705.GA11879%40waves.
For more options, visit https://groups.google.com/d/optout.

Signature · 2 lines
Kind Regards,
Aya Saif El-yazal Mahfouz
#5 lore raw
In reply to Arnd Bergmann
Quoted 11 lines · Arnd Bergmann
Quoted 9 lines · On Tuesday 03 March 2015 07:01:47 Julia Lawall wrote:

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

Quoted 8 lines
drivers/staging/iio/meter/ade7758_core.c +5 -3
-     ade7758_spi_read_reg_8(dev,
-                     ADE7758_OPMODE,
-                     &val);
+     ret = ade7758_spi_read_reg_8(dev, ADE7758_OPMODE, &val);
+     if (ret < 0) {
+             dev_err(dev, "failed to read from device");
+             goto error_ret;
+     }
Quoted 3 lines

The ade7758_spi_read_reg_8 already prints an error message, I don't think we want to see two messages about the same error.

good to know. Thanks Arnd!

Quoted 1 line

Arnd

Signature · 2 lines
Kind Regards,
Aya Saif El-yazal Mahfouz
#7 lore raw
In reply to Aya Mahfouz
Quoted 11 lines · Aya Mahfouz
Quoted 9 lines · Julia Lawall
Quoted 4 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 occured and hence a message is printed through dev_err. Labels have been added too, since the check is done more than once.

How did you find the functions that have this property?

Do you mean that I should state that I found it using coccinelle?

No, I meant that there are millions offunctions used in this code, so what motivated you to look at these ones. But the explanation you put in a later message is fine.

julia

Quoted 87 lines
Quoted 75 lines

julia

Quoted 73 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.

 drivers/staging/iio/meter/ade7758_core.c | 35 ++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/staging/iio/meter/ade7758_core.c +22 -14
index 70e96b2..cab392f 100644
@@ -303,14 +303,18 @@ 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) {
+		dev_err(dev, "failed to read from device");
+		goto error_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);
+	if (ret < 0) {
+		dev_err(dev, "failed to reset device");
+		goto error_ret;
+	}
+error_ret:
 	return ret;
 }
 
@@ -444,14 +448,19 @@ 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) {
+		dev_err(dev, "failed to read from device");
+		goto error_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);
+	if (ret < 0) {
+		dev_err(dev, "failed to stop device");
+		goto error_ret;
+	}
 
+error_ret:
 	return ret;
 }
 
--

1.9.3

--
Kind Regards,
Aya Saif El-yazal Mahfouz

Quoted 7 lines
Quoted 7 lines

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20150303112705.GA11879%40waves.
For more options, visit https://groups.google.com/d/optout.

--
Kind Regards,
Aya Saif El-yazal Mahfouz