[PATCH 00/12] treewide: Remove unnecessary kmalloc casts
Pekka Enberg
penberg at cs.helsinki.fi
Wed Jun 9 20:06:59 EST 2010
Hi Milton,
On Wed, Jun 9, 2010 at 10:20 AM, Milton Miller <miltonm at bga.com> wrote:
> However, in this case you are removing casts that, while not necessary
> for C, are indeed there for a reason.
>
> Specifically, they are of the form
> type *p;
> <code>
> p = (type *)kmalloc(sizeof(type), ...);
>
> For example, from the powerpc patch:
>> goto out;
>> }
>> - tmp_part = (struct nvram_partition *)
>> - kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
>> + tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
>> err = -ENOMEM;
>
> The reason they casts are present is to guard against someone changing
> the type of p at the top of the function and not changing the type at
> the kmalloc.
If you're worried about that...
[snip]
> There may have been discussion of doing the above vs
> p = kmalloc(sizeof(*p), ...);
...it's better to use this form. There's actually a mention of this in
"Chapter 14: Allocating memory" of Documentation/CodingStyle. The
guard is not really a guard as someone can still change the "sizeof"
part to something else and the cast from "void *" will silently ignore
it.
Pekka
More information about the Linuxppc-dev
mailing list