Re: [PATCH] iio: adc: ad7173: Remove const from chan_arr allocation type

From: Kees Cook

Date: Mon Sep 21 2026 - 04:34:34 EST


On Mon, Sep 21, 2026 at 12:21:28AM +0100, Jonathan Cameron wrote:
> On Fri, 18 Sep 2026 09:51:48 +0300
> Andy Shevchenko <andriy.shevchenko@xxxxxxxxx> wrote:
>
> > On Thu, Sep 17, 2026 at 02:14:37PM -0700, Kees Cook wrote:
> >
> > > In preparation for making the devm_kmalloc family of allocators type
> > > aware, we need to make sure that the returned type from the allocation
> > > matches the type of the variable being assigned. (Before, the allocator
> > > would always return "void *", which can be implicitly cast to any
> > > pointer type.)
> > >
> > > The assigned type is "struct iio_chan_spec *", but the converted
> > > allocation type would be "const struct iio_chan_spec *", as the size was
> > > taken from "*indio_dev->channels", and "indio_dev->channels" points to
> > > const. As there is no general way to remove const qualifiers, take the
> > > size from the assignment target instead. No change in allocation size
> > > results.
> >
> > Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>
> >
> > ...
> >
> > > - chan_arr = devm_kcalloc(dev, sizeof(*indio_dev->channels),
> > > + chan_arr = devm_kcalloc(dev, sizeof(*chan_arr),
> > > st->num_channels, GFP_KERNEL);
> >
> > I would even dare to put it on a single line now (yes, 86 characters long).
> > (No need to resend, Jonathan I hope can tweak if he agrees with this.)
> Done, though I would assume Kees is going to send a mega series
> that rewrites this line anyway and might well make it even shorter.

I will indeed. :) But Coccinelle will be happy with whatever shape it
has. (It'll swap "devm_kcalloc" for "devm_kmalloc" and then gain "_objs"
and lose ", GFP_KERNEL".)

> Applied to the testing branch of iio.git (queued for next merge window
> subject to tests etc)

Thanks!

--
Kees Cook