[glib: 2/6] garray: Change free/unref semantics under static analysis




commit 423bcab9f4e7456006099e65c9d5ea505b7e6d53
Author: Philip Withnall <pwithnall endlessos org>
Date:   Tue Jun 7 11:05:26 2022 +0100

    garray: Change free/unref semantics under static analysis
    
    Recent changes to `GPtrArray` and/or Coverity mean that Coverity is now
    assuming that `g_ptr_array_free (my_array, TRUE)` can leak memory. This
    is true in the case that `g_ptr_array_ref (my_array)` has been called
    elsewhere, but Coverity never actually verifies that.
    
    Very little (or no?) GLib code mixes `g_ptr_array_free()` with
    `g_ptr_array_{ref,unref}()`, so this isn’t a problem in practice.
    
    However, it has created a hundred or more false positives in Coverity
    (as pointer arrays are widely used within GLib and GIO), which is a
    complete pain.
    
    Before taking the dramatic step of ditching Coverity due to its
    atrocious false positive rate, let’s try changing the semantics of
    `g_ptr_array_free()` only when running under Coverity.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 glib/garray.c | 5 +++++
 1 file changed, 5 insertions(+)
---
diff --git a/glib/garray.c b/glib/garray.c
index 920a402584..f77ef19906 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1555,9 +1555,14 @@ g_ptr_array_free (GPtrArray *array,
 
   /* if others are holding a reference, preserve the wrapper but
    * do free/return the data
+   *
+   * Coverity doesn’t understand this and assumes it’s a leak, so comment this
+   * out.
    */
+#ifndef __COVERITY__
   if (!g_atomic_ref_count_dec (&rarray->ref_count))
     flags |= PRESERVE_WRAPPER;
+#endif
 
   return ptr_array_free (array, flags);
 }


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