Skip to contentSkip to conversation

staging: iio: Add error code handling

PATCH Superseded Cristina Moraru 3 participants 7 messages Oct 20, 2015 – Oct 25, 2015

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

PATCH 1/1 staging: iio: Add error code handling

The function adis16060_spi_read can return negative values which must be caught and handled.

---
 drivers/staging/iio/gyro/adis16060_core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/staging/iio/gyro/adis16060_core.c +10 -4
index 80783e7..fb02bed 100644
@@ -89,11 +89,13 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
 		/* Take the iio_dev status lock */
 		mutex_lock(&indio_dev->mlock);
 		ret = adis16060_spi_write(indio_dev, chan->address);
-		if (ret < 0) {
-			mutex_unlock(&indio_dev->mlock);
-			return ret;
-		}
+		if (ret < 0)
+			goto error_ret;
+
 		ret = adis16060_spi_read(indio_dev, &tval);
+		if (ret < 0)
+			goto error_ret;
+
 		mutex_unlock(&indio_dev->mlock);
 		*val = tval;
 		return IIO_VAL_INT;
@@ -108,6 +110,10 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
 	}
 
 	return -EINVAL;
+
+error_ret:
+	mutex_unlock(&indio_dev->mlock);
+	return ret;
 }
 
 static const struct iio_info adis16060_info = {
Signature · 1 line
1.9.1
#1 lore raw
Quoted 2 lines · Cristina Moraru

The function adis16060_spi_read can return negative values which must be caught and handled.

The comment doesn't completely describe the changes performed in the patch. That is, the patch has an impact on the call to adis16060_spi_write too, but that isn't mentioned.

It is better for the label to reflect what is done at the destination, so something like out_unlock could work here. Just from the code shown in the patch, error_ret seems a little strange, because the return just above it is clearly indicating an error too.

julia

Quoted 49 lines
---
 drivers/staging/iio/gyro/adis16060_core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/staging/iio/gyro/adis16060_core.c +10 -5
index 80783e7..fb02bed 100644
@@ -89,11 +89,13 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
 		/* Take the iio_dev status lock */
 		mutex_lock(&indio_dev->mlock);
 		ret = adis16060_spi_write(indio_dev, chan->address);
-		if (ret < 0) {
-			mutex_unlock(&indio_dev->mlock);
-			return ret;
-		}
+		if (ret < 0)
+			goto error_ret;
+
 		ret = adis16060_spi_read(indio_dev, &tval);
+		if (ret < 0)
+			goto error_ret;
+
 		mutex_unlock(&indio_dev->mlock);
 		*val = tval;
 		return IIO_VAL_INT;
@@ -108,6 +110,10 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
 	}
 
 	return -EINVAL;
+
+error_ret:
+	mutex_unlock(&indio_dev->mlock);
+	return ret;
 }
 
 static const struct iio_info adis16060_info = {
--

1.9.1

--
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/1445379022-14114-1-git-send-email-cristina.moraru09%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

#2 lore raw

2015-10-21 11:05 GMT+03:00 Julia Lawall <[email protected]>:

Quoted 9 lines · Cristina Moraru
Quoted 3 lines

The function adis16060_spi_read can return negative values which must be caught and handled.

The comment doesn't completely describe the changes performed in the patch. That is, the patch has an impact on the call to adis16060_spi_write too, but that isn't mentioned.

Quoted 8 lines

It is better for the label to reflect what is done at the destination, so something like out_unlock could work here. Just from the code shown in the patch, error_ret seems a little strange, because the return just above it is clearly indicating an error too.

julia

For the label name I chose another label found in file but it's true that it does not describe
completely the current situation. I will change it accordingly.

Thanks for the feedback,
Cristina

Quoted 57 lines

Review

Quoted 7 lines
---
 drivers/staging/iio/gyro/adis16060_core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/iio/gyro/adis16060_core.c

drivers/staging/iio/gyro/adis16060_core.c static int adis16060_read_raw(struct iio_dev

Quoted 4 lines

index 80783e7..fb02bed 100644

drivers/staging/iio/gyro/adis16060_core.c +0 -0
@@ -89,11 +89,13 @@ static int adis16060_read_raw(struct iio_dev

*indio_dev,

drivers/staging/iio/gyro/adis16060_core.c static int adis16060_read_raw(struct iio_dev

Quoted 18 lines
drivers/staging/iio/gyro/adis16060_core.c +6 -4
              /* Take the iio_dev status lock */
              mutex_lock(&indio_dev->mlock);
              ret = adis16060_spi_write(indio_dev, chan->address);
-             if (ret < 0) {
-                     mutex_unlock(&indio_dev->mlock);
-                     return ret;
-             }
+             if (ret < 0)
+                     goto error_ret;
+
              ret = adis16060_spi_read(indio_dev, &tval);
+             if (ret < 0)
+                     goto error_ret;
+
              mutex_unlock(&indio_dev->mlock);
              *val = tval;
              return IIO_VAL_INT;
@@ -108,6 +110,10 @@ static int adis16060_read_raw(struct iio_dev

*indio_dev,

Quoted 13 lines
drivers/staging/iio/gyro/adis16060_core.c +4 -1
      }
 
      return -EINVAL;
+
+error_ret:
+     mutex_unlock(&indio_dev->mlock);
+     return ret;
 }
 
 static const struct iio_info adis16060_info = {
--

1.9.1

Quoted 2 lines

--
You received this message because you are subscribed to the Google

Groups "outreachy-kernel" group.

Quoted 1 line

To unsubscribe from this group and stop receiving emails from it, send

an email to [email protected].

Quoted 2 lines

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/1445379022-14114-1-git-send-email-cristina.moraru09%40gmail.com
.

Quoted 2 lines

For more options, visit https://groups.google.com/d/optout.

#3 lore raw
Quoted 21 lines · Cristina-Gabriela Moraru

2015-10-21 11:05 GMT+03:00 Julia Lawall <[email protected]>:
On Wed, 21 Oct 2015, Cristina Moraru wrote:

> The function adis16060_spi_read can return negative values > which must be caught and handled.

The comment doesn't completely describe the changes performed in the
patch.  That is, the patch has an impact on the call to adis16060_spi_write too, but that isn't mentioned.

It is better for the label to reflect what is done at the destination, so
something like out_unlock could work here.  Just from the code shown in
the patch, error_ret seems a little strange, because the return just above
it is clearly indicating an error too.

Quoted 4 lines

julia

For the label name I chose another label found in file

OK, it's not a bad strategy. But in this case, it is a bit strange to have an error_ret label and other error returning code.

julia

Quoted 79 lines

but it's true that it
does not describe
completely the current situation. I will change it accordingly.

Thanks for the feedback,
Cristina

      >
      > Signed-off-by: Cristina Moraru <[email protected]>
      > ---
      >  drivers/staging/iio/gyro/adis16060_core.c | 14 ++++++++++----
      >  1 file changed, 10 insertions(+), 4 deletions(-)
      >
      > diff --git a/drivers/staging/iio/gyro/adis16060_core.c
      b/drivers/staging/iio/gyro/adis16060_core.c
      > index 80783e7..fb02bed 100644
      > --- a/drivers/staging/iio/gyro/adis16060_core.c
      > +++ b/drivers/staging/iio/gyro/adis16060_core.c
      > @@ -89,11 +89,13 @@ static int adis16060_read_raw(struct
      iio_dev *indio_dev,
      >               /* Take the iio_dev status lock */
      >               mutex_lock(&indio_dev->mlock);
      >               ret = adis16060_spi_write(indio_dev,
      chan->address);
      > -             if (ret < 0) {
      > -                     mutex_unlock(&indio_dev->mlock);
      > -                     return ret;
      > -             }
      > +             if (ret < 0)
      > +                     goto error_ret;
      > +
      >               ret = adis16060_spi_read(indio_dev, &tval);
      > +             if (ret < 0)
      > +                     goto error_ret;
      > +
      >               mutex_unlock(&indio_dev->mlock);
      >               *val = tval;
      >               return IIO_VAL_INT;
      > @@ -108,6 +110,10 @@ static int adis16060_read_raw(struct
      iio_dev *indio_dev,
      >       }
      >
      >       return -EINVAL;
      > +
      > +error_ret:
      > +     mutex_unlock(&indio_dev->mlock);
      > +     return ret;
      >  }
      >
      >  static const struct iio_info adis16060_info = {
      > --
      > 1.9.1
      >
Quoted 2 lines

--
You received this message because you are subscribed to the Google

Groups "outreachy-kernel" group.

Quoted 1 line

To unsubscribe from this group and stop receiving emails from it,

send an email to [email protected].

Quoted 1 line

To post to this group, send email to

[email protected].

Quoted 1 line

To view this discussion on the web visithttps://groups.google.com/d/msgid/outreachy-kernel/1445379022-14114-1-git-s

end-email-cristina.moraru09%40gmail.com.

Quoted 2 lines

For more options, visit https://groups.google.com/d/optout.

--
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 visithttps://groups.google.com/d/msgid/outreachy-kernel/CAGZ2q2zs65X6SPcDWHSO%2B GiyUsdtVr1FX4nbJFKRG%3DttWEBZJQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

#4 lore raw

PATCH v2 1/1 staging: iio: Add exit label and error code handling

In reply to Cristina Moraru

This commit introduces a label for unlocking the mutex and exiting the function adis16060_read_raw and redirects the exit on failure of function call adis16060_spi_write to that label. Also, adds error handling of the function call adis16060_spi_read by a redirection to the same label.

---
 drivers/staging/iio/gyro/adis16060_core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/staging/iio/gyro/adis16060_core.c +10 -4
index 80783e7..981b63f 100644
@@ -89,11 +89,13 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
 		/* Take the iio_dev status lock */
 		mutex_lock(&indio_dev->mlock);
 		ret = adis16060_spi_write(indio_dev, chan->address);
-		if (ret < 0) {
-			mutex_unlock(&indio_dev->mlock);
-			return ret;
-		}
+		if (ret < 0)
+			goto out_unlock;
+
 		ret = adis16060_spi_read(indio_dev, &tval);
+		if (ret < 0)
+			goto out_unlock;
+
 		mutex_unlock(&indio_dev->mlock);
 		*val = tval;
 		return IIO_VAL_INT;
@@ -108,6 +110,10 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
 	}
 
 	return -EINVAL;
+
+out_unlock:
+	mutex_unlock(&indio_dev->mlock);
+	return ret;
 }
 
 static const struct iio_info adis16060_info = {
Signature · 1 line
1.9.1
#5 lore raw

Re: [Outreachy kernel] [PATCH v2] staging: iio: Add exit label and error code handling

Quoted 6 lines · Cristina Moraru

This commit introduces a label for unlocking the mutex and exiting the function adis16060_read_raw and redirects the exit on failure of function call adis16060_spi_write to that label. Also, adds error handling of the function call adis16060_spi_read by a redirection to the same label.

The code is correct. Anyhow, the commit message should say why this change is needed.

staging: iio: adis16060_core: Fix error handling

We need to check adis16060_spi_read return code to avoid feeding user space with bogus data.
While, at it introduce out_unlock label in order to simplify locking on error path.

Quoted 47 lines
---
 drivers/staging/iio/gyro/adis16060_core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/staging/iio/gyro/adis16060_core.c +10 -5
index 80783e7..981b63f 100644
@@ -89,11 +89,13 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
                /* Take the iio_dev status lock */
                mutex_lock(&indio_dev->mlock);
                ret = adis16060_spi_write(indio_dev, chan->address);
-               if (ret < 0) {
-                       mutex_unlock(&indio_dev->mlock);
-                       return ret;
-               }
+               if (ret < 0)
+                       goto out_unlock;
+
                ret = adis16060_spi_read(indio_dev, &tval);
+               if (ret < 0)
+                       goto out_unlock;
+
                mutex_unlock(&indio_dev->mlock);
                *val = tval;
                return IIO_VAL_INT;
@@ -108,6 +110,10 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
        }
 
        return -EINVAL;
+
+out_unlock:
+       mutex_unlock(&indio_dev->mlock);
+       return ret;
 }
 
 static const struct iio_info adis16060_info = {
--

1.9.1

--
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/1445446380-19431-1-git-send-email-cristina.moraru09%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

#6 lore raw

PATCH 1/1 staging: iio: adis16060_core: Fix error handling

In reply to Cristina Moraru

We need to check adis16060_spi_read return code to avoid feeding user space with bogus data.
While, at it introduce out_unlock label in order to simplify locking on error path.

---
 drivers/staging/iio/gyro/adis16060_core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/staging/iio/gyro/adis16060_core.c +10 -4
index 80783e7..981b63f 100644
@@ -89,11 +89,13 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
 		/* Take the iio_dev status lock */
 		mutex_lock(&indio_dev->mlock);
 		ret = adis16060_spi_write(indio_dev, chan->address);
-		if (ret < 0) {
-			mutex_unlock(&indio_dev->mlock);
-			return ret;
-		}
+		if (ret < 0)
+			goto out_unlock;
+
 		ret = adis16060_spi_read(indio_dev, &tval);
+		if (ret < 0)
+			goto out_unlock;
+
 		mutex_unlock(&indio_dev->mlock);
 		*val = tval;
 		return IIO_VAL_INT;
@@ -108,6 +110,10 @@ static int adis16060_read_raw(struct iio_dev *indio_dev,
 	}
 
 	return -EINVAL;
+
+out_unlock:
+	mutex_unlock(&indio_dev->mlock);
+	return ret;
 }
 
 static const struct iio_info adis16060_info = {
Signature · 1 line
1.9.1