VIR_FREE: Replace internals by g_clear_pointer

Our implementation masks GCC warnings of uninitialized use of the passed
argument. After changing this I got a load of following warnings:

src/conf/virnetworkportdef.c: In function 'virNetworkPortDefSaveStatus':
/usr/include/glib-2.0/glib/gmem.h:136:8: error: 'path' may be used uninitialized in this function [-Werror=maybe-uninitialized]
  136 |     if (_p)                \
      |        ^
src/conf/virnetworkportdef.c:447:11: note: 'path' was declared here
  447 |     char *path;
      |           ^~~~

For the curious, g_clear_pointer is still safe for arguments with
side-effect. Here's the pre-processed output of trying to do a
VIR_FREE(*(test2++)):

 do {
     typedef char _GStaticAssertCompileTimeAssertion_1[(sizeof *(&(*(test2++))) == sizeof (gpointer)) ? 1 : -1] __attribute__((__unused__));
     __typeof__((&(*(test2++)))) _pp = (&(*(test2++)));
     __typeof__(*(&(*(test2++)))) _ptr = *_pp;

     *_pp = ((void *)0);
     if (_ptr)
        (g_free) (_ptr);
 } while (0) ;

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2020-03-05 09:42:23 +01:00
parent 8a2390f640
commit 09352cca2a
3 changed files with 3 additions and 26 deletions

View File

@ -1584,7 +1584,6 @@ virDeleteElementsN;
virDispose;
virDisposeString;
virExpandN;
virFree;
virInsertElementsN;
virReallocN;
virResizeN;

View File

@ -178,7 +178,8 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
if (virReallocN(ptrptr, size, *countptr -= toremove) < 0)
abort();
} else {
virFree(ptrptr);
g_free(*((void **)ptrptr));
*((void **)ptrptr) = NULL;
*countptr = 0;
}
}
@ -333,24 +334,6 @@ int virAllocVar(void *ptrptr,
}
/**
* virFree:
* @ptrptr: pointer to pointer for address of memory to be freed
*
* Release the chunk of memory in the pointer pointed to by
* the 'ptrptr' variable. After release, 'ptrptr' will be
* updated to point to NULL.
*/
void virFree(void *ptrptr)
{
int save_errno = errno;
g_free(*(void**)ptrptr);
*(void**)ptrptr = NULL;
errno = save_errno;
}
/**
* virDispose:
* @ptrptr: pointer to pointer for address of memory to be sanitized and freed

View File

@ -55,7 +55,6 @@ int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t count)
G_GNUC_WARN_UNUSED_RESULT ATTRIBUTE_NONNULL(1);
void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
void virDispose(void *ptrptr, size_t count, size_t element_size, size_t *countptr)
ATTRIBUTE_NONNULL(1);
@ -417,11 +416,7 @@ void virDisposeString(char **strptr)
*
* This macro is safe to use on arguments with side effects.
*/
/* The ternary ensures that ptr is a non-const pointer and not an
* integer type, all while evaluating ptr only once. This gives us
* extra compiler safety when compiling under gcc.
*/
#define VIR_FREE(ptr) virFree(1 ? (void *) &(ptr) : (ptr))
#define VIR_FREE(ptr) g_clear_pointer(&(ptr), g_free)
/**