From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NFS1n-0001N8-FK for qemu-devel@nongnu.org; Tue, 01 Dec 2009 07:40:23 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NFS1i-0001I5-L8 for qemu-devel@nongnu.org; Tue, 01 Dec 2009 07:40:22 -0500 Received: from [199.232.76.173] (port=52893 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NFS1i-0001Hc-Aq for qemu-devel@nongnu.org; Tue, 01 Dec 2009 07:40:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56420) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NFS1h-00044a-VZ for qemu-devel@nongnu.org; Tue, 01 Dec 2009 07:40:18 -0500 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id nB1CeGRN032235 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 1 Dec 2009 07:40:16 -0500 Message-ID: <4B150EAD.4050502@redhat.com> Date: Tue, 01 Dec 2009 13:40:13 +0100 From: Gerd Hoffmann MIME-Version: 1.0 Subject: Re: [Qemu-devel] [PATCH] Permit zero-sized qemu_malloc() & friends References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster Cc: qemu-devel@nongnu.org > 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. Even more advanced: Make zero_length_malloc page-sized and page-aligned, then munmap int, so dereferencing it actually traps. > 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)); cheers, Gerd