bug-gnulib
[Top][All Lists]
Advanced

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

tests: free allocated memory


From: Bruno Haible
Subject: tests: free allocated memory
Date: Sun, 10 Mar 2019 14:06:31 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-141-generic; KDE/5.18.0; x86_64; ; )

CC="gcc -fsanitize=leak" and valgrind can detect memory leaks.
But first, we have to eliminate the false positives coming from the
tests.

Yes, I spoke against it recently, in
<https://lists.gnu.org/archive/html/bug-gnulib/2018-10/msg00037.html>.
But well, I changed my opinion.


2019-03-10  Bruno Haible  <address@hidden>

        tests: Free allocated memory.
        Reported by <address@hidden> via Assaf Gordon.
        * tests/test-astrxfrm.c (main): Free allocated memory.
        * tests/test-bitset.c (compare, check_attributes): Free allocated
        bitsets.
        * tests/test-filenamecat.c (main): Free allocated memory.
        * tests/test-freadahead.c (main): Free allocated memory and close stdin.
        * tests/test-freadptr.c (main): Likewise.
        * tests/test-freadptr2.c (main): Free allocated memory.
        * tests/test-freadseek.c (main): Likewise.
        * tests/test-gc-arcfour.c (main): Close allocated context.
        * tests/test-gc-arctwo.c (main): Likewise.
        * tests/test-gc-des.c (main): Close all allocated contexts.
        * tests/test-pipe-filter-gi1.c (main): Free allocated memory.
        * tests/test-pipe-filter-ii1.c (main): Likewise.
        * tests/test-posix_spawn_file_actions_addchdir.c (main): Destroy the
        allocated file actions.
        * tests/test-posix_spawn_file_actions_addclose.c (main): Likewise.
        * tests/test-posix_spawn_file_actions_adddup2.c (main): Likewise.
        * tests/test-posix_spawn_file_actions_addopen.c (main): Likewise.
        * tests/test-sameacls.c (main): Free allocated memory and ACLs.
        * tests/test-strfmon_l.c (main): Free allocated locales.
        * tests/test-striconveh.c (main): Free allocated iconv_t objects.
        * tests/uniconv/test-u8-conv-to-enc.c (main): Free allocated memory.
        * tests/uniconv/test-u16-conv-to-enc.c (main): Likewise.
        * tests/uniconv/test-u32-conv-to-enc.c (main): Likewise.
        * tests/unistr/test-chr.h (main): Free input32.
        * tests/unistr/test-strchr.h (test_strchr): Likewise.

diff --git a/tests/test-astrxfrm.c b/tests/test-astrxfrm.c
index c553c90..be0241a 100644
--- a/tests/test-astrxfrm.c
+++ b/tests/test-astrxfrm.c
@@ -49,6 +49,7 @@ main ()
     ASSERT (transform != NULL);
     ASSERT (strcmp (transform, expected_transform) == 0);
     ASSERT (length == strlen (transform) + 1);
+    free (transform);
   }
 
   /* Test resultbuf != NULL with an initial length = 0.  */
@@ -63,6 +64,7 @@ main ()
     ASSERT (strcmp (transform, expected_transform) == 0);
     ASSERT (length == strlen (transform) + 1);
     ASSERT (buf[0] == '@');
+    free (transform);
   }
 
   /* Test resultbuf != NULL with an initial length that is too small.  */
@@ -77,6 +79,7 @@ main ()
     ASSERT (strcmp (transform, expected_transform) == 0);
     ASSERT (length == strlen (transform) + 1);
     ASSERT (buf[sizeof (buf) - 1] == '@');
+    free (transform);
   }
 
   /* Test resultbuf != NULL with an initial length that is large enough.  */
@@ -93,5 +96,7 @@ main ()
     ASSERT (buf[sizeof (buf) - 1] == '@');
   }
 
+  free (expected_transform);
+
   return 0;
 }
diff --git a/tests/test-bitset.c b/tests/test-bitset.c
index a8c2531..0328650 100644
--- a/tests/test-bitset.c
+++ b/tests/test-bitset.c
@@ -114,6 +114,17 @@ void compare (enum bitset_attr a, enum bitset_attr b)
   ASSERT (bitset_or_and_cmp (adst, asrc0, asrc1, asrc2)
           == bitset_or_and_cmp (bdst, bsrc0, bsrc1, bsrc2));
   assert_bitset_equal (adst, bdst);
+
+  bitset_free (bdst);
+  bitset_free (bsrc3);
+  bitset_free (bsrc2);
+  bitset_free (bsrc1);
+  bitset_free (bsrc0);
+  bitset_free (adst);
+  bitset_free (asrc3);
+  bitset_free (asrc2);
+  bitset_free (asrc1);
+  bitset_free (asrc0);
 }
 
 void check_attributes (enum bitset_attr attr)
@@ -148,6 +159,11 @@ void check_attributes (enum bitset_attr attr)
   /* or */
   bitset_or (bs, bs1, bs2);
   ASSERT (bitset_count (bs) == 6);
+
+  bitset_free (bs);
+  bitset_free (bs2);
+  bitset_free (bs1);
+  bitset_free (bs0);
 }
 
 int main (void)
diff --git a/tests/test-filenamecat.c b/tests/test-filenamecat.c
index 4ca7e2f..a37018f 100644
--- a/tests/test-filenamecat.c
+++ b/tests/test-filenamecat.c
@@ -79,6 +79,7 @@ main (int argc _GL_UNUSED, char *argv[])
                    i, t[0], res);
           fail = true;
         }
+      free (res);
     }
   exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);
 }
diff --git a/tests/test-freadahead.c b/tests/test-freadahead.c
index 287a936..a3c75d0 100644
--- a/tests/test-freadahead.c
+++ b/tests/test-freadahead.c
@@ -33,6 +33,7 @@ main (int argc, char **argv)
     {
       void *buf = malloc (nbytes);
       ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
+      free (buf);
     }
 
   if (nbytes == 0)
@@ -69,5 +70,8 @@ main (int argc, char **argv)
         }
     }
 
+  /* Free memory allocated during ungetc().  */
+  fclose (stdin);
+
   return 0;
 }
diff --git a/tests/test-freadptr.c b/tests/test-freadptr.c
index 8b17923..83b2294 100644
--- a/tests/test-freadptr.c
+++ b/tests/test-freadptr.c
@@ -30,8 +30,11 @@ int
 main (int argc, char **argv)
 {
   int nbytes = atoi (argv[1]);
-  void *buf = malloc (nbytes);
-  ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
+  {
+    void *buf = malloc (nbytes);
+    ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
+    free (buf);
+  }
 
   if (lseek (0, 0, SEEK_CUR) == nbytes)
     {
@@ -90,5 +93,8 @@ main (int argc, char **argv)
       }
     }
 
+  /* Free memory allocated during ungetc().  */
+  fclose (stdin);
+
   return 0;
 }
diff --git a/tests/test-freadptr2.c b/tests/test-freadptr2.c
index 95abb8d..3767988 100644
--- a/tests/test-freadptr2.c
+++ b/tests/test-freadptr2.c
@@ -43,6 +43,7 @@ main (int argc, char **argv)
     {
       void *buf = malloc (nbytes);
       ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
+      free (buf);
     }
 
   if (nbytes == 0)
diff --git a/tests/test-freadseek.c b/tests/test-freadseek.c
index 7ae02c1..c5a7b49 100644
--- a/tests/test-freadseek.c
+++ b/tests/test-freadseek.c
@@ -89,5 +89,10 @@ main (int argc, char **argv)
   ASSERT (!ferror (stdin));
 #endif
 
+  free (buf7);
+  free (buf5);
+  free (buf3);
+  free (buf1);
+
   return 0;
 }
diff --git a/tests/test-gc-arcfour.c b/tests/test-gc-arcfour.c
index 6def7ab..aae29c9 100644
--- a/tests/test-gc-arcfour.c
+++ b/tests/test-gc-arcfour.c
@@ -90,6 +90,8 @@ main (int argc, char *argv[])
       return 1;
     }
 
+  gc_cipher_close (ctx);
+
   gc_done ();
 
   return 0;
diff --git a/tests/test-gc-arctwo.c b/tests/test-gc-arctwo.c
index f52b8da..6473953 100644
--- a/tests/test-gc-arctwo.c
+++ b/tests/test-gc-arctwo.c
@@ -89,6 +89,8 @@ main (int argc, char *argv[])
       return 1;
     }
 
+  gc_cipher_close (ctx);
+
   gc_done ();
 
   return 0;
diff --git a/tests/test-gc-des.c b/tests/test-gc-des.c
index 9103d27..3d50f83 100644
--- a/tests/test-gc-des.c
+++ b/tests/test-gc-des.c
@@ -25,7 +25,6 @@
 int
 main (int argc, char *argv[])
 {
-  gc_cipher_handle ctx;
   Gc_rc rc;
 
   rc = gc_init ();
@@ -44,9 +43,11 @@ main (int argc, char *argv[])
     char input[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
     char result[8] = { 0x24, 0x6e, 0x9d, 0xb9, 0xc5, 0x50, 0x38, 0x1a };
     char temp1[8], temp2[8], temp3[8];
+    gc_cipher_handle ctx_array[64];
 
     for (i = 0; i < 64; ++i)
       {
+        gc_cipher_handle ctx;
 
         rc = gc_cipher_open (GC_DES, GC_ECB, &ctx);
         if (rc != GC_OK)
@@ -77,9 +78,14 @@ main (int argc, char *argv[])
 
         memcpy (key, temp3, 8);
         memcpy (input, temp1, 8);
+
+        ctx_array[i] = ctx;
       }
     if (memcmp (temp3, result, 8))
       return 1;
+
+    for (i = 0; i < 64; ++i)
+      gc_cipher_close (ctx_array[i]);
   }
 
   gc_done ();
diff --git a/tests/test-pipe-filter-gi1.c b/tests/test-pipe-filter-gi1.c
index 83ce73b..1ba4c89 100644
--- a/tests/test-pipe-filter-gi1.c
+++ b/tests/test-pipe-filter-gi1.c
@@ -108,5 +108,7 @@ main (int argc, char *argv[])
     ASSERT (l.nread == input_size);
   }
 
+  free (input);
+
   return 0;
 }
diff --git a/tests/test-pipe-filter-ii1.c b/tests/test-pipe-filter-ii1.c
index 7efa1c2..af97f33 100644
--- a/tests/test-pipe-filter-ii1.c
+++ b/tests/test-pipe-filter-ii1.c
@@ -130,5 +130,7 @@ main (int argc, char *argv[])
     ASSERT (l.nread == input_size);
   }
 
+  free (input);
+
   return 0;
 }
diff --git a/tests/test-posix_spawn_file_actions_addchdir.c 
b/tests/test-posix_spawn_file_actions_addchdir.c
index ebdd804..7a196b5 100644
--- a/tests/test-posix_spawn_file_actions_addchdir.c
+++ b/tests/test-posix_spawn_file_actions_addchdir.c
@@ -38,5 +38,7 @@ main (void)
 
   ASSERT (posix_spawn_file_actions_addchdir (&actions, "/") == 0);
 
+  posix_spawn_file_actions_destroy (&actions);
+
   return 0;
 }
diff --git a/tests/test-posix_spawn_file_actions_addclose.c 
b/tests/test-posix_spawn_file_actions_addclose.c
index d023766..77927d8 100644
--- a/tests/test-posix_spawn_file_actions_addclose.c
+++ b/tests/test-posix_spawn_file_actions_addclose.c
@@ -60,5 +60,7 @@ main (void)
     ASSERT (posix_spawn_file_actions_addclose (&actions, bad_fd) == EBADF);
   }
 
+  posix_spawn_file_actions_destroy (&actions);
+
   return 0;
 }
diff --git a/tests/test-posix_spawn_file_actions_adddup2.c 
b/tests/test-posix_spawn_file_actions_adddup2.c
index ebc6efd..0c102f9 100644
--- a/tests/test-posix_spawn_file_actions_adddup2.c
+++ b/tests/test-posix_spawn_file_actions_adddup2.c
@@ -68,5 +68,7 @@ main (void)
     ASSERT (posix_spawn_file_actions_adddup2 (&actions, 2, bad_fd) == EBADF);
   }
 
+  posix_spawn_file_actions_destroy (&actions);
+
   return 0;
 }
diff --git a/tests/test-posix_spawn_file_actions_addopen.c 
b/tests/test-posix_spawn_file_actions_addopen.c
index abee6e8..148002a 100644
--- a/tests/test-posix_spawn_file_actions_addopen.c
+++ b/tests/test-posix_spawn_file_actions_addopen.c
@@ -66,5 +66,7 @@ main (void)
             == EBADF);
   }
 
+  posix_spawn_file_actions_destroy (&actions);
+
   return 0;
 }
diff --git a/tests/test-sameacls.c b/tests/test-sameacls.c
index 228c16b..44a111b 100644
--- a/tests/test-sameacls.c
+++ b/tests/test-sameacls.c
@@ -84,6 +84,9 @@ main (int argc, char *argv[])
         fflush (stderr);
         abort ();
       }
+
+    free (contents2);
+    free (contents1);
   }
 
   /* Compare the access permissions of the two files, including ACLs.  */
@@ -218,6 +221,12 @@ main (int argc, char *argv[])
                 return 1;
               }
           }
+        acl_free (text2);
+        if (acl2 != (acl_t)NULL)
+          acl_free (acl2);
+        acl_free (text1);
+        if (acl1 != (acl_t)NULL)
+          acl_free (acl1);
       }
 #elif HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
   int count1;
@@ -287,6 +296,8 @@ main (int argc, char *argv[])
               return 1;
             }
         }
+      free (entries2);
+      free (entries1);
     }
 # ifdef ACE_GETACL
   count1 = acl (file1, ACE_GETACLCNT, 0, NULL);
@@ -366,6 +377,8 @@ main (int argc, char *argv[])
             return 1;
           }
       }
+    free (entries2);
+    free (entries1);
   }
 # endif
 #elif HAVE_GETACL /* HP-UX */
@@ -438,6 +451,8 @@ main (int argc, char *argv[])
               return 1;
             }
         }
+      free (entries2);
+      free (entries1);
     }
 
 # if HAVE_ACLV_H /* HP-UX >= 11.11 */
@@ -511,6 +526,8 @@ main (int argc, char *argv[])
               return 1;
             }
         }
+      free (entries2);
+      free (entries1);
     }
 # endif
 #elif HAVE_ACLX_GET /* AIX */
@@ -687,6 +704,8 @@ main (int argc, char *argv[])
               return 1;
             }
         }
+      free (entries2);
+      free (entries1);
     }
 #endif
   }
diff --git a/tests/test-strfmon_l.c b/tests/test-strfmon_l.c
index 1294609..3e60b7c 100644
--- a/tests/test-strfmon_l.c
+++ b/tests/test-strfmon_l.c
@@ -74,6 +74,7 @@ main (void)
     loc = newlocale (LC_ALL_MASK, "en_US.UTF-8", NULL);
     ASSERT (strfmon_l (buf, sizeof (buf), loc, "%.2n", 123.5) >= 0);
     ASSERT (strcmp (buf, expected_buf) == 0);
+    freelocale (loc);
   }
   {
     char expected_buf[80];
@@ -86,6 +87,7 @@ main (void)
     loc = newlocale (LC_ALL_MASK, "de_DE.UTF-8", NULL);
     ASSERT (strfmon_l (buf, sizeof (buf), loc, "%.2n", 123.5) >= 0);
     ASSERT (strcmp (buf, expected_buf) == 0);
+    freelocale (loc);
   }
 #endif
 
diff --git a/tests/test-striconveh.c b/tests/test-striconveh.c
index 6cb7f1f..ed41884 100644
--- a/tests/test-striconveh.c
+++ b/tests/test-striconveh.c
@@ -1108,5 +1108,13 @@ main ()
 
 #endif
 
+  /* -------------------------------- Done. -------------------------------- */
+
+  if (cd_ascii_to_88591 != (iconv_t)(-1))
+    iconv_close (cd_ascii_to_88591);
+  iconv_close (cd_ascii_to_utf8);
+  if (cd_utf7_to_utf8 != (iconv_t)(-1))
+    iconv_close (cd_utf7_to_utf8);
+
   return 0;
 }
diff --git a/tests/uniconv/test-u16-conv-to-enc.c 
b/tests/uniconv/test-u16-conv-to-enc.c
index 0d34816..79808f6 100644
--- a/tests/uniconv/test-u16-conv-to-enc.c
+++ b/tests/uniconv/test-u16-conv-to-enc.c
@@ -106,6 +106,8 @@ main ()
               ASSERT (result == NULL);
               ASSERT (errno == EILSEQ);
               ASSERT (length == 0xdead);
+              if (o)
+                free (offsets);
               break;
             case iconveh_question_mark:
               {
diff --git a/tests/uniconv/test-u32-conv-to-enc.c 
b/tests/uniconv/test-u32-conv-to-enc.c
index b2d48ce..4e838a6 100644
--- a/tests/uniconv/test-u32-conv-to-enc.c
+++ b/tests/uniconv/test-u32-conv-to-enc.c
@@ -106,6 +106,8 @@ main ()
               ASSERT (result == NULL);
               ASSERT (errno == EILSEQ);
               ASSERT (length == 0xdead);
+              if (o)
+                free (offsets);
               break;
             case iconveh_question_mark:
               {
diff --git a/tests/uniconv/test-u8-conv-to-enc.c 
b/tests/uniconv/test-u8-conv-to-enc.c
index 9e2b098..93ec476 100644
--- a/tests/uniconv/test-u8-conv-to-enc.c
+++ b/tests/uniconv/test-u8-conv-to-enc.c
@@ -106,6 +106,8 @@ main ()
               ASSERT (result == NULL);
               ASSERT (errno == EILSEQ);
               ASSERT (length == 0xdead);
+              if (o)
+                free (offsets);
               break;
             case iconveh_question_mark:
               {
diff --git a/tests/unistr/test-chr.h b/tests/unistr/test-chr.h
index 8dbdcca..375677c 100644
--- a/tests/unistr/test-chr.h
+++ b/tests/unistr/test-chr.h
@@ -132,6 +132,8 @@ main (void)
   }
 
   free (input);
+  if (sizeof (UNIT) != sizeof (uint32_t))
+    free (input32);
 
   return 0;
 }
diff --git a/tests/unistr/test-strchr.h b/tests/unistr/test-strchr.h
index fa48767..c967b2d 100644
--- a/tests/unistr/test-strchr.h
+++ b/tests/unistr/test-strchr.h
@@ -158,4 +158,6 @@ test_strchr (void)
 #endif
 
   free (input);
+  if (sizeof (UNIT) != sizeof (uint32_t))
+    free (input32);
 }




reply via email to

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