From: Marek Vasut <marek.vasut@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] Prevent malloc with size 0
Date: Mon, 2 Apr 2012 04:51:24 +0200 [thread overview]
Message-ID: <201204020451.24592.marek.vasut@gmail.com> (raw)
In-Reply-To: <CALButC+xSAy23ZfeoGygjB1Nab0wVRH-H7VeJnrTwGFrQawENQ@mail.gmail.com>
Dear Graeme Russ,
> Hi Marek,
>
> On Mon, Apr 2, 2012 at 11:04 AM, Marek Vasut <marek.vasut@gmail.com> wrote:
> > Dear Graeme Russ,
> >
> >> Hi Marek,
> >>
> >> On Mon, Apr 2, 2012 at 10:13 AM, Marek Vasut <marek.vasut@gmail.com> wrote:
> >> > Dear Graeme Russ,
> >> >
> >> >> Hi Marek,
> >> >>
> >> >> On Mon, Apr 2, 2012 at 9:45 AM, Marek Vasut <marek.vasut@gmail.com>
wrote:
> >> >> > Dear Graeme Russ,
> >> >>
> >> >> Because you just set it off - Right now, that code is assuming
> >> >> malloc(0) will return a valid pointer and thus not throw an E_NOMEM
> >> >> error - Now all that code will fail with E_NOMEM
> >> >
> >> > Well ... that code worked with invalid memory (most probably not even
> >> > R/W because it was some completely random hunk) and worked only by
> >> > sheer coincidence. Let's break it, it was broken anyway.
> >>
> >> a) The code calling malloc(0) is not broken, U-Boot's implementation of
> >> malloc(0) is.
> >
> > Well if it corrupts the internal structures of the mallocator, it's
> > broken because it works by sheer coincidence. But I know what you wanna
> > point out.
>
> If I call printf() with incorrect format specifiers and arguments (and the
> compile does not pick it up) then my code is broken. If I call printf() and
> the systems implementation does not support a standard format specifier
> that I'm using then printf() is broken, not my code.
>
> malloc(0) is a permissible call - It's unfortunate that the behaviour is
> unspecified:
>
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
> ISO/IEC 9899:TC2 - May 6, 2005
>
> 7.20.3.3 The malloc function (p.314)
> The malloc function allocates space for an object whose size is specified
> by size and whose value is indeterminate.
>
> Returns
> The malloc function returns either a null pointer or a pointer to the
> allocated space.
>
> J.1 Unspecified behavior (p. 490)
> - The amount of storage allocated by a successful call to the calloc,
> malloc, or realloc function when 0 bytes was requested (7.20.3)
>
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
> ISO/IEC 9899:201x - April 12, 2011
>
> 7.22.3.4 The malloc function (p. 349)
> Synopsis
> #include <stdlib.h>
> void *malloc(size_t size);
> Description
> The malloc function allocates space for an object whose size is specified
> by size and whose value is indeterminate.
> Returns
> The malloc function returns either a null pointer or a pointer to the
> allocated space.
>
> J.1 Unspecified behavior (p. 556)
> The amount of storage allocated by a successful call to the calloc,
> malloc, or realloc function when 0 bytes was requested (7.22.3).
>
> I have also seen forum postings of the form:
> Section 7.20.3
> If the size of the space requested is zero, the behavior is
> implementation defined: either a null pointer is returned, or the
> behavior is as if the size were some nonzero value, except that the
> returned pointer shall not be used to access an object.
>
> but have not seen an official document stating this
>
> >> b) The code calling malloc(0) is making a perfectly legitimate
> >> assumption based on how glibc handles malloc(0)
> >
> > Yes, agreed
> >
> >> c) Just because glibc does something does not mean we have to
> >
> > ACK
> >
> >> d) malloc(0) returning NULL and malloc(0) returning a valid pointer is
> >> not going to trouble me as I will never call malloc(0)
> >
> > You sure? :)
>
> No ;)
>
> > Anyway, if we return something else than 0, how are we gonna trap such a
> > null pointer?
>
> You don't - You ask for a pointer to a block of memory of zero size and
> malloc will return the smallest block it can. Remember, malloc(x) does not
> have to return a block of exactly x bytes - it must return a block of at
> least x bytes. It is up to you not to deference the pointer passed back
> from malloc(0)
>
> >> > Do you know about any such code? That's why I suggest adding such a
> >> > debug() only in case there's malloc(0) called. Maybe even add a
> >> > printf() instead.
> >>
> >> Did you see the FDT example - Admitedly not in U-Boot but it's a really
> >> good example IMHO - For the sake of code simplisity and clarity, some
> >> processing loops are best implemented assuming malloc(0) will return
> >> a valid pointer. Now if that pointer is de-referenced, then that is
> >> the callers problem...
> >
> > I did not see it, where?
>
> patchwork (http://patchwork.ozlabs.org/patch/71914/)
>
> Bottom line is, we could do either and we would be 100% compliant with the
> C standard
>
> The question is, what would be more onerous. Since the majority of U-Boot
> developers will be more familiar with the glibc implementation, we may
> one day end up with code that blindly assumes malloc(0) returns a valid
> pointer and not NULL so to me, returning a valid pointer would be a logical
> choice.
>
> On the converse, returning NULL from malloc(0) means that any attempt to
> (illegally) deference it will be immediately obvious...
So it's a question of being fool-proof vs. being compatible with glibc. This is
a tough one, so what about voting ? ;-)
> Regards,
>
> Graeme
next prev parent reply other threads:[~2012-04-02 2:51 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-21 9:24 [U-Boot] [PATCH] Prevent malloc with size 0 Kostaras Nikolaos
2010-10-21 11:25 ` Joakim Tjernlund
2010-10-21 11:32 ` Wolfgang Denk
2010-10-21 11:45 ` Joakim Tjernlund
2010-10-21 11:51 ` Wolfgang Denk
2010-10-21 11:56 ` Joakim Tjernlund
2010-10-21 12:02 ` Wolfgang Denk
2010-10-21 12:54 ` Joakim Tjernlund
2010-10-21 19:51 ` Mike Frysinger
2010-10-21 21:10 ` Graeme Russ
2010-10-21 21:27 ` Mike Frysinger
2012-03-31 19:59 ` Marek Vasut
2012-04-01 12:25 ` Joakim Tjernlund
2012-04-01 14:01 ` Marek Vasut
2012-04-01 14:15 ` Joakim Tjernlund
2012-04-01 14:21 ` Marek Vasut
2012-04-01 22:40 ` Graeme Russ
2012-04-01 23:45 ` Marek Vasut
2012-04-01 23:52 ` Graeme Russ
2012-04-02 0:13 ` Marek Vasut
2012-04-02 0:25 ` Graeme Russ
2012-04-02 1:04 ` Marek Vasut
2012-04-02 1:40 ` Graeme Russ
2012-04-02 2:51 ` Marek Vasut [this message]
2012-04-02 3:05 ` Graeme Russ
2012-04-02 6:39 ` Joakim Tjernlund
2012-04-02 3:12 ` Mike Frysinger
2012-04-02 3:16 ` Graeme Russ
2012-04-02 3:36 ` Marek Vasut
2012-04-02 3:43 ` Graeme Russ
2012-04-02 4:23 ` Marek Vasut
2012-04-02 4:27 ` Graeme Russ
2012-04-02 6:55 ` Joakim Tjernlund
2012-04-02 7:17 ` Graeme Russ
2012-04-02 7:40 ` Joakim Tjernlund
2012-04-02 14:05 ` Marek Vasut
2012-04-02 14:26 ` Joakim Tjernlund
2012-04-02 14:42 ` Marek Vasut
2012-04-02 15:08 ` Joakim Tjernlund
2012-04-02 15:23 ` Marek Vasut
2012-04-02 16:00 ` Joakim Tjernlund
2012-04-02 16:39 ` Marek Vasut
2012-04-02 17:22 ` Joakim Tjernlund
2012-04-02 18:00 ` Marek Vasut
2012-04-02 18:40 ` Joakim Tjernlund
2012-04-02 19:14 ` Mike Frysinger
2012-04-02 21:02 ` Joakim Tjernlund
2012-04-02 19:23 ` Marek Vasut
2012-04-02 20:28 ` Graeme Russ
2012-04-02 20:56 ` Joakim Tjernlund
2012-04-02 20:59 ` Graeme Russ
2012-04-02 21:14 ` Joakim Tjernlund
2012-04-02 23:35 ` Graeme Russ
2012-04-03 10:35 ` Graeme Russ
2012-10-16 6:31 ` Marek Vasut
2012-10-16 9:22 ` Joakim Tjernlund
2012-10-16 10:43 ` Marek Vasut
2012-10-16 11:46 ` Joakim Tjernlund
2012-10-16 10:43 ` Wolfgang Denk
2012-10-16 22:41 ` Graeme Russ
2012-04-02 3:10 ` Mike Frysinger
2012-04-02 3:36 ` Marek Vasut
2010-10-22 6:10 ` Joakim Tjernlund
2010-10-22 7:18 ` Reinhard Meyer
2010-10-22 7:47 ` Joakim Tjernlund
2010-10-22 7:20 ` Mike Frysinger
2010-10-22 7:37 ` Joakim Tjernlund
2010-10-22 7:55 ` Mike Frysinger
2010-10-22 8:34 ` Joakim Tjernlund
2010-10-22 15:18 ` Mike Frysinger
2010-10-22 16:40 ` Joakim Tjernlund
2010-10-22 17:06 ` Mike Frysinger
2010-10-23 9:14 ` Joakim Tjernlund
2010-10-22 17:36 ` Scott Wood
2010-10-23 9:23 ` Joakim Tjernlund
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=201204020451.24592.marek.vasut@gmail.com \
--to=marek.vasut@gmail.com \
--cc=u-boot@lists.denx.de \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.