qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] Permit zero-sized qemu_malloc() & friends


From: Markus Armbruster
Subject: Re: [Qemu-devel] [PATCH] Permit zero-sized qemu_malloc() & friends
Date: Tue, 01 Dec 2009 14:11:49 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Gerd Hoffmann <address@hidden> 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));




reply via email to

[Prev in Thread] Current Thread [Next in Thread]