All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eduardo Habkost <ehabkost@redhat.com>
To: malc <av1474@comtv.ru>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] fix qemu_malloc() error check for size==0
Date: Tue, 19 May 2009 10:52:16 -0300	[thread overview]
Message-ID: <20090519135216.GA4254@blackpad> (raw)
In-Reply-To: <Pine.LNX.4.64.0905190410290.3632@linmac.oyster.ru>

On Tue, May 19, 2009 at 04:17:55AM +0400, malc wrote:
> On Mon, 18 May 2009, Eduardo Habkost wrote:
> 
> > On Tue, May 19, 2009 at 01:56:55AM +0400, malc wrote:
> > > On Mon, 18 May 2009, Eduardo Habkost wrote:
> > > 
> > > > This patch is similar to a previous qemu_realloc() fix
> > > > (commit 322691a5c9f1c8531554148d47c078b5be590805), but for qemu_malloc().
> > > > 
> > > > malloc(0) may correctly return NULL if size==0. We don't want to abort qemu on
> > > > this case.
> > > 
> > > Only it wouldn't (on Linux):
> > > 
> > > $ cat malloc.c
> > > #include <stdlib.h>
> > > 
> > > int main (void)
> > > {
> > >     printf ("%p\n", malloc (0));
> > >     return 0;
> > > }
> > > $ gcc malloc.c
> > > $ ./a.out 
> > > 0x10011008
> > > 
> > > Standard (in 7.20.3) says that malloc's behaviour in case of size being
> > > zero is implementation defined.
> > > 
> > > Try `git show 63c75dcd669d011f438421980b4379827da4bb1c'.
> > > 
> > > The best(only?) thing to do is to check size passed to qemu_malloc[z]
> > > and abort the program if this situation is encountered.
> > 
> > Why? malloc(0) is as valid as realloc(p, 0). It will either return NULL
> > or a pointer, and on any case the value can be safely passed to free()
> > later.
> 
> I believe you haven't examined the commit that i referenced. Thing is
> existing code used to, i'd venture a guess accidentaly, rely on the
> behaviour that current GLIBC provides and consequently failed to
> operate on AIX where malloc(0) returns NULL, IOW making qemu_malloc[z]
> return whatever the underlying system returns is just hiding the bugs,
> the code becomes unportable.

The assumption that malloc(0) will return anything (either NULL or
not-NULL) is not portable. That's exactly the point of my patch: not
making any assumption about the returned value when size==0.

But calling malloc(0) is perfectly valid, as long as you call free() on
the returned value later. I don't see any reason to make the
qemu_malloc() behavior from the standard malloc() behavior. The sequence
"p=malloc(0);free(p)" is valid and works. Why would we prevent
"p=qemu_malloc(0);qemu_free(p)" from working?

Yes, we may have broken code that assumes that qemu_malloc(0) is not
NULL, and _that_ code is broken and must be fixed. But why would we
break the cases where qemu_malloc(0) is called and handled correctly?

-- 
Eduardo

  parent reply	other threads:[~2009-05-19 13:52 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-18 20:31 [Qemu-devel] [PATCH] fix qemu_malloc() error check for size==0 Eduardo Habkost
2009-05-18 21:56 ` malc
2009-05-18 22:17   ` Eduardo Habkost
2009-05-19  0:17     ` malc
2009-05-19  6:44       ` Markus Armbruster
2009-05-19 13:00         ` malc
2009-05-19 13:37           ` Markus Armbruster
2009-05-19 14:06             ` malc
2009-05-19 14:28               ` Eduardo Habkost
2009-05-19 14:48                 ` malc
2009-05-19 14:56                   ` Eduardo Habkost
2009-05-19 15:23                     ` malc
2009-05-19 15:43                       ` Eduardo Habkost
2009-05-19 20:32                         ` Jamie Lokier
2009-05-19 22:12                           ` Eduardo Habkost
2009-05-19 22:49                             ` Jamie Lokier
2009-05-20  3:28                               ` Eduardo Habkost
2009-05-19 20:31                     ` Jamie Lokier
2009-05-19 16:09               ` Markus Armbruster
2009-05-19 14:02           ` Eduardo Habkost
2009-05-19 14:37             ` malc
2009-05-19 14:44               ` Eduardo Habkost
2009-05-19 14:55                 ` malc
2009-05-19 16:44                   ` [PATCH] Make qemu_alloc()/qemu_realloc() return NULL for size==0 (was Re: [Qemu-devel] [PATCH] fix qemu_malloc() error check for size==0) Eduardo Habkost
2009-05-19 18:40                     ` malc
2009-05-19 19:38                       ` Eduardo Habkost
2009-05-19 20:34                       ` Jamie Lokier
2009-05-20  8:00                       ` Kevin Wolf
2009-05-20  9:30                       ` [Qemu-devel] Re: [PATCH] Make qemu_alloc()/qemu_realloc() return NULL for size==0 Markus Armbruster
2009-05-20 18:20                         ` malc
2009-05-19 20:37                   ` [Qemu-devel] [PATCH] fix qemu_malloc() error check " Jamie Lokier
2009-05-19 13:52       ` Eduardo Habkost [this message]
2009-05-19 14:39         ` malc

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=20090519135216.GA4254@blackpad \
    --to=ehabkost@redhat.com \
    --cc=av1474@comtv.ru \
    --cc=qemu-devel@nongnu.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 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.