From: beck Date: Thu, 24 Apr 2014 15:07:20 +0000 (+0000) Subject: add ERR_asprintf_error_data, A tool to be used to get rid of the far too X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=4ef4c558942e8a149bcf1979b5b546f6c7fac55d;p=openbsd add ERR_asprintf_error_data, A tool to be used to get rid of the far too frequent construct of 30 lines of pointer and strlcat insanity followed by an ERR_add_error_data. I will sweep through here like a chubby mongol horde in the next few days pillaging crappy ERR_add_error_data's. Oh and while we're at it fix the nasty vdata function to use something less hard on the eyes. ok jsing@ --- diff --git a/lib/libcrypto/err/err.c b/lib/libcrypto/err/err.c index eaafb29101e..ee4597e9b17 100644 --- a/lib/libcrypto/err/err.c +++ b/lib/libcrypto/err/err.c @@ -1068,6 +1068,20 @@ ERR_set_error_data(char *data, int flags) es->err_data_flags[i] = flags; } +void +ERR_asprintf_error_data(char * format, ...) { + char *errbuf = NULL; + va_list ap; + int r; + + va_start(ap, format); + r = vasprintf(&errbuf, format, ap); + va_end(ap); + if (r == -1) + ERR_set_error_data("malloc failed", ERR_TXT_STRING); + else + ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); +} void ERR_add_error_data(int num, ...) { @@ -1080,34 +1094,20 @@ ERR_add_error_data(int num, ...) void ERR_add_error_vdata(int num, va_list args) { - int i, n, s; - char *str, *p, *a; - - s = 80; - str = malloc(s + 1); - if (str == NULL) - return; - str[0] = '\0'; - - n = 0; + char format[129]; + char *errbuf; + format[0] = '\0'; + int i; for (i = 0; i < num; i++) { - a = va_arg(args, char*); - /* ignore NULLs, thanks to Bob Beck */ - if (a != NULL) { - n += strlen(a); - if (n > s) { - s = n + 20; - p = realloc(str, s + 1); - if (p == NULL) { - free(str); - return; - } else - str = p; - } - strlcat(str, a, (size_t)s + 1); + if (strlcat(format, "%s", sizeof(format)) >= sizeof(format)) { + ERR_set_error_data("too many errors", ERR_TXT_STRING); + return; } } - ERR_set_error_data(str, ERR_TXT_MALLOCED|ERR_TXT_STRING); + if (vasprintf(&errbuf, format, args) == -1) + ERR_set_error_data("malloc failed", ERR_TXT_STRING); + else + ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); } int diff --git a/lib/libcrypto/err/err.h b/lib/libcrypto/err/err.h index 87dfef2456c..8facd627119 100644 --- a/lib/libcrypto/err/err.h +++ b/lib/libcrypto/err/err.h @@ -343,6 +343,7 @@ void ERR_print_errors_fp(FILE *fp); #ifndef OPENSSL_NO_BIO void ERR_print_errors(BIO *bp); #endif +void ERR_asprintf_error_data(char * format, ...); void ERR_add_error_data(int num, ...); void ERR_add_error_vdata(int num, va_list args); void ERR_load_strings(int lib, ERR_STRING_DATA str[]); diff --git a/lib/libssl/src/crypto/err/err.c b/lib/libssl/src/crypto/err/err.c index eaafb29101e..ee4597e9b17 100644 --- a/lib/libssl/src/crypto/err/err.c +++ b/lib/libssl/src/crypto/err/err.c @@ -1068,6 +1068,20 @@ ERR_set_error_data(char *data, int flags) es->err_data_flags[i] = flags; } +void +ERR_asprintf_error_data(char * format, ...) { + char *errbuf = NULL; + va_list ap; + int r; + + va_start(ap, format); + r = vasprintf(&errbuf, format, ap); + va_end(ap); + if (r == -1) + ERR_set_error_data("malloc failed", ERR_TXT_STRING); + else + ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); +} void ERR_add_error_data(int num, ...) { @@ -1080,34 +1094,20 @@ ERR_add_error_data(int num, ...) void ERR_add_error_vdata(int num, va_list args) { - int i, n, s; - char *str, *p, *a; - - s = 80; - str = malloc(s + 1); - if (str == NULL) - return; - str[0] = '\0'; - - n = 0; + char format[129]; + char *errbuf; + format[0] = '\0'; + int i; for (i = 0; i < num; i++) { - a = va_arg(args, char*); - /* ignore NULLs, thanks to Bob Beck */ - if (a != NULL) { - n += strlen(a); - if (n > s) { - s = n + 20; - p = realloc(str, s + 1); - if (p == NULL) { - free(str); - return; - } else - str = p; - } - strlcat(str, a, (size_t)s + 1); + if (strlcat(format, "%s", sizeof(format)) >= sizeof(format)) { + ERR_set_error_data("too many errors", ERR_TXT_STRING); + return; } } - ERR_set_error_data(str, ERR_TXT_MALLOCED|ERR_TXT_STRING); + if (vasprintf(&errbuf, format, args) == -1) + ERR_set_error_data("malloc failed", ERR_TXT_STRING); + else + ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); } int diff --git a/lib/libssl/src/crypto/err/err.h b/lib/libssl/src/crypto/err/err.h index 87dfef2456c..8facd627119 100644 --- a/lib/libssl/src/crypto/err/err.h +++ b/lib/libssl/src/crypto/err/err.h @@ -343,6 +343,7 @@ void ERR_print_errors_fp(FILE *fp); #ifndef OPENSSL_NO_BIO void ERR_print_errors(BIO *bp); #endif +void ERR_asprintf_error_data(char * format, ...); void ERR_add_error_data(int num, ...); void ERR_add_error_vdata(int num, va_list args); void ERR_load_strings(int lib, ERR_STRING_DATA str[]);