2023-05-23 15:27:08

by Herve Codina

[permalink] [raw]
Subject: [PATCH v2 3/9] iio: inkern: Check error explicitly in iio_channel_read_max()

The current implementation returns the error code as part of the
default switch case.
This can lead to returning an incorrect positive value in case of
iio_avail_type enum entries evolution.

In order to avoid this case, be more strict in error checking.

Signed-off-by: Herve Codina <[email protected]>
---
drivers/iio/inkern.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 872fd5c24147..f738db9a0c04 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -858,6 +858,9 @@ static int iio_channel_read_max(struct iio_channel *chan,
val2 = &unused;

ret = iio_channel_read_avail(chan, &vals, type, &length, info);
+ if (ret < 0)
+ return ret;
+
switch (ret) {
case IIO_AVAIL_RANGE:
switch (*type) {
@@ -888,7 +891,7 @@ static int iio_channel_read_max(struct iio_channel *chan,
return 0;

default:
- return ret;
+ return -EINVAL;
}
}

--
2.40.1



2023-05-28 17:36:39

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v2 3/9] iio: inkern: Check error explicitly in iio_channel_read_max()

On Tue, 23 May 2023 17:12:17 +0200
Herve Codina <[email protected]> wrote:

> The current implementation returns the error code as part of the
> default switch case.
> This can lead to returning an incorrect positive value in case of
> iio_avail_type enum entries evolution.
>
> In order to avoid this case, be more strict in error checking.
>
> Signed-off-by: Herve Codina <[email protected]>
It's an odd code construct, so in the interests of robustness this
looks like a good improvement to me.

Given the later patch you need for this series will see some fuzz
I think if I pick this up separately I'll currently assume this whole
series will go together.

Acked-by: Jonathan Cameron <[email protected]>

> ---
> drivers/iio/inkern.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
> index 872fd5c24147..f738db9a0c04 100644
> --- a/drivers/iio/inkern.c
> +++ b/drivers/iio/inkern.c
> @@ -858,6 +858,9 @@ static int iio_channel_read_max(struct iio_channel *chan,
> val2 = &unused;
>
> ret = iio_channel_read_avail(chan, &vals, type, &length, info);
> + if (ret < 0)
> + return ret;
> +
> switch (ret) {
> case IIO_AVAIL_RANGE:
> switch (*type) {
> @@ -888,7 +891,7 @@ static int iio_channel_read_max(struct iio_channel *chan,
> return 0;
>
> default:
> - return ret;
> + return -EINVAL;
> }
> }
>