From: Simon Josefsson Date: Thu, 20 Aug 2009 10:15:21 +0000 (+0200) Subject: Reduce stack usage. X-Git-Tag: gnutls_2_9_4~59 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a4e5f6581c55aee85cc805081a8b4171c326da5a;p=thirdparty%2Fgnutls.git Reduce stack usage. --- diff --git a/lib/openpgp/output.c b/lib/openpgp/output.c index 68da821876..bd395841a6 100644 --- a/lib/openpgp/output.c +++ b/lib/openpgp/output.c @@ -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); diff --git a/lib/openpgp/pgp.c b/lib/openpgp/pgp.c index e9400414a2..8018ced20a 100644 --- a/lib/openpgp/pgp.c +++ b/lib/openpgp/pgp.c @@ -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)