Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NFSWN-0003ht-Ke for qemu-devel@nongnu.org; Tue, 01 Dec 2009 08:11:59 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NFSWJ-0003g4-Q9 for qemu-devel@nongnu.org; Tue, 01 Dec 2009 08:11:59 -0500 Received: from [199.232.76.173] (port=55723 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NFSWJ-0003fy-Kt for qemu-devel@nongnu.org; Tue, 01 Dec 2009 08:11:55 -0500 Received: from mx1.redhat.com ([209.132.183.28]:2511) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NFSWJ-0006Ou-95 for qemu-devel@nongnu.org; Tue, 01 Dec 2009 08:11:55 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id nB1DBpsV021597 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 1 Dec 2009 08:11:52 -0500 From: Markus Armbruster Subject: Re: [Qemu-devel] [PATCH] Permit zero-sized qemu_malloc() & friends References: <4B150EAD.4050502@redhat.com> Date: Tue, 01 Dec 2009 14:11:49 +0100 In-Reply-To: <4B150EAD.4050502@redhat.com> (Gerd Hoffmann's message of "Tue, 01 Dec 2009 13:40:13 +0100") Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Gerd Hoffmann Cc: qemu-devel@nongnu.org Gerd Hoffmann writes: >> diff --git a/qemu-malloc.c b/qemu-malloc.c >> index 295d185..aeeb78b 100644 >> --- a/qemu-malloc.c >> +++ b/qemu-malloc.c >> @@ -44,22 +44,12 @@ void qemu_free(void *ptr) >> >> void *qemu_malloc(size_t size) >> { >> - if (!size) { >> - abort(); >> - } >> - return oom_check(malloc(size)); >> + return oom_check(malloc(size ? size : 1)); >> } > > You might want to have a 'static uint8_t zero_length_malloc[0]' and > return that instead of the magic cookie '1'. Makes the code more > readable IMHO and you'll also have symbol in gdb when debugging qemu. Complicates qemu_realloc() and qemu_free() somewhat, and that makes me think we better do it as a separate commit. Agree? > Even more advanced: Make zero_length_malloc page-sized and > page-aligned, then munmap int, so dereferencing it actually traps. Overrunning a malloc'ed buffer very rarely traps, not sure catching this special case is worth the portability headaches. If you really want to catch overruns, you need special tools like valgrind or electric fence anyway. >> void *qemu_realloc(void *ptr, size_t size) >> { >> + return oom_check(realloc(ptr, size ? size : 1)); > > qemu_realloc(qemu_malloc(0), 42); > > should better work correctly ... > > Likewise qemu_free(qemu_malloc(0));