iio: imu: st_lsm6dsx: Allocate ext_channels with ARRAY_SIZE()
Subsystems (2)
PATCH 1/1 iio: imu: st_lsm6dsx: Allocate ext_channels with ARRAY_SIZE()
From: Kees Cook <[email protected]>
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
---
Cc: Lorenzo Bianconi <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: David Lechner <[email protected]>
Cc: "Nuno Sá" <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: <[email protected]>
--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +2 -2
@@ -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 ;
Signature · 1 line
2.34.1
Quoted 14 lines
From: Kees Cook <[email protected]>
In preparation for making the
devm_kmallocfamily 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 structiio_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 "structiio_chan_spec*" being assigned. Allocate ARRAY_SIZE-many entries instead. The resulting allocation size is the same.
Quoted 5 lines
Build tested ARCH=x86_64 allmodconfig with GCC 16.2.0:
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.oAssisted-by LLM coccinelleSigned-off-by Kees Cook <[email protected]>
Quoted 29 lines
---
Cc: Lorenzo Bianconi <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: David Lechner <[email protected]>
Cc: "Nuno Sá" <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +2
-2@@ -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 ;Signature · 1 line
2.34.1
Jonathan Cameron
maintainerQuoted 14 lines · On Fri, 18 Sep 2026 08:40:05 +0200 Lorenzo Bianconi <[email protected]> wrote:
Quoted 14 lines
From: Kees Cook <[email protected]>
In preparation for making the
devm_kmallocfamily 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 structiio_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 "structiio_chan_spec*" being assigned. Allocate ARRAY_SIZE-many entries instead. The resulting allocation size is the same.
Quoted 7 lines
Quoted 5 lines
Build tested ARCH=x86_64 allmodconfig with GCC 16.2.0:
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.oAssisted-by LLM coccinelleSigned-off-by Kees Cook <[email protected]>Acked-by Lorenzo Bianconi <[email protected]>
Applied to the testing branch of iio.git (next merge window material).
Thanks
Jonathan
Quoted 30 lines
Quoted 30 lines
---
Cc: Lorenzo Bianconi <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: David Lechner <[email protected]>
Cc: "Nuno Sá" <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: <[email protected]>--- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +2
-2@@ -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 ;Signature · 1 line
2.34.1