bug-gnulib
[Top][All Lists]
Advanced

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

Re: memory leak in bitset


From: Akim Demaille
Subject: Re: memory leak in bitset
Date: Tue, 19 Mar 2019 21:45:01 +0100

Hi Bruno,

> Le 10 mars 2019 à 14:27, Bruno Haible <address@hidden> a écrit :
> 
> Hi Akim,
> 
> There is apparently a memory leak in the 'bitset' module. After I modified
> 'test-bitset.c' to free the allocated bitsets, "gcc -fsanitize=leak" and
> valgrind still report lost memory allocations.
> 
> 
> "gcc -fsanitize=leak" reports:
> 
> ERROR: LeakSanitizer: detected memory leaks
> 
> Direct leak of 376 byte(s) in 27 object(s) allocated from:
>    #0 0x7ff0f00179d4 in __interceptor_realloc 
> ../../../../gcc-8.2.0/libsanitizer/lsan/lsan_interceptors.cc:80
>    #1 0x40b3d8 in vbitset_resize ../../gllib/bitset/vector.c:77
> 
> SUMMARY: LeakSanitizer: 376 byte(s) leaked in 27 allocation(s).
> FAIL test-bitset (exit status: 23)

Good catch, thanks!

In two patches.  A stylistic one, then the actual fix.  Cheers!

commit 7e957a8a54a798cc8367b8e7ad0b8ad79c512be1
Author: Akim Demaille <address@hidden>
Date:   Tue Mar 19 21:26:35 2019 +0100

    bitset: minor changes
    
    * lib/bitset/base.h (bitset_alloc_type): Remove, unused.
    * lib/bitset/table.c: Formatting changes.
    Remove useless braces.
    Prefer using else in cascades of if/else-if with returns.
    * lib/bitset/vector.c: Reduce scopes.

diff --git a/ChangeLog b/ChangeLog
index e842813d1..d0049e3c4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2019-03-19  Akim Demaille  <address@hidden>
+
+       bitset: minor changes
+       * lib/bitset/base.h (bitset_alloc_type): Remove, unused.
+       * lib/bitset/table.c: Formatting changes.
+       Remove useless braces.
+       Prefer using else in cascades of if/else-if with returns.
+       * lib/bitset/vector.c: Reduce scopes.
+
 2019-03-19  Akim Demaille  <address@hidden>
 
        bitset: expose bitset_resize
diff --git a/lib/bitset/base.h b/lib/bitset/base.h
index 4fcafac8b..fe1b8fbe1 100644
--- a/lib/bitset/base.h
+++ b/lib/bitset/base.h
@@ -55,8 +55,6 @@ enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, 
BITSET_VECTOR,
 
 extern const char * const bitset_type_names[];
 
-enum bitset_alloc_type {BITSET_MALLOC, BITSET_OBALLOC};
-
 /* Data type used to store a word of bits.  */
 typedef unsigned long bitset_word;
 #define BITSET_WORD_BITS ((unsigned) (CHAR_BIT * sizeof (bitset_word)))
diff --git a/lib/bitset/table.c b/lib/bitset/table.c
index 9cac96469..07184d657 100644
--- a/lib/bitset/table.c
+++ b/lib/bitset/table.c
@@ -188,21 +188,21 @@ tbitset_elt_alloc (void)
           /* Let particular systems override the size of a chunk.  */
 
 #ifndef OBSTACK_CHUNK_SIZE
-#define OBSTACK_CHUNK_SIZE 0
+# define OBSTACK_CHUNK_SIZE 0
 #endif
 
           /* Let them override the alloc and free routines too.  */
 
 #ifndef OBSTACK_CHUNK_ALLOC
-#define OBSTACK_CHUNK_ALLOC xmalloc
+# define OBSTACK_CHUNK_ALLOC xmalloc
 #endif
 
 #ifndef OBSTACK_CHUNK_FREE
-#define OBSTACK_CHUNK_FREE free
+# define OBSTACK_CHUNK_FREE free
 #endif
 
 #if ! defined __GNUC__ || __GNUC__ < 2
-#define __alignof__(type) 0
+# define __alignof__(type) 0
 #endif
 
           obstack_specify_allocation (&tbitset_obstack, OBSTACK_CHUNK_SIZE,
@@ -1018,13 +1018,9 @@ tbitset_op3_cmp (bitset dst, bitset src1, bitset src2, 
enum bitset_ops op)
         }
 
       if (!tbitset_elt_zero_p (delt))
-        {
-          tbitset_elt_add (dst, delt, j);
-        }
+        tbitset_elt_add (dst, delt, j);
       else
-        {
-          tbitset_elt_free (delt);
-        }
+        tbitset_elt_free (delt);
     }
 
   /* If we have elements of DST left over, free them all.  */
@@ -1059,7 +1055,8 @@ tbitset_and_cmp (bitset dst, bitset src1, bitset src2)
       tbitset_zero (dst);
       return changed;
     }
-  return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_AND);
+  else
+    return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_AND);
 }
 
 
@@ -1074,9 +1071,7 @@ static bool
 tbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
 {
   if (EBITSET_ZERO_P (src2))
-    {
-      return tbitset_copy_cmp (dst, src1);
-    }
+    return tbitset_copy_cmp (dst, src1);
   else if (EBITSET_ZERO_P (src1))
     {
       tbitset_weed (dst);
@@ -1084,7 +1079,8 @@ tbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
       tbitset_zero (dst);
       return changed;
     }
-  return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_ANDN);
+  else
+    return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_ANDN);
 }
 
 
@@ -1099,14 +1095,11 @@ static bool
 tbitset_or_cmp (bitset dst, bitset src1, bitset src2)
 {
   if (EBITSET_ZERO_P (src2))
-    {
-      return tbitset_copy_cmp (dst, src1);
-    }
+    return tbitset_copy_cmp (dst, src1);
   else if (EBITSET_ZERO_P (src1))
-    {
-      return tbitset_copy_cmp (dst, src2);
-    }
-  return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_OR);
+    return tbitset_copy_cmp (dst, src2);
+  else
+    return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_OR);
 }
 
 
@@ -1121,14 +1114,11 @@ static bool
 tbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
 {
   if (EBITSET_ZERO_P (src2))
-    {
-      return tbitset_copy_cmp (dst, src1);
-    }
+    return tbitset_copy_cmp (dst, src1);
   else if (EBITSET_ZERO_P (src1))
-    {
-      return tbitset_copy_cmp (dst, src2);
-    }
-  return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_XOR);
+    return tbitset_copy_cmp (dst, src2);
+  else
+    return tbitset_op3_cmp (dst, src1, src2, BITSET_OP_XOR);
 }
 
 
diff --git a/lib/bitset/vector.c b/lib/bitset/vector.c
index 15d3e9956..0623651dc 100644
--- a/lib/bitset/vector.c
+++ b/lib/bitset/vector.c
@@ -467,7 +467,6 @@ vbitset_and (bitset dst, bitset src1, bitset src2)
 static bool
 vbitset_and_cmp (bitset dst, bitset src1, bitset src2)
 {
-  bool changed = false;
   vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
 
   bitset_windex dsize = VBITSET_SIZE (dst);
@@ -477,6 +476,7 @@ vbitset_and_cmp (bitset dst, bitset src1, bitset src2)
   bitset_word *src1p = VBITSET_WORDS (src1);
   bitset_word *src2p = VBITSET_WORDS (src2);
 
+  bool changed = false;
   unsigned i;
   for (i = 0; i < min (ssize1, ssize2); i++, dstp++)
     {
@@ -546,7 +546,6 @@ vbitset_andn (bitset dst, bitset src1, bitset src2)
 static bool
 vbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
 {
-  bool changed = false;
   vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
 
   bitset_windex dsize = VBITSET_SIZE (dst);
@@ -556,6 +555,7 @@ vbitset_andn_cmp (bitset dst, bitset src1, bitset src2)
   bitset_word *src1p = VBITSET_WORDS (src1);
   bitset_word *src2p = VBITSET_WORDS (src2);
 
+  bool changed = false;
   unsigned i;
   for (i = 0; i < min (ssize1, ssize2); i++, dstp++)
     {
@@ -633,8 +633,6 @@ vbitset_or (bitset dst, bitset src1, bitset src2)
 static bool
 vbitset_or_cmp (bitset dst, bitset src1, bitset src2)
 {
-  bool changed = false;
-
   vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
 
   bitset_windex dsize = VBITSET_SIZE (dst);
@@ -644,6 +642,7 @@ vbitset_or_cmp (bitset dst, bitset src1, bitset src2)
   bitset_word *src1p = VBITSET_WORDS (src1);
   bitset_word *src2p = VBITSET_WORDS (src2);
 
+  bool changed = false;
   unsigned i;
   for (i = 0; i < min (ssize1, ssize2); i++, dstp++)
     {
@@ -711,7 +710,6 @@ vbitset_xor (bitset dst, bitset src1, bitset src2)
 static bool
 vbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
 {
-  bool changed = false;
   vbitset_resize (dst, max (BITSET_SIZE_ (src1), BITSET_SIZE_ (src2)));
 
   bitset_windex dsize = VBITSET_SIZE (dst);
@@ -721,6 +719,7 @@ vbitset_xor_cmp (bitset dst, bitset src1, bitset src2)
   bitset_word *src1p = VBITSET_WORDS (src1);
   bitset_word *src2p = VBITSET_WORDS (src2);
 
+  bool changed = false;
   unsigned i;
   for (i = 0; i < min (ssize1, ssize2); i++, dstp++)
     {
@@ -903,8 +902,8 @@ vbitset_or_and_cmp (bitset dst, bitset src1, bitset src2, 
bitset src3)
   bitset_word *dstp = VBITSET_WORDS (dst);
   bitset_windex size = VBITSET_SIZE (dst);
 
-  unsigned i;
   bool changed = false;
+  unsigned i;
   for (i = 0; i < size; i++, dstp++)
     {
       bitset_word tmp = (*src1p++ | *src2p++) & *src3p++;







commit bbc77d41000305aed9fd022106eb1f46803dc25c
Author: Akim Demaille <address@hidden>
Date:   Tue Mar 19 21:38:00 2019 +0100

    bitset: fix memory leaks
    
    Reported by Bruno Haible.
    https://lists.gnu.org/archive/html/bug-gnulib/2019-03/msg00027.html
    
    * lib/bitset/vector.c (vbitset_free): New.
    (vbitset_vtable): Use it.

diff --git a/ChangeLog b/ChangeLog
index d0049e3c4..9a1698598 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2019-03-19  Akim Demaille  <address@hidden>
+
+       bitset: fix memory leaks
+       Reported by Bruno Haible.
+       https://lists.gnu.org/archive/html/bug-gnulib/2019-03/msg00027.html
+       * lib/bitset/vector.c (vbitset_free): New.
+       (vbitset_vtable): Use it.
+
 2019-03-19  Akim Demaille  <address@hidden>
 
        bitset: minor changes
diff --git a/lib/bitset/vector.c b/lib/bitset/vector.c
index 0623651dc..54f148d56 100644
--- a/lib/bitset/vector.c
+++ b/lib/bitset/vector.c
@@ -928,6 +928,13 @@ vbitset_copy (bitset dst, bitset src)
 }
 
 
+static void
+vbitset_free (bitset bset)
+{
+  free (VBITSET_WORDS (bset));
+}
+
+
 /* Vector of operations for multiple word bitsets.  */
 struct bitset_vtable vbitset_vtable = {
   vbitset_set,
@@ -961,7 +968,7 @@ struct bitset_vtable vbitset_vtable = {
   vbitset_or_and_cmp,
   vbitset_list,
   vbitset_list_reverse,
-  NULL,
+  vbitset_free,
   BITSET_VECTOR
 };
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]