mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
build: make VIR_FREE do some type checking
We can exploit the fact that gcc warns about int-to-pointer conversion in ternary cond?(void*):(int) in order to prevent future mistakes of calling VIR_FREE on a scalar lvalue. For example, between commits158ba873
and802e2df
, we would have had this warning: cc1: warnings being treated as errors remote.c: In function 'remoteDispatchListNetworks': remote.c:3684:70: error: pointer/integer type mismatch in conditional expression There are still a number of places that malloc into a const char*; while it would probably be worth scrubbing them to use char* instead, that is a separate patch, so we have to cast away const in VIR_FREE for now. * src/util/memory.h (VIR_FREE): Make gcc warn about integers. Iteratively developed from a patch by Christophe Fergeau.
This commit is contained in:
parent
99de59900a
commit
90d761eeb2
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* memory.c: safer memory allocation
|
* memory.c: safer memory allocation
|
||||||
*
|
*
|
||||||
* Copyright (C) 2010 Red Hat, Inc.
|
* Copyright (C) 2010-2011 Red Hat, Inc.
|
||||||
* Copyright (C) 2008 Daniel P. Berrange
|
* Copyright (C) 2008 Daniel P. Berrange
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
@ -197,7 +197,11 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
|
|||||||
* Free the memory stored in 'ptr' and update to point
|
* Free the memory stored in 'ptr' and update to point
|
||||||
* to NULL.
|
* to NULL.
|
||||||
*/
|
*/
|
||||||
# define VIR_FREE(ptr) virFree(&(ptr))
|
/* The ternary ensures that ptr is a pointer and not an integer type,
|
||||||
|
* while evaluating ptr only once. For now, we intentionally cast
|
||||||
|
* away const, since a number of callers safely pass const char *.
|
||||||
|
*/
|
||||||
|
# define VIR_FREE(ptr) virFree((void *) (1 ? (const void *) &(ptr) : (ptr)))
|
||||||
|
|
||||||
|
|
||||||
# if TEST_OOM
|
# if TEST_OOM
|
||||||
|
Loading…
Reference in New Issue
Block a user