]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Reduce stack usage.
authorSimon Josefsson <simon@josefsson.org>
Thu, 20 Aug 2009 10:15:21 +0000 (12:15 +0200)
committerSimon Josefsson <simon@josefsson.org>
Thu, 20 Aug 2009 10:15:21 +0000 (12:15 +0200)
lib/openpgp/output.c
lib/openpgp/pgp.c

index 68da8218764414c409c0f5140ba7c54e8252ff68..bd395841a6104a7226c0b3dbdd8a7b263745ef2e 100644 (file)
@@ -309,8 +309,6 @@ print_cert (gnutls_string * str, gnutls_openpgp_crt_t cert)
 {
   int i, subkeys;
   int err;
-  char dn[1024];
-  size_t dn_size;
 
   print_key_revoked (str, cert, -1);
 
@@ -332,23 +330,36 @@ print_cert (gnutls_string * str, gnutls_openpgp_crt_t cert)
   i = 0;
   do
     {
-      dn_size = sizeof (dn);
-      err = gnutls_openpgp_crt_get_name (cert, i++, dn, &dn_size);
-
-      if (err < 0 && err != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE &&
-         err != GNUTLS_E_OPENPGP_UID_REVOKED)
-       {
-         addf (str, "error: get_name: %s %d\n", gnutls_strerror (err), err);
-         break;
-       }
-
-      if (err >= 0)
-       addf (str, _("\tName[%d]: %s\n"), i - 1, dn);
-      else if (err == GNUTLS_E_OPENPGP_UID_REVOKED)
+      char *dn;
+      size_t dn_size = 0;
+
+      err = gnutls_openpgp_crt_get_name (cert, i, NULL, &dn_size);
+      if (err != GNUTLS_E_SHORT_MEMORY_BUFFER
+         && err != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
+         && err != GNUTLS_E_OPENPGP_UID_REVOKED)
+       addf (str, "error: get_name: %s\n", gnutls_strerror (err));
+      else
        {
-         addf (str, _("\tRevoked Name[%d]: %s\n"), i - 1, dn);
+         dn = gnutls_malloc (dn_size);
+         if (!dn)
+           addf (str, "error: malloc (%d): %s\n", (int) dn_size,
+                 gnutls_strerror (GNUTLS_E_MEMORY_ERROR));
+         else
+           {
+             err = gnutls_openpgp_crt_get_name (cert, i, dn, &dn_size);
+             if (err < 0 && err != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE &&
+                 err != GNUTLS_E_OPENPGP_UID_REVOKED)
+               addf (str, "error: get_name: %s\n", gnutls_strerror (err));
+             else if (err >= 0)
+               addf (str, _("\tName[%d]: %s\n"), i, dn);
+             else if (err == GNUTLS_E_OPENPGP_UID_REVOKED)
+               addf (str, _("\tRevoked Name[%d]: %s\n"), i, dn);
+
+             gnutls_free (dn);
+           }
        }
 
+      i++;
     }
   while (err >= 0);
 
@@ -379,26 +390,39 @@ print_oneline (gnutls_string * str, gnutls_openpgp_crt_t cert)
 {
   int err, i;
 
-  /* Names. */
   i = 0;
   do
     {
-      size_t dn_size;
-      char dn[1024];
-
-      dn_size = sizeof (dn);
-      err = gnutls_openpgp_crt_get_name (cert, i++, dn, &dn_size);
-      if (err < 0 && err != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE &&
-         err != GNUTLS_E_OPENPGP_UID_REVOKED)
+      char *dn;
+      size_t dn_size = 0;
+
+      err = gnutls_openpgp_crt_get_name (cert, i, NULL, &dn_size);
+      if (err != GNUTLS_E_SHORT_MEMORY_BUFFER
+         && err != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
+         && err != GNUTLS_E_OPENPGP_UID_REVOKED)
+       addf (str, "unknown name (%s), ", gnutls_strerror (err));
+      else
        {
-         addf (str, "cannot get_name %d (%s), ", err, gnutls_strerror (err));
-         break;
+         dn = gnutls_malloc (dn_size);
+         if (!dn)
+           addf (str, "unknown name (%s), ",
+                 gnutls_strerror (GNUTLS_E_MEMORY_ERROR));
+         else
+           {
+             err = gnutls_openpgp_crt_get_name (cert, i, dn, &dn_size);
+             if (err < 0 && err != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE &&
+                 err != GNUTLS_E_OPENPGP_UID_REVOKED)
+               addf (str, "unknown name (%s), ", gnutls_strerror (err));
+             else if (err >= 0)
+               addf (str, _("name[%d]: %s, "), i, dn);
+             else if (err == GNUTLS_E_OPENPGP_UID_REVOKED)
+               addf (str, _("revoked name[%d]: %s, "), i, dn);
+
+             gnutls_free (dn);
+           }
        }
 
-      if (err >= 0)
-       addf (str, _("name[%d]: %s, "), i - 1, dn);
-      else if (err == GNUTLS_E_OPENPGP_UID_REVOKED)
-       addf (str, _("revoked name[%d]: %s, "), i - 1, dn);
+      i++;
     }
   while (err >= 0);
 
index e9400414a2923b05ea9fc2f8488d92220959a0c0..8018ced20aafb28bbf0c67c4824cdcb736603534 100644 (file)
@@ -305,7 +305,8 @@ _gnutls_openpgp_count_key_names (gnutls_openpgp_crt_t key)
  * gnutls_openpgp_crt_get_name - Extracts the userID
  * @key: the structure that contains the OpenPGP public key.
  * @idx: the index of the ID to extract
- * @buf: a pointer to a structure to hold the name
+ * @buf: a pointer to a structure to hold the name, may be %NULL
+ *       to only get the @sizeof_buf.
  * @sizeof_buf: holds the maximum size of @buf, on return hold the
  *   actual/required size of @buf.
  *
@@ -324,7 +325,7 @@ gnutls_openpgp_crt_get_name (gnutls_openpgp_crt_t key,
   cdk_pkt_userid_t uid = NULL;
   int pos = 0;
 
-  if (!key || !buf)
+  if (!key)
     {
       gnutls_assert ();
       return GNUTLS_E_INVALID_REQUEST;
@@ -359,8 +360,11 @@ gnutls_openpgp_crt_get_name (gnutls_openpgp_crt_t key,
       return GNUTLS_E_SHORT_MEMORY_BUFFER;
     }
 
-  memcpy (buf, uid->name, uid->len);
-  buf[uid->len] = '\0';                /* make sure it's a string */
+  if (buf)
+    {
+      memcpy (buf, uid->name, uid->len);
+      buf[uid->len] = '\0';            /* make sure it's a string */
+    }
   *sizeof_buf = uid->len + 1;
 
   if (uid->is_revoked)