Re: [PATCH] irqchip/apple-aic: application of sizeof() to a pointer

From: Marc Zyngier
Date: Thu Mar 10 2022 - 03:27:38 EST


On Thu, 10 Mar 2022 05:02:38 +0000,
Guo Zhengkui <guozhengkui@xxxxxxxx> wrote:
>
> `ic->fiq_aff[fiq]` is a pointer to the unnamed struct.
> `sizeof(ic->fiq_aff[fiq])` intends to get the size of this pointer. But
> readers may get confused. `sizeof(unsigned long)` makes more sense because
> `unsigned long` has the same size of pointer.

More sense? It really depends on who reads the code.

>
> reference:
> https://lore.kernel.org/all/Ya%2FeUbdN1+ABFVWf@xxxxxxxxxxxxxxxxxxx/
> https://lore.kernel.org/all/YbBGGI9wQenI4kP7@xxxxxxxxx/
> https://lore.kernel.org/all/20211209062441.9856-1-guozhengkui@xxxxxxxx/
>
> Signed-off-by: Guo Zhengkui <guozhengkui@xxxxxxxx>
> ---
> drivers/irqchip/irq-apple-aic.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c
> index b40199c6625e..23098b469b1a 100644
> --- a/drivers/irqchip/irq-apple-aic.c
> +++ b/drivers/irqchip/irq-apple-aic.c
> @@ -810,7 +810,7 @@ static void build_fiq_affinity(struct aic_irq_chip *ic, struct device_node *aff)
> if (WARN_ON(n < 0))
> return;
>
> - ic->fiq_aff[fiq] = kzalloc(sizeof(ic->fiq_aff[fiq]), GFP_KERNEL);
> + ic->fiq_aff[fiq] = kzalloc(sizeof(unsigned long), GFP_KERNEL);

And by doing that, you are making even more difficult to spot the
glaring bug that is in front of your eyes: we're not trying to
allocate a pointer, but to allocate the full data structure.

Nothing went wrong as a 64bit allocation is plenty when you need at
most 10 bits, but jeez, what a howler. I'm stashing the fixlet below
on top.

Thanks,

M.

diff --git a/drivers/irqchip/irq-apple-aic.c b/drivers/irqchip/irq-apple-aic.c
index b40199c6625e..3f1d2f3ccb7f 100644
--- a/drivers/irqchip/irq-apple-aic.c
+++ b/drivers/irqchip/irq-apple-aic.c
@@ -810,7 +810,7 @@ static void build_fiq_affinity(struct aic_irq_chip *ic, struct device_node *aff)
if (WARN_ON(n < 0))
return;

- ic->fiq_aff[fiq] = kzalloc(sizeof(ic->fiq_aff[fiq]), GFP_KERNEL);
+ ic->fiq_aff[fiq] = kzalloc(sizeof(*ic->fiq_aff[fiq]), GFP_KERNEL);
if (!ic->fiq_aff[fiq])
return;


--
Without deviation from the norm, progress is not possible.