From: Dr. David von Oheimb Date: Wed, 23 Jun 2021 12:26:22 +0000 (+0200) Subject: ossl_sk_ASN1_UTF8STRING2text(): Minor generalization and refactoring for readability X-Git-Tag: openssl-3.0.0-beta2~196 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cfd854a55e45626dd094f5d3846fd56fb4ec3cbf;p=thirdparty%2Fopenssl.git ossl_sk_ASN1_UTF8STRING2text(): Minor generalization and refactoring for readability Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/15879) --- diff --git a/crypto/asn1/asn1_lib.c b/crypto/asn1/asn1_lib.c index b1fa6b55a05..bdd0ec488d8 100644 --- a/crypto/asn1/asn1_lib.c +++ b/crypto/asn1/asn1_lib.c @@ -413,9 +413,9 @@ unsigned char *ASN1_STRING_data(ASN1_STRING *x) } #endif +/* |max_len| excludes NUL terminator and may be 0 to indicate no restriction */ char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text, - const char *sep, - size_t max_len /* excluding NUL terminator */) + const char *sep, size_t max_len) { int i; ASN1_UTF8STRING *current; @@ -423,26 +423,27 @@ char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text, char *result = NULL; char *p; - if (!ossl_assert(sep != NULL)) - return NULL; + if (sep == NULL) + sep = ""; sep_len = strlen(sep); - for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) { + for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) { current = sk_ASN1_UTF8STRING_value(text, i); if (i > 0) length += sep_len; length += ASN1_STRING_length(current); - if (length > max_len) + if (max_len != 0 && length > max_len) return NULL; } if ((result = OPENSSL_malloc(length + 1)) == NULL) return NULL; - for (i = 0, p = result; i < sk_ASN1_UTF8STRING_num(text); ++i) { + p = result; + for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) { current = sk_ASN1_UTF8STRING_value(text, i); length = ASN1_STRING_length(current); if (i > 0 && sep_len > 0) { - strncpy(p, sep, sep_len + 1); + strncpy(p, sep, sep_len + 1); /* using + 1 to silence gcc warning */ p += sep_len; } strncpy(p, (const char *)ASN1_STRING_get0_data(current), length);