From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NHD15-0004MY-Dd for qemu-devel@nongnu.org; Sun, 06 Dec 2009 04:02:55 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NHD10-0004LY-Fx for qemu-devel@nongnu.org; Sun, 06 Dec 2009 04:02:54 -0500 Received: from [199.232.76.173] (port=46695 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NHD10-0004LV-9R for qemu-devel@nongnu.org; Sun, 06 Dec 2009 04:02:50 -0500 Received: from mail-yw0-f183.google.com ([209.85.211.183]:43744) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NHD0z-0006Yq-TK for qemu-devel@nongnu.org; Sun, 06 Dec 2009 04:02:50 -0500 Received: by ywh13 with SMTP id 13so3956368ywh.29 for ; Sun, 06 Dec 2009 01:02:49 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <4B193DA5.6040507@codemonkey.ws> From: Blue Swirl Date: Sun, 6 Dec 2009 11:02:28 +0200 Message-ID: Subject: Re: [Qemu-devel] [PATCH] Permit zero-sized qemu_malloc() & friends Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: malc Cc: Paul Brook , Markus Armbruster , qemu-devel@nongnu.org On Sun, Dec 6, 2009 at 10:39 AM, malc wrote: > On Sun, 6 Dec 2009, Markus Armbruster wrote: > >> malc writes: >> >> > On Sat, 5 Dec 2009, Markus Armbruster wrote: >> > >> >> Anthony Liguori writes: >> >> >> >> > Markus Armbruster wrote: >> >> >> Commit a7d27b53 made zero-sized allocations a fatal error, deviati= ng >> >> >> from ISO C's malloc() & friends. =C2=A0Revert that, but take care = never to >> >> >> return a null pointer, like malloc() & friends may do (it's >> >> >> implementation defined), because that's another source of bugs. >> >> >> >> >> >> Rationale: while zero-sized allocations might occasionally be a si= gn of >> >> >> something going wrong, they can also be perfectly legitimate. =C2= =A0The >> >> >> change broke such legitimate uses. =C2=A0We've found and "fixed" a= t least one >> >> >> of them already (commit eb0b64f7, also reverted by this patch), an= d >> >> >> another one just popped up: the change broke qcow2 images with vir= tual >> >> >> disk size zero, i.e. images that don't hold real data but only VM = state >> >> >> of snapshots. >> >> >> >> >> > > > [..snip..] > > >> > >> > P.S. It would be interesting to know how this code behaves under OpenB= SD, with >> > =C2=A0 =C2=A0 =C2=A0p =3D malloc (0); >> > >> > [1] As does, in essence, http://www.opengroup.org/onlinepubs/799098977= 5/xsh/read.html >> >> Replace "p =3D (void *)-1" by "p =3D NULL" and it works just fine. >> > > That's why i asked for somone to run it on OpenBSD: $ cat mall.c #define _GNU_SOURCE #include #include #include #include #include int main (void) { int fd =3D open ("/dev/zero", 0); int ret; #if 0 void *p =3D (void *) -1; #else void *p =3D malloc(0); #endif fprintf(stderr, "ptr %p\n", p); if (fd =3D=3D -1) err (1, "open"); ret =3D read (fd, p, 0); if (ret !=3D 0) err (1, "read"); return 0; } $ gcc mall.c $ ./a.out ptr 0x46974060 $ Changing read count to 1: $ ./a.out ptr 0x41ce0070 a.out: read: Bad address