Re: [PATCH 06/10] iio: adc: pac1934: Fix custom attribute allocation and channel bounds

From: Jonathan Cameron

Date: Sat Nov 21 2026 - 17:54:54 EST


On Thu, 19 Nov 2026 19:27:56 +0200
Andy Shevchenko <andriy.shevchenko@xxxxxxxxx> wrote:

> On Thu, Nov 19, 2026 at 03:30:39PM +0000, Lee Jones wrote:
> > pac1934_prep_custom_attributes() sizes the pac1934_custom_attr pointer
> > array using sizeof(*pac1934_group) instead of
> > sizeof(*pac1934_custom_attr) and adds 1 byte rather than one pointer
> > element for the terminating NULL entry. When active_channels_count is
> > zero, only a single byte is allocated and iio_device_register_sysfs()
> > reads past the allocation when scanning the attribute list.
> >
> > In addition, active_channels_count is counted over info->phys_channels,
> > whereas the population steps unconditionally inspect all four entries of
> > info->active_channels[], which can write past the end of the array on
> > 1-, 2-, and 3-channel chip variants.
> >
> > Allocate active_channels_count + 1 attribute pointers with
> > devm_kzalloc_objs() and bound the population loop by
> > info->phys_channels.
>
> ...
>
> > - int i, active_channels_count = 0;
> > + int i, j, active_channels_count = 0;
>
> While at it, can we move to make it more like a reversed xmas tree order?
> But see below first.
>
> ...
>
> > + j = 0;
> > + for (i = 0; i < info->phys_channels; i++)
> > + if (info->active_channels[i])
> > + pac1934_custom_attr[j++] = pac1934_shunt_attrs[i];
>
> But i is not used outside of the loop, can we rather have
>
> i = 0;
> for (int j = 0; j < info->phys_channels; j++)
> if (info->active_channels[j])
> pac1934_custom_attr[i++] = pac1934_shunt_attrs[j];
>
> ? In such a case the above hunk won't be needed.
>
This change seems reasonable but significant enough I'm not going to just
tweak it whilst applying.

So v2 on this one please.

Thanks,

J