diff options
author | beck <> | 2014-04-24 15:07:20 +0000 |
---|---|---|
committer | beck <> | 2014-04-24 15:07:20 +0000 |
commit | 24f437ddb1b6ef36ec5b11f24b7b9d5ce1257f1c (patch) | |
tree | d252bc0e88f78d2be56f39cbb6bb4358e0299383 /src/lib | |
parent | 7a428742444c9261fdb1a28c252724134c3e7bef (diff) | |
download | openbsd-24f437ddb1b6ef36ec5b11f24b7b9d5ce1257f1c.tar.gz openbsd-24f437ddb1b6ef36ec5b11f24b7b9d5ce1257f1c.tar.bz2 openbsd-24f437ddb1b6ef36ec5b11f24b7b9d5ce1257f1c.zip |
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@
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/err/err.c | 50 | ||||
-rw-r--r-- | src/lib/libcrypto/err/err.h | 1 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/err/err.c | 50 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/err/err.h | 1 |
4 files changed, 52 insertions, 50 deletions
diff --git a/src/lib/libcrypto/err/err.c b/src/lib/libcrypto/err/err.c index eaafb29101..ee4597e9b1 100644 --- a/src/lib/libcrypto/err/err.c +++ b/src/lib/libcrypto/err/err.c | |||
@@ -1069,6 +1069,20 @@ ERR_set_error_data(char *data, int flags) | |||
1069 | } | 1069 | } |
1070 | 1070 | ||
1071 | void | 1071 | void |
1072 | ERR_asprintf_error_data(char * format, ...) { | ||
1073 | char *errbuf = NULL; | ||
1074 | va_list ap; | ||
1075 | int r; | ||
1076 | |||
1077 | va_start(ap, format); | ||
1078 | r = vasprintf(&errbuf, format, ap); | ||
1079 | va_end(ap); | ||
1080 | if (r == -1) | ||
1081 | ERR_set_error_data("malloc failed", ERR_TXT_STRING); | ||
1082 | else | ||
1083 | ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); | ||
1084 | } | ||
1085 | void | ||
1072 | ERR_add_error_data(int num, ...) | 1086 | ERR_add_error_data(int num, ...) |
1073 | { | 1087 | { |
1074 | va_list args; | 1088 | va_list args; |
@@ -1080,34 +1094,20 @@ ERR_add_error_data(int num, ...) | |||
1080 | void | 1094 | void |
1081 | ERR_add_error_vdata(int num, va_list args) | 1095 | ERR_add_error_vdata(int num, va_list args) |
1082 | { | 1096 | { |
1083 | int i, n, s; | 1097 | char format[129]; |
1084 | char *str, *p, *a; | 1098 | char *errbuf; |
1085 | 1099 | format[0] = '\0'; | |
1086 | s = 80; | 1100 | int i; |
1087 | str = malloc(s + 1); | ||
1088 | if (str == NULL) | ||
1089 | return; | ||
1090 | str[0] = '\0'; | ||
1091 | |||
1092 | n = 0; | ||
1093 | for (i = 0; i < num; i++) { | 1101 | for (i = 0; i < num; i++) { |
1094 | a = va_arg(args, char*); | 1102 | if (strlcat(format, "%s", sizeof(format)) >= sizeof(format)) { |
1095 | /* ignore NULLs, thanks to Bob Beck <beck@obtuse.com> */ | 1103 | ERR_set_error_data("too many errors", ERR_TXT_STRING); |
1096 | if (a != NULL) { | 1104 | return; |
1097 | n += strlen(a); | ||
1098 | if (n > s) { | ||
1099 | s = n + 20; | ||
1100 | p = realloc(str, s + 1); | ||
1101 | if (p == NULL) { | ||
1102 | free(str); | ||
1103 | return; | ||
1104 | } else | ||
1105 | str = p; | ||
1106 | } | ||
1107 | strlcat(str, a, (size_t)s + 1); | ||
1108 | } | 1105 | } |
1109 | } | 1106 | } |
1110 | ERR_set_error_data(str, ERR_TXT_MALLOCED|ERR_TXT_STRING); | 1107 | if (vasprintf(&errbuf, format, args) == -1) |
1108 | ERR_set_error_data("malloc failed", ERR_TXT_STRING); | ||
1109 | else | ||
1110 | ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); | ||
1111 | } | 1111 | } |
1112 | 1112 | ||
1113 | int | 1113 | int |
diff --git a/src/lib/libcrypto/err/err.h b/src/lib/libcrypto/err/err.h index 87dfef2456..8facd62711 100644 --- a/src/lib/libcrypto/err/err.h +++ b/src/lib/libcrypto/err/err.h | |||
@@ -343,6 +343,7 @@ void ERR_print_errors_fp(FILE *fp); | |||
343 | #ifndef OPENSSL_NO_BIO | 343 | #ifndef OPENSSL_NO_BIO |
344 | void ERR_print_errors(BIO *bp); | 344 | void ERR_print_errors(BIO *bp); |
345 | #endif | 345 | #endif |
346 | void ERR_asprintf_error_data(char * format, ...); | ||
346 | void ERR_add_error_data(int num, ...); | 347 | void ERR_add_error_data(int num, ...); |
347 | void ERR_add_error_vdata(int num, va_list args); | 348 | void ERR_add_error_vdata(int num, va_list args); |
348 | void ERR_load_strings(int lib, ERR_STRING_DATA str[]); | 349 | void ERR_load_strings(int lib, ERR_STRING_DATA str[]); |
diff --git a/src/lib/libssl/src/crypto/err/err.c b/src/lib/libssl/src/crypto/err/err.c index eaafb29101..ee4597e9b1 100644 --- a/src/lib/libssl/src/crypto/err/err.c +++ b/src/lib/libssl/src/crypto/err/err.c | |||
@@ -1069,6 +1069,20 @@ ERR_set_error_data(char *data, int flags) | |||
1069 | } | 1069 | } |
1070 | 1070 | ||
1071 | void | 1071 | void |
1072 | ERR_asprintf_error_data(char * format, ...) { | ||
1073 | char *errbuf = NULL; | ||
1074 | va_list ap; | ||
1075 | int r; | ||
1076 | |||
1077 | va_start(ap, format); | ||
1078 | r = vasprintf(&errbuf, format, ap); | ||
1079 | va_end(ap); | ||
1080 | if (r == -1) | ||
1081 | ERR_set_error_data("malloc failed", ERR_TXT_STRING); | ||
1082 | else | ||
1083 | ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); | ||
1084 | } | ||
1085 | void | ||
1072 | ERR_add_error_data(int num, ...) | 1086 | ERR_add_error_data(int num, ...) |
1073 | { | 1087 | { |
1074 | va_list args; | 1088 | va_list args; |
@@ -1080,34 +1094,20 @@ ERR_add_error_data(int num, ...) | |||
1080 | void | 1094 | void |
1081 | ERR_add_error_vdata(int num, va_list args) | 1095 | ERR_add_error_vdata(int num, va_list args) |
1082 | { | 1096 | { |
1083 | int i, n, s; | 1097 | char format[129]; |
1084 | char *str, *p, *a; | 1098 | char *errbuf; |
1085 | 1099 | format[0] = '\0'; | |
1086 | s = 80; | 1100 | int i; |
1087 | str = malloc(s + 1); | ||
1088 | if (str == NULL) | ||
1089 | return; | ||
1090 | str[0] = '\0'; | ||
1091 | |||
1092 | n = 0; | ||
1093 | for (i = 0; i < num; i++) { | 1101 | for (i = 0; i < num; i++) { |
1094 | a = va_arg(args, char*); | 1102 | if (strlcat(format, "%s", sizeof(format)) >= sizeof(format)) { |
1095 | /* ignore NULLs, thanks to Bob Beck <beck@obtuse.com> */ | 1103 | ERR_set_error_data("too many errors", ERR_TXT_STRING); |
1096 | if (a != NULL) { | 1104 | return; |
1097 | n += strlen(a); | ||
1098 | if (n > s) { | ||
1099 | s = n + 20; | ||
1100 | p = realloc(str, s + 1); | ||
1101 | if (p == NULL) { | ||
1102 | free(str); | ||
1103 | return; | ||
1104 | } else | ||
1105 | str = p; | ||
1106 | } | ||
1107 | strlcat(str, a, (size_t)s + 1); | ||
1108 | } | 1105 | } |
1109 | } | 1106 | } |
1110 | ERR_set_error_data(str, ERR_TXT_MALLOCED|ERR_TXT_STRING); | 1107 | if (vasprintf(&errbuf, format, args) == -1) |
1108 | ERR_set_error_data("malloc failed", ERR_TXT_STRING); | ||
1109 | else | ||
1110 | ERR_set_error_data(errbuf, ERR_TXT_MALLOCED|ERR_TXT_STRING); | ||
1111 | } | 1111 | } |
1112 | 1112 | ||
1113 | int | 1113 | int |
diff --git a/src/lib/libssl/src/crypto/err/err.h b/src/lib/libssl/src/crypto/err/err.h index 87dfef2456..8facd62711 100644 --- a/src/lib/libssl/src/crypto/err/err.h +++ b/src/lib/libssl/src/crypto/err/err.h | |||
@@ -343,6 +343,7 @@ void ERR_print_errors_fp(FILE *fp); | |||
343 | #ifndef OPENSSL_NO_BIO | 343 | #ifndef OPENSSL_NO_BIO |
344 | void ERR_print_errors(BIO *bp); | 344 | void ERR_print_errors(BIO *bp); |
345 | #endif | 345 | #endif |
346 | void ERR_asprintf_error_data(char * format, ...); | ||
346 | void ERR_add_error_data(int num, ...); | 347 | void ERR_add_error_data(int num, ...); |
347 | void ERR_add_error_vdata(int num, va_list args); | 348 | void ERR_add_error_vdata(int num, va_list args); |
348 | void ERR_load_strings(int lib, ERR_STRING_DATA str[]); | 349 | void ERR_load_strings(int lib, ERR_STRING_DATA str[]); |