Re: [PATCH v4 5/9] iio: light: gp2ap020a00f: Return directly from the switch cases

From: Ethan Tidmore

Date: Thu Feb 19 2026 - 14:33:35 EST


On Tue Feb 17, 2026 at 10:37 PM CST, Ethan Tidmore wrote:
> From: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
>
> Return directly from the switch cases which makes code easier to follow.
> In some cases convert pieces to the standard pattern which also unifies
> it with the accepted kernel practices.
>
> By doing this, it appears to have fixed a preexisting bug, the variable
> err in gp2ap020a00f_buffer_predisable() is only checked once for errors
> after the for loop is finished. This could allow errors to be swallowed.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
> Signed-off-by: Ethan Tidmore <ethantidmore06@xxxxxxxxx>
> ---
> v4:
> - Integrate Andy Shevchenko's cleanups.
>
> drivers/iio/light/gp2ap020a00f.c | 96 ++++++++++++--------------------
> 1 file changed, 37 insertions(+), 59 deletions(-)

...

>
> @@ -1366,7 +1344,7 @@ static const struct iio_info gp2ap020a00f_info = {
> static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev)
> {
> struct gp2ap020a00f_data *data = iio_priv(indio_dev);
> - int i, err = 0;
> + int i, err;
>
> guard(mutex)(&data->lock);
>
> @@ -1400,15 +1378,15 @@ static int gp2ap020a00f_buffer_postenable(struct iio_dev *indio_dev)
>
> data->buffer = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
> if (!data->buffer)
> - err = -ENOMEM;
> + return -ENOMEM;
>
> - return err;
> + return 0;
> }
>

Looking over the code again it looks like
gp2ap020a00f_buffer_postenable() contains the same bug you mentioned
where the error check should have been inside of the loop?


iio_for_each_active_channel(indio_dev, i) {
switch (i) {
case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR:
err = gp2ap020a00f_exec_cmd(data,
GP2AP020A00F_CMD_TRIGGER_CLEAR_EN);
break;
case GP2AP020A00F_SCAN_MODE_LIGHT_IR:
err = gp2ap020a00f_exec_cmd(data,
GP2AP020A00F_CMD_TRIGGER_IR_EN);
break;
case GP2AP020A00F_SCAN_MODE_PROXIMITY:
err = gp2ap020a00f_exec_cmd(data,
GP2AP020A00F_CMD_TRIGGER_PROX_EN);
break;
}
}

if (err < 0)
goto error_unlock;

Just wanted to confirm before putting it in the v5.

Thanks,

ET