All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] oslib-posix: change free to g_free
@ 2014-10-11  2:54 arei.gonglei
  2014-10-11  3:10 ` zhanghailiang
  2014-10-11  3:23 ` Eric Blake
  0 siblings, 2 replies; 9+ messages in thread
From: arei.gonglei @ 2014-10-11  2:54 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, weidong.huang, qemu-trivial, Gonglei, stefanha, pbonzini

From: Gonglei <arei.gonglei@huawei.com>

The caller of qemu_vfree() maybe not check whether parameter
ptr pointer is NULL or not, such as vpc_open().
Using g_free() is more safe.

Signed-off-by: Gonglei <arei.gonglei@huawei.com>
---
 util/oslib-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 016a047..ca435d0 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -153,7 +153,7 @@ void *qemu_anon_ram_alloc(size_t size)
 void qemu_vfree(void *ptr)
 {
     trace_qemu_vfree(ptr);
-    free(ptr);
+    g_free(ptr);
 }
 
 void qemu_anon_ram_free(void *ptr, size_t size)
-- 
1.7.12.4

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] oslib-posix: change free to g_free
  2014-10-11  2:54 [Qemu-devel] [PATCH] oslib-posix: change free to g_free arei.gonglei
@ 2014-10-11  3:10 ` zhanghailiang
  2014-10-11  3:21   ` Gonglei
  2014-10-11  3:23 ` Eric Blake
  1 sibling, 1 reply; 9+ messages in thread
From: zhanghailiang @ 2014-10-11  3:10 UTC (permalink / raw)
  To: arei.gonglei, qemu-devel
  Cc: kwolf, qemu-trivial, weidong.huang, stefanha, pbonzini

On 2014/10/11 10:54, arei.gonglei@huawei.com wrote:
> From: Gonglei <arei.gonglei@huawei.com>
>
> The caller of qemu_vfree() maybe not check whether parameter
> ptr pointer is NULL or not, such as vpc_open().
> Using g_free() is more safe.
>

It seems that free(NULL) is harmless.

 From section 7.20.3.2/2 of the C99 standard:
The free function causes the space pointed to by ptr to be deallocated, that is,
made available for further allocation. If ptr is a null pointer, no action occurs.

> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> ---
>   util/oslib-posix.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 016a047..ca435d0 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -153,7 +153,7 @@ void *qemu_anon_ram_alloc(size_t size)
>   void qemu_vfree(void *ptr)
>   {
>       trace_qemu_vfree(ptr);
> -    free(ptr);
> +    g_free(ptr);
>   }
>
>   void qemu_anon_ram_free(void *ptr, size_t size)
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] oslib-posix: change free to g_free
  2014-10-11  3:10 ` zhanghailiang
@ 2014-10-11  3:21   ` Gonglei
  2014-10-11  3:26     ` Eric Blake
  0 siblings, 1 reply; 9+ messages in thread
From: Gonglei @ 2014-10-11  3:21 UTC (permalink / raw)
  To: Zhanghailiang
  Cc: kwolf, Huangweidong (C), qemu-trivial, qemu-devel, stefanha, pbonzini

On 2014/10/11 11:10, Zhanghailiang wrote:

> On 2014/10/11 10:54, arei.gonglei@huawei.com wrote:
>> From: Gonglei <arei.gonglei@huawei.com>
>>
>> The caller of qemu_vfree() maybe not check whether parameter
>> ptr pointer is NULL or not, such as vpc_open().
>> Using g_free() is more safe.
>>
> 
> It seems that free(NULL) is harmless.
> 

Actually, I had noted that C standard says it is a no-operation.
But that doesn't mean that every C-library handles it like that.
Some people saw crashes for free(NULL), so it's best to avoid
calling the free in the first place (caller) or using  g_free() in qemu_vfree().

Best regards,
-Gonglei

>  From section 7.20.3.2/2 of the C99 standard:
> The free function causes the space pointed to by ptr to be deallocated, that is,
> made available for further allocation. If ptr is a null pointer, no action occurs.
> 
>> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
>> ---
>>   util/oslib-posix.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
>> index 016a047..ca435d0 100644
>> --- a/util/oslib-posix.c
>> +++ b/util/oslib-posix.c
>> @@ -153,7 +153,7 @@ void *qemu_anon_ram_alloc(size_t size)
>>   void qemu_vfree(void *ptr)
>>   {
>>       trace_qemu_vfree(ptr);
>> -    free(ptr);
>> +    g_free(ptr);
>>   }
>>
>>   void qemu_anon_ram_free(void *ptr, size_t size)
>>
> 
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] oslib-posix: change free to g_free
  2014-10-11  2:54 [Qemu-devel] [PATCH] oslib-posix: change free to g_free arei.gonglei
  2014-10-11  3:10 ` zhanghailiang
@ 2014-10-11  3:23 ` Eric Blake
  2014-10-12  7:44   ` Kevin Wolf
  1 sibling, 1 reply; 9+ messages in thread
From: Eric Blake @ 2014-10-11  3:23 UTC (permalink / raw)
  To: arei.gonglei, qemu-devel
  Cc: kwolf, qemu-trivial, weidong.huang, stefanha, pbonzini

[-- Attachment #1: Type: text/plain, Size: 652 bytes --]

On 10/10/2014 08:54 PM, arei.gonglei@huawei.com wrote:
> From: Gonglei <arei.gonglei@huawei.com>
> 
> The caller of qemu_vfree() maybe not check whether parameter
> ptr pointer is NULL or not, such as vpc_open().
> Using g_free() is more safe.

NACK.  g_free is only safe for pointers allocated by g_malloc.
qemu_vfree is for use on pointers allocated by qemu_try_memalign and
friends (matching the name valloc which is an older spelling of
posix_memalign), which are NOT allocated by g_malloc.  Furthermore,
free(NULL) is safe.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] oslib-posix: change free to g_free
  2014-10-11  3:21   ` Gonglei
@ 2014-10-11  3:26     ` Eric Blake
  2014-10-11  3:32       ` Gonglei
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Blake @ 2014-10-11  3:26 UTC (permalink / raw)
  To: Gonglei, Zhanghailiang
  Cc: kwolf, Huangweidong (C), qemu-trivial, qemu-devel, stefanha, pbonzini

[-- Attachment #1: Type: text/plain, Size: 794 bytes --]

On 10/10/2014 09:21 PM, Gonglei wrote:

> 
> Actually, I had noted that C standard says it is a no-operation.
> But that doesn't mean that every C-library handles it like that.

EVERY libc that is C89 compliant handles it like that.  The last
platform that failed on free(NULL) was SunOS 4, which is such
museum-ware it's not funny.  There is no need to cater to platforms from
25 years ago.

> Some people saw crashes for free(NULL), so it's best to avoid
> calling the free in the first place (caller) or using  g_free() in qemu_vfree().

Absolutely not.  g_free is unsafe to use except for pointers from
g_malloc, which is NOT the case that qemu_vfree is used on.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] oslib-posix: change free to g_free
  2014-10-11  3:26     ` Eric Blake
@ 2014-10-11  3:32       ` Gonglei
  2014-10-11  3:44         ` Eric Blake
  0 siblings, 1 reply; 9+ messages in thread
From: Gonglei @ 2014-10-11  3:32 UTC (permalink / raw)
  To: Eric Blake
  Cc: kwolf, Huangweidong (C),
	Zhanghailiang, qemu-trivial, qemu-devel, stefanha, pbonzini

On 2014/10/11 11:26, Eric Blake wrote:

> On 10/10/2014 09:21 PM, Gonglei wrote:
> 
>>
>> Actually, I had noted that C standard says it is a no-operation.
>> But that doesn't mean that every C-library handles it like that.
> 
> EVERY libc that is C89 compliant handles it like that.  The last
> platform that failed on free(NULL) was SunOS 4, which is such
> museum-ware it's not funny.  There is no need to cater to platforms from
> 25 years ago.
> 

OK.Thanks for explanation!

>> Some people saw crashes for free(NULL), so it's best to avoid
>> calling the free in the first place (caller) or using  g_free() in qemu_vfree().
> 
> Absolutely not.  g_free is unsafe to use except for pointers from
> g_malloc, which is NOT the case that qemu_vfree is used on.
> 

Got it, thanks :)

But why some callers make a check,
but some other callers don't do this check?
Can I consider those check is superfluous?

-Best regards,
-Gonglei

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] oslib-posix: change free to g_free
  2014-10-11  3:32       ` Gonglei
@ 2014-10-11  3:44         ` Eric Blake
  2014-10-11  3:47           ` Gonglei
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Blake @ 2014-10-11  3:44 UTC (permalink / raw)
  To: Gonglei
  Cc: kwolf, Huangweidong (C),
	Zhanghailiang, qemu-trivial, qemu-devel, stefanha, pbonzini

[-- Attachment #1: Type: text/plain, Size: 864 bytes --]

On 10/10/2014 09:32 PM, Gonglei wrote:

>>> Actually, I had noted that C standard says it is a no-operation.
>>> But that doesn't mean that every C-library handles it like that.
>>
>> EVERY libc that is C89 compliant handles it like that.  The last
>> platform that failed on free(NULL) was SunOS 4, which is such
>> museum-ware it's not funny.  There is no need to cater to platforms from
>> 25 years ago.

> 
> But why some callers make a check,
> but some other callers don't do this check?

Because some people haven't learned that free(NULL) is safe yet.  You're
welcome to simplify code as you touch it.

> Can I consider those check is superfluous?

Yes.  Checking for NULL before calling free() or g_free() is wasted effort.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] oslib-posix: change free to g_free
  2014-10-11  3:44         ` Eric Blake
@ 2014-10-11  3:47           ` Gonglei
  0 siblings, 0 replies; 9+ messages in thread
From: Gonglei @ 2014-10-11  3:47 UTC (permalink / raw)
  To: Eric Blake
  Cc: kwolf, Huangweidong (C),
	Zhanghailiang, qemu-trivial, qemu-devel, stefanha, pbonzini

On 2014/10/11 11:44, Eric Blake wrote:

> On 10/10/2014 09:32 PM, Gonglei wrote:
> 
>>>> Actually, I had noted that C standard says it is a no-operation.
>>>> But that doesn't mean that every C-library handles it like that.
>>>
>>> EVERY libc that is C89 compliant handles it like that.  The last
>>> platform that failed on free(NULL) was SunOS 4, which is such
>>> museum-ware it's not funny.  There is no need to cater to platforms from
>>> 25 years ago.
> 
>>
>> But why some callers make a check,
>> but some other callers don't do this check?
> 
> Because some people haven't learned that free(NULL) is safe yet.  You're
> welcome to simplify code as you touch it.
> 

OK, I will. Thanks again :)

>> Can I consider those check is superfluous?
> 
> Yes.  Checking for NULL before calling free() or g_free() is wasted effort.
> 


Best regards,
-Gonglei

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] oslib-posix: change free to g_free
  2014-10-11  3:23 ` Eric Blake
@ 2014-10-12  7:44   ` Kevin Wolf
  0 siblings, 0 replies; 9+ messages in thread
From: Kevin Wolf @ 2014-10-12  7:44 UTC (permalink / raw)
  To: Eric Blake
  Cc: weidong.huang, qemu-trivial, qemu-devel, arei.gonglei, stefanha,
	pbonzini

Am 11.10.2014 um 05:23 hat Eric Blake geschrieben:
> On 10/10/2014 08:54 PM, arei.gonglei@huawei.com wrote:
> > From: Gonglei <arei.gonglei@huawei.com>
> > 
> > The caller of qemu_vfree() maybe not check whether parameter
> > ptr pointer is NULL or not, such as vpc_open().
> > Using g_free() is more safe.
> 
> NACK.  g_free is only safe for pointers allocated by g_malloc.
> qemu_vfree is for use on pointers allocated by qemu_try_memalign and
> friends (matching the name valloc which is an older spelling of
> posix_memalign), which are NOT allocated by g_malloc.  Furthermore,
> free(NULL) is safe.

I second that. Strong NACK.

Kevin

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2014-10-12  7:44 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-11  2:54 [Qemu-devel] [PATCH] oslib-posix: change free to g_free arei.gonglei
2014-10-11  3:10 ` zhanghailiang
2014-10-11  3:21   ` Gonglei
2014-10-11  3:26     ` Eric Blake
2014-10-11  3:32       ` Gonglei
2014-10-11  3:44         ` Eric Blake
2014-10-11  3:47           ` Gonglei
2014-10-11  3:23 ` Eric Blake
2014-10-12  7:44   ` Kevin Wolf

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.