cipher: fix memory leaks

NIIBE Yutaka gniibe at fsij.org
Tue Aug 6 04:28:12 CEST 2013


I found memory leaks, and following is my fix for master.

Still, there is a leak for sexp_elements_extract_ecc when
ELEMENT_NAMES is "pabgnqd" and WANT_PRIVATE is true, and
Q is optional.  In this case, elements[6] won't be freed,
as elements[5] is NULL (for Q).  We need to fix caller.


------------------------------------------
cipher: fix memory leak.

* cipher/elgamal.c (elg_generate_ext): Free XVALUE.

* cipher/pubkey.c (sexp_elements_extract): Don't use IDX for loop.
Call mpi_free.
(sexp_elements_extract_ecc): Call mpi_free.

diff --git a/cipher/elgamal.c b/cipher/elgamal.c
index b40d132..7540e3f 100644
--- a/cipher/elgamal.c
+++ b/cipher/elgamal.c
@@ -641,7 +641,10 @@ elg_generate_ext (int algo, unsigned int nbits, unsigned long evalue,
     }
 
   if (xvalue)
-    ec = generate_using_x (&sk, nbits, xvalue, retfactors);
+    {
+      ec = generate_using_x (&sk, nbits, xvalue, retfactors);
+      mpi_free (xvalue);
+    }
   else
     {
       generate (&sk, nbits, retfactors);
diff --git a/cipher/pubkey.c b/cipher/pubkey.c
index b540bd5..e867169 100644
--- a/cipher/pubkey.c
+++ b/cipher/pubkey.c
@@ -1832,8 +1832,8 @@ sexp_elements_extract (gcry_sexp_t key_sexp, const char *element_names,
   if (!err)
     {
       /* Check that all elements are available.  */
-      for (name = element_names, idx = 0; *name; name++, idx++)
-        if (!elements[idx])
+      for (name = element_names, i = 0; *name; name++, i++)
+        if (!elements[i])
           break;
       if (*name)
         {
@@ -1857,7 +1857,7 @@ sexp_elements_extract (gcry_sexp_t key_sexp, const char *element_names,
     {
       for (i = 0; i < idx; i++)
         if (elements[i])
-          gcry_free (elements[i]);
+          mpi_free (elements[i]);
     }
   return err;
 }
@@ -1963,7 +1963,7 @@ sexp_elements_extract_ecc (gcry_sexp_t key_sexp, const char *element_names,
     {
       for (name = element_names, idx = 0; *name; name++, idx++)
         if (elements[idx])
-          gcry_free (elements[idx]);
+          mpi_free (elements[idx]);
     }
   return err;
 }






More information about the Gcrypt-devel mailing list