Re: [PATCH] iio: imu: st_lsm6dsx: Allocate ext_channels with ARRAY_SIZE()
From: Jonathan Cameron
Date: Sun Sep 20 2026 - 19:23:07 EST
On Fri, 18 Sep 2026 08:40:05 +0200
Lorenzo Bianconi <lorenzo@xxxxxxxxxx> wrote:
> > From: Kees Cook <kees+treewide@xxxxxxxxxx>
> >
> > 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.)
> >
> > This is allocating a copy of magn_channels, which is an array of struct
> > iio_chan_spec, but the size was taken from the whole array, which would
> > make the allocation type a pointer to the array rather than the
> > "struct iio_chan_spec *" being assigned. Allocate ARRAY_SIZE-many
> > entries instead. The resulting allocation size is the same.
> >
> > Build tested ARCH=x86_64 allmodconfig with GCC 16.2.0:
> > drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.o
> >
> > Assisted-by: LLM coccinelle
> > Signed-off-by: Kees Cook <kees+treewide@xxxxxxxxxx>
>
> Acked-by: Lorenzo Bianconi <lorenzo@xxxxxxxxxx>
Applied to the testing branch of iio.git (next merge window
material).
Thanks
Jonathan
>
> > ---
> > Cc: Lorenzo Bianconi <lorenzo@xxxxxxxxxx>
> > Cc: Jonathan Cameron <jic23@xxxxxxxxxx>
> > Cc: David Lechner <dlechner@xxxxxxxxxxxx>
> > Cc: "Nuno Sá" <nuno.sa@xxxxxxxxxx>
> > Cc: Andy Shevchenko <andy@xxxxxxxxxx>
> > Cc: <linux-iio@xxxxxxxxxxxxxxx>
> > ---
> > drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c
> > index d6a1eeb151ca..cbc47fa5b101 100644
> > --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c
> > +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c
> > @@ -766,8 +766,8 @@ st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw *hw,
> > IIO_CHAN_SOFT_TIMESTAMP(3),
> > };
> >
> > - ext_channels = devm_kzalloc(hw->dev, sizeof(magn_channels),
> > - GFP_KERNEL);
> > + ext_channels = devm_kcalloc(hw->dev, ARRAY_SIZE(magn_channels),
> > + sizeof(*ext_channels), GFP_KERNEL);
> > if (!ext_channels)
> > return NULL;
> >
> > --
> > 2.34.1
> >