tests: fix memory leaks

NIIBE Yutaka gniibe at fsij.org
Wed Aug 7 02:05:26 CEST 2013


On 2013-08-06 at 13:03 +0900, NIIBE Yutaka wrote:
> I found memory leaks in test code.  It's better to fix these
> as people may use the code as example.

Here is another fix for test code.  Another reason of these fixes is
that when we will find some memory leaks in future running test code,
we will be able to distinguish them as bugs.

OK to commit?

--------------------------------
tests: fix memory leaks.

* tests/benchmark.c (dsa_bench): Release SIG.

* tests/mpitests.c (test_powm): Release BASE, EXP, MOD, and RES.

* tests/prime.c (check_primes): Release PRIME.

* tests/tsexp.c (basic): Use intermediate variable M for constant.
Release S1, S2 and A.

diff --git a/tests/benchmark.c b/tests/benchmark.c
index 79048a3..f332003 100644
--- a/tests/benchmark.c
+++ b/tests/benchmark.c
@@ -864,7 +864,7 @@ dsa_bench (int iterations, int print_header)
   int p_sizes[3] = { 1024, 2048, 3072 };
   int q_sizes[3] = { 160, 224, 256 };
   gcry_sexp_t data;
-  gcry_sexp_t sig;
+  gcry_sexp_t sig = NULL;
   int i, j;
 
   err = gcry_sexp_sscan (pub_key+0, NULL, sample_public_dsa_key_1024,
@@ -916,6 +916,7 @@ dsa_bench (int iterations, int print_header)
       start_timer ();
       for (j=0; j < iterations; j++)
         {
+          gcry_sexp_release (sig);
           err = gcry_pk_sign (&sig, data, sec_key[i]);
           if (err)
             {
@@ -947,6 +948,7 @@ dsa_bench (int iterations, int print_header)
 
       gcry_sexp_release (sig);
       gcry_sexp_release (data);
+      sig = NULL;
     }
 

diff --git a/tests/mpitests.c b/tests/mpitests.c
index 432f3e8..03c15b9 100644
--- a/tests/mpitests.c
+++ b/tests/mpitests.c
@@ -362,6 +362,10 @@ test_powm (void)
   if (gcry_mpi_cmp (res, base))
     die ("test_powm failed at %d\n", __LINE__);
 
+  gcry_mpi_release (base);
+  gcry_mpi_release (exp);
+  gcry_mpi_release (mod);
+  gcry_mpi_release (res);
   /* Fixme: We should add the rest of the cases of course.  */
 

diff --git a/tests/prime.c b/tests/prime.c
index 6e825ae..89800e8 100644
--- a/tests/prime.c
+++ b/tests/prime.c
@@ -95,6 +95,7 @@ check_primes (void)
       gcry_mpi_add_ui (prime, prime, 1);
       err = gcry_prime_check (prime, 0);
       assert (err);
+      gcry_mpi_release (prime); prime = NULL;
     }
 }
 
diff --git a/tests/tsexp.c b/tests/tsexp.c
index cef3ed1..7c4f7c8 100644
--- a/tests/tsexp.c
+++ b/tests/tsexp.c
@@ -89,44 +89,52 @@ basic (void)
 
   for (pass=0;;pass++)
     {
+      gcry_mpi_t m;
+
       switch (pass)
         {
         case 0:
           string = ("(public-key (dsa (p #41424344#) (y this_is_y) "
                     "(q #61626364656667#) (g %m)))");
 
-          if ( gcry_sexp_build (&sexp, NULL, string,
-                                gcry_mpi_set_ui (NULL, 42)) )
+          m = gcry_mpi_set_ui (NULL, 42);
+          if ( gcry_sexp_build (&sexp, NULL, string, m ) )
             {
+              gcry_mpi_release (m);
               fail (" scanning `%s' failed\n", string);
               return;
             }
+          gcry_mpi_release (m);
           break;
 
         case 1:
           string = ("(public-key (dsa (p #41424344#) (y this_is_y) "
                     "(q %b) (g %m)))");
 
+          m = gcry_mpi_set_ui (NULL, 42);
           if ( gcry_sexp_build (&sexp, NULL, string,
-                                15, "foo\0\x01\0x02789012345",
-                                gcry_mpi_set_ui (NULL, 42)) )
+                                15, "foo\0\x01\0x02789012345", m) )
             {
+              gcry_mpi_release (m);
               fail (" scanning `%s' failed\n", string);
               return;
             }
+          gcry_mpi_release (m);
           break;
 
         case 2:
           string = ("(public-key (dsa (p #41424344#) (y silly_y_value) "
                     "(q %b) (g %m)))");
 
+          m = gcry_mpi_set_ui (NULL, 17);
           if ( gcry_sexp_build (&sexp, NULL, string,
-                                secure_buffer_len, secure_buffer,
-                                gcry_mpi_set_ui (NULL, 17)) )
+                                secure_buffer_len, secure_buffer, m) )
             {
+              gcry_mpi_release (m);
               fail (" scanning `%s' failed\n", string);
               return;
             }
+          gcry_mpi_release (m);
           if (!gcry_is_secure (sexp))
             fail ("gcry_sexp_build did not switch to secure memory\n");
           break;
@@ -144,13 +152,15 @@ basic (void)
 
             string = ("(public-key (dsa (p #41424344#) (parm %S) "
                       "(y dummy)(q %b) (g %m)))");
+            m = gcry_mpi_set_ui (NULL, 17);
             if ( gcry_sexp_build (&sexp, NULL, string, help_sexp,
-                                  secure_buffer_len, secure_buffer,
-                                  gcry_mpi_set_ui (NULL, 17)) )
+                                  secure_buffer_len, secure_buffer, m) )
               {
+                gcry_mpi_release (m);
                 fail (" scanning `%s' failed\n", string);
                 return;
               }
+            gcry_mpi_release (m);
             gcry_sexp_release (help_sexp);
           }
           break;
@@ -181,6 +191,7 @@ basic (void)
           p = gcry_sexp_nth_data (s1, 0, &n);
           if (!p)
             {
+              gcry_sexp_release (s1);
               fail ("no car for `%s'\n", token);
               continue;
             }
@@ -189,13 +200,16 @@ basic (void)
           s2 = gcry_sexp_cdr (s1);
           if (!s2)
             {
+              gcry_sexp_release (s1);
               fail ("no cdr for `%s'\n", token);
               continue;
             }
 
           p = gcry_sexp_nth_data (s2, 0, &n);
+          gcry_sexp_release (s2);
           if (p)
             {
+              gcry_sexp_release (s1);
               fail ("data at car of `%s'\n", token);
               continue;
             }
@@ -203,6 +217,7 @@ basic (void)
           if (parm)
             {
               s2 = gcry_sexp_find_token (s1, parm, strlen (parm));
+              gcry_sexp_release (s1);
               if (!s2)
                 {
                   fail ("didn't found `%s'\n", parm);
@@ -211,6 +226,7 @@ basic (void)
               p = gcry_sexp_nth_data (s2, 0, &n);
               if (!p)
                 {
+                  gcry_sexp_release (s2);
                   fail("no car for `%s'\n", parm );
                   continue;
                 }
@@ -218,18 +234,23 @@ basic (void)
               p = gcry_sexp_nth_data (s2, 1, &n);
               if (!p)
                 {
+                  gcry_sexp_release (s2);
                   fail("no cdr for `%s'\n", parm );
                   continue;
                 }
               info ("cdr=`%.*s'\n", (int)n, p);
 
               a = gcry_sexp_nth_mpi (s2, 0, GCRYMPI_FMT_USG);
+              gcry_sexp_release (s2);
               if (!a)
                 {
                   fail("failed to cdr the mpi for `%s'\n", parm);
                   continue;
                 }
+              gcry_mpi_release (a);
             }
+          else
+            gcry_sexp_release (s1);
         }
 
       gcry_sexp_release (sexp);
-- 






More information about the Gcrypt-devel mailing list