]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
VIR_FREE: Replace internals by g_clear_pointer
authorPeter Krempa <pkrempa@redhat.com>
Thu, 5 Mar 2020 08:42:23 +0000 (09:42 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 5 Mar 2020 15:22:27 +0000 (16:22 +0100)
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>
src/libvirt_private.syms
src/util/viralloc.c
src/util/viralloc.h

index 07dee6d841d9ddd8edfd704947324b8ec4dda19e..511fb888726b810bfd97d4b97b1b30a742a8c8e4 100644 (file)
@@ -1584,7 +1584,6 @@ virDeleteElementsN;
 virDispose;
 virDisposeString;
 virExpandN;
-virFree;
 virInsertElementsN;
 virReallocN;
 virResizeN;
index b8ca850764fe150a3c2e9a3e2a8eb00c444e254e..e254416cdffa49aafded87e9d89d1a17c22dbfcc 100644 (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
index 1d42aeead1d5a3ca7789019158f2157cb9f33f6b..833f85f62ec3d80e5543812a630cfcff5f07578b 100644 (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)
 
 
 /**