linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Pekka Enberg <penberg@cs.helsinki.fi>
To: Milton Miller <miltonm@bga.com>
Cc: Joe Perches <joe@perches.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 00/12] treewide: Remove unnecessary kmalloc casts
Date: Wed, 9 Jun 2010 13:06:59 +0300	[thread overview]
Message-ID: <AANLkTimSgiMP8EZ57Eba00vGsVTA3QKRZbh1bNYavISv@mail.gmail.com> (raw)
In-Reply-To: <1276068001_13533@mail4.comsite.net>

Hi Milton,

On Wed, Jun 9, 2010 at 10:20 AM, Milton Miller <miltonm@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
> =A0 type *p;
> =A0 <code>
> =A0 p =3D (type *)kmalloc(sizeof(type), ...);
>
> For example, from the powerpc patch:
>> goto out;
>> }
>> - tmp_part =3D (struct nvram_partition *)
>> - kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
>> + tmp_part =3D kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
>> err =3D -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
> =A0 p =3D 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

      parent reply	other threads:[~2010-06-09 10:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1275968594.git.joe@perches.com>
     [not found] ` <96933b66aa5f995746a34410ca63e5e8b84593cb.1275968594.git.joe@perches.com>
2010-06-09  7:20   ` [PATCH 00/12] treewide: Remove unnecessary kmalloc casts Milton Miller
2010-06-09  7:46     ` Joe Perches
2010-06-09 10:06     ` Pekka Enberg [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AANLkTimSgiMP8EZ57Eba00vGsVTA3QKRZbh1bNYavISv@mail.gmail.com \
    --to=penberg@cs.helsinki.fi \
    --cc=akpm@linux-foundation.org \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=miltonm@bga.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).