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

Assisted-by: LLM coccinelle
Signed-off-by: Kees Cook <[email protected]>
---
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(-)

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


Reply via email to