Skip to contentSkip to conversation

iio: adc: ad7173: Remove const from chan_arr allocation type

PATCH In review Kees Cook 4 participants 5 messages Sep 17 – Sep 21

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 iio: adc: ad7173: Remove const from chan_arr allocation type

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.)

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.

Build tested ARCH=x86_64 allmodconfig with GCC 16.2.0:
drivers/iio/adc/ad7173.o

Assisted-by LLM coccinelle

---
Cc: "Nuno Sá" <[email protected]>
Cc: Michael Hennerich <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: David Lechner <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>

---
 drivers/iio/adc/ad7173.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ad7173.c +1 -1
index eb47175a0528..fdd24f3ec1de 100644
@@ -1801,7 +1801,7 @@ static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev)
 	indio_dev->num_channels = num_channels;
 	st->num_channels = num_channels;
 
-	chan_arr = devm_kcalloc(dev, sizeof(*indio_dev->channels),
+	chan_arr = devm_kcalloc(dev, sizeof(*chan_arr),
 				st->num_channels, GFP_KERNEL);
 	if (!chan_arr)
 		return -ENOMEM;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Kees Cook

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.)

Quoted 6 lines

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.

...

drivers/iio/adc/ad7173.c static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev) · line 1805

Quoted 3 lines
drivers/iio/adc/ad7173.c +1 -1
-	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.)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v1 1/1
#4 lore raw
In reply to Kees Cook
Quoted 15 lines · On Thu, 17 Sep 2026 14:14:37 -0700 Kees Cook <[email protected]> wrote:

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.)

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.

Quoted 6 lines

Build tested ARCH=x86_64 allmodconfig with GCC 16.2.0:
drivers/iio/adc/ad7173.o

Assisted-by LLM coccinelle

---

Makes sense.

Signature · 2 lines
Kind regards,
Joshua Crofts
maintainer
#2 lore raw
In reply to Andy Shevchenko
Quoted 17 lines · On Fri, 18 Sep 2026 09:51:48 +0300 Andy Shevchenko…
Quoted 6 lines · Kees Cook

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.)

Quoted 6 lines

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.

Quoted 8 lines

...

drivers/iio/adc/ad7173.c static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev) · line 1805

Quoted 3 lines
drivers/iio/adc/ad7173.c +1 -1
-	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.

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

Thanks,

Jonathan

Quoted 1 line
author
#3 lore raw
Quoted 22 lines · Jonathan Cameron
Quoted 19 lines · On Fri, 18 Sep 2026 09:51:48 +0300 Andy Shevchenko…
Quoted 6 lines · Kees Cook

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.)

Quoted 6 lines

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.

...

Quoted 8 lines
Quoted 6 lines

drivers/iio/adc/ad7173.c static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev) · line 1805

Quoted 3 lines
drivers/iio/adc/ad7173.c +1 -1
-	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".)

Quoted 2 lines

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

Thanks!

Signature · 1 line
Kees Cook