mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Roland Dreier <rdreier@cisco.com>
To: Amit Choudhary <amit2030@yahoo.com>
Cc: Jan Engelhardt <jengelh@linux01.gwdg.de>,
	Pekka Enberg <penberg@cs.helsinki.fi>,
	linux-kernel@vger.kernel.org
Subject: Re: Hopefully, kmalloc() will always succeed, but if it doesn't then....
Date: Sun, 22 Oct 2006 17:25:30 -0700	[thread overview]
Message-ID: <adalkn7j2th.fsf@cisco.com> (raw)
In-Reply-To: <20061022230957.78480.qmail@web55615.mail.re4.yahoo.com> (Amit Choudhary's message of "Sun, 22 Oct 2006 16:09:56 -0700 (PDT)")

 >         struct mixart_enum_connector_resp *connector;
 >         struct mixart_audio_info_req  *audio_info_req;
 >         struct mixart_audio_info_resp *audio_info;
 > 
 >         connector = kmalloc(sizeof(*connector), GFP_KERNEL);
 >         audio_info_req = kmalloc(sizeof(*audio_info_req), GFP_KERNEL);
 >         audio_info = kmalloc(sizeof(*audio_info), GFP_KERNEL);
 >         if (! connector || ! audio_info_req || ! audio_info) {
 >                 err = -ENOMEM;
 >                 goto __error;
 >         }

This is not a bug.  All of the pointers are initialized, and if
kmalloc() fails, then one of them will be set to NULL.  However,
kfree(NULL) is a perfectly fine thing to do (kfree just returns
immediately in this case).

So this is just a way of saving some tests and optimizing for the
common case when all allocations succeed.  In other words, this is
good code -- although the spacing is slightly bogus: it should be

        if (!connector || !audio_info_req || !audio_info) {

and also using __error as a label is slightly silly -- why not just
make it "error"?

 - R.

  reply	other threads:[~2006-10-23  0:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-22 19:58 Amit Choudhary
2006-10-22 20:22 ` Pekka Enberg
2006-10-22 21:09   ` Jan Engelhardt
2006-10-22 23:09     ` Amit Choudhary
2006-10-23  0:25       ` Roland Dreier [this message]
2006-10-23  4:00         ` Amit Choudhary
2006-10-22 22:51   ` Amit Choudhary

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=adalkn7j2th.fsf@cisco.com \
    --to=rdreier@cisco.com \
    --cc=amit2030@yahoo.com \
    --cc=jengelh@linux01.gwdg.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penberg@cs.helsinki.fi \
    /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®