mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ben Pfaff <pfaffben@msu.edu>
To: linux-kernel@vger.kernel.org
Subject: Re: [CHECKER] 3 kmalloc underallocation bugs
Date: 05 Apr 2001 23:33:36 -0400	[thread overview]
Message-ID: <87wv8yg0rz.fsf@pfaffben.user.msu.edu> (raw)
In-Reply-To: <200104052245.PAA29663@csl.Stanford.EDU>
In-Reply-To: Dawson Engler's message of "Thu, 5 Apr 2001 15:45:47 -0700 (PDT)"

Dawson Engler <engler@csl.Stanford.EDU> writes:

> enclosed are three bugs found in the 2.4.1 kernel by an extension
> that checks that kmalloc calls allocate enough memory.  It examines all
> callsites of the form:
> 	p = [kv]malloc(nbytes);
> and issues an error if
> 	sizeof *p < nbytes

[...]

>         struct midi_hdr *midihdr;
> 
> Error --->
>         if ((midihdr = (struct midi_hdr *) kmalloc(sizeof(struct midi_hdr *), GF
> P_KERNEL)) == NULL) {

This sort of thing is why the comp.lang.c approved way to call
malloc() is
	foo *x = malloc (sizeof *x);
No cast is required and the sizeof usage resembles the
declaration.  The following is what I say on comp.lang.c when
someone does it another way.  AFAICS the recommendations apply
equally to [kv]malloc().
----------------------------------------------------------------------
When calling malloc(), I recommend using the sizeof operator on the
object you are allocating, not on the type.  For instance, *don't*
write this:

	int *x = malloc (sizeof (int) * 128); /* Don't do this! */

Instead, write it this way:

	int *x = malloc (sizeof *x * 128);

There's a few reasons to do it this way:

	* If you ever change the type that `x' points to, it's not
          necessary to change the malloc() call as well.  

	  This is more of a problem in a large program, but it's still
	  convenient in a small one.

	* Taking the size of an object makes your sizeof call more
          similar to your declaration, which makes writing the
          statement less error-prone.  

	  For instance, above, the declaration syntax is `*x' and the
	  sizeof operation is also written `*x'.  This provides a
	  visual clue that the malloc() call is correct.

I don't recommend casting the return value of malloc():

	* The cast is not required in ANSI C.

	* Casting its return value can mask a failure to #include
          <stdlib.h>, which leads to undefined behavior.

	* If you cast to the wrong type by accident, odd failures can
	  result.
----------------------------------------------------------------------

-- 
Ben Pfaff <pfaffben@msu.edu> <pfaffben@debian.org> <blp@gnu.org>
MSU Student - Debian GNU/Linux Maintainer - GNU Developer
Personal webpage: http://www.msu.edu/user/pfaffben

      parent reply	other threads:[~2001-04-06  3:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-05 22:45 Dawson Engler
2001-04-05 23:13 ` André Dahlqvist
     [not found]   ` <3ACCFF07.5AF0A74A@resilience.com>
2001-04-05 23:27     ` Jeff Golds
2001-04-06  3:33 ` Ben Pfaff [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=87wv8yg0rz.fsf@pfaffben.user.msu.edu \
    --to=pfaffben@msu.edu \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®