staging: iio: meter: add check on return variables
Subsystems (4)
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
@@ -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
+ 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.Signed-off-by Aya Mahfouz <[email protected]>---
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@@ -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.
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
Signed-off-by Aya Mahfouz <[email protected]>---
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@@ -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.
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
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
Signed-off-by Aya Mahfouz <[email protected]>---
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@@ -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
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_8already 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
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
Signed-off-by Aya Mahfouz <[email protected]>---
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@@ -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 MahfouzQuoted 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