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 17:23:03 +0200 [thread overview]
Message-ID: <201204021723.03688.marek.vasut@gmail.com> (raw)
In-Reply-To: <OFD76CA56B.13A26996-ONC12579D4.00525DB1-C12579D4.005331BE@transmode.se>
Dear Joakim Tjernlund,
> Marek Vasut <marek.vasut@gmail.com> wrote on 2012/04/02 16:42:30:
> > Dear Joakim Tjernlund,
> >
> > > Marek Vasut <marek.vasut@gmail.com> wrote on 2012/04/02 16:05:13:
> > > > Dear Joakim Tjernlund,
> > > >
> > > > > Hi Grame
> > > > >
> > > > > Graeme Russ <graeme.russ@gmail.com> wrote on 2012/04/02 09:17:44:
> > > > > > Hi Joakim,
> > > > > >
> > > > > > On Apr 2, 2012 4:55 PM, "Joakim Tjernlund"
> > > > > > <joakim.tjernlund@transmode.se>
> > > >
> > > > wrote:
> > > > > > > > Hi Marek,
> > > > > > > >
> > > > > > > > On Mon, Apr 2, 2012 at 1:36 PM, Marek Vasut
> > > > > > > > <marek.vasut@gmail.com>
> > > >
> > > > wrote:
> > > > > > > > > Dear Mike Frysinger,
> > > > > > > > >
> > > > > > > > >> On Sunday 01 April 2012 20:25:44 Graeme Russ wrote:
> > > > > > > > >> > b) The code calling malloc(0) is making a perfectly
> > > > > > > > >> > legitimate assumption
> > > > > > > > >> >
> > > > > > > > >> > based on how glibc handles malloc(0)
> > > > > > > > >>
> > > > > > > > >> not really. POSIX says malloc(0) is implementation
> > > > > > > > >> defined (so it may return a unique address, or it may
> > > > > > > > >> return NULL). no userspace code assuming malloc(0) will
> > > > > > > > >> return non-NULL is correct.
> > > > > > > > >
> > > > > > > > > Which is your implementation-defined ;-) But I have to
> > > > > > > > > agree with this one. So my vote is for returning NULL.
> > > > > > > >
> > > > > > > > Also, no userspace code assuming malloc(0) will return NULL
> > > > > > > > is correct
> > > > > > > >
> > > > > > > > Point being, no matter which implementation is chosen, it is
> > > > > > > > up to the caller to not assume that the choice that was made
> > > > > > > > was, in fact, the choice that was made.
> > > > > > > >
> > > > > > > > I.e. the behaviour of malloc(0) should be able to be changed
> > > > > > > > on a whim with no side-effects
> > > > > > > >
> > > > > > > > So I think I should change my vote to returning NULL for one
> > > > > > > > reason and one reason only - It is faster during run-time
> > > > > > >
> > > > > > > Then u-boot will be incompatible with both glibc and the linux
> > > > > > > kernel, it seems
> > > > > >
> > > > > > Forget aboug other implementations...
> > > > > > What matters is that the fact that the behaviour is undefined and
> > > > > > it is up to the caller to take that into account
> > > > >
> > > > > Well, u-boot borrows code from both kernel and user space so it
> > > > > would make sense if malloc(0) behaved the same. Especially for
> > > > > kernel code which tend to depend on the kernels impl.(just look at
> > > > > Scotts example)
> > > > >
> > > > > > > to me that any modern impl. of malloc(0) will return a non NULL
> > > > > > > ptr.
> > > > > > >
> > > > > > > It does need to be slower, just return ~0 instead, the kernel
> > > > > > > does something similar: if (!size)
> > > > > > >
> > > > > > > return ZERO_SIZE_PTR;
> > > > > >
> > > > > > That could work, but technically I don't think it complies as it
> > > > > > is not a pointer to allocated memory...
> > > > >
> > > > > It doesn't not have to be allocated memory, just a ptr != NULL
> > > > > which you can do free() on.
> > > >
> > > > But kernel has something mapped there to trap these pointers I
> > > > believe.
> > >
> > > So? That only means that the kernel has extra protection if someone
> > > tries to deference such a ptr. You are not required to do that(nice to
> > > have though) You don have any protection for deferencing NULL either I
> > > think?
> >
> > Can't GCC track it?
>
> Track what? NULL ptrs? I don't think so. Possibly when you have a static
> NULL ptr but not in the general case.
Well of course.
> I am getting tired of this discussion now. I am just trying to tell you
> that no sane impl. of malloc() these days return NULL for malloc(0).
And I got your point. Though for u-boot, this would be the best solution
actually. Anyone who uses memory allocated by malloc(0) is insane.
> Even
> though standards allow it they don't consider malloc(0) an error, glibc
> will not update errno in this case.
There's no errno in uboot I'm aware of ;-)
> Jocke
next prev parent reply other threads:[~2012-04-02 15:23 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
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 [this message]
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=201204021723.03688.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox