From 98d172d6d1244463ebc4f94534f6de14f3cac2f1 Mon Sep 17 00:00:00 2001 From: beck Date: Sun, 20 Apr 2014 23:30:12 +0000 Subject: [PATCH] ASN1_STRING cleanup - realloc has handled NULL since I had a mullet and parachute pants - and since it's obvious there is no guarantee the caller doesn't pass in the data area in the argument, use memmove instead of memcpy so overlapping areas are handled correctly. Also, pointers can be usefully printed in hex with %p, in error messaeges rather than the bizzaro stuff that was there using mystical buffer lengths and abuse of strlcpy-converted-blindly-from-strcpy --- lib/libcrypto/asn1/asn1_lib.c | 27 ++++++++++----------------- lib/libssl/src/crypto/asn1/asn1_lib.c | 27 ++++++++++----------------- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/lib/libcrypto/asn1/asn1_lib.c b/lib/libcrypto/asn1/asn1_lib.c index 86cfdd3967d..f3b2f0480fb 100644 --- a/lib/libcrypto/asn1/asn1_lib.c +++ b/lib/libcrypto/asn1/asn1_lib.c @@ -373,7 +373,6 @@ ASN1_STRING_dup(const ASN1_STRING *str) int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) { - unsigned char *c; const char *data = _data; if (len < 0) { @@ -383,24 +382,19 @@ ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) len = strlen(data); } if ((str->length < len) || (str->data == NULL)) { - c = str->data; - if (c == NULL) - str->data = malloc(len + 1); - else - str->data = realloc(c, len + 1); - - if (str->data == NULL) { + unsigned char *tmp; + tmp = realloc(str->data, len + 1); + if (tmp == NULL) { ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE); - str->data = c; return (0); } + str->data = tmp; } str->length = len; if (data != NULL) { - memcpy(str->data, data, len); - /* an allowance for strings :-) */ - str->data[len]='\0'; + memmove(str->data, data, len); } + str->data[str->length]='\0'; return (1); } @@ -465,11 +459,10 @@ ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) void asn1_add_error(const unsigned char *address, int offset) { - char buf1[DECIMAL_SIZE(address) + 1], buf2[DECIMAL_SIZE(offset) + 1]; - - snprintf(buf1, sizeof buf1, "%lu", (unsigned long)address); - snprintf(buf2, sizeof buf2, "%d", offset); - ERR_add_error_data(4, "address=", buf1, " offset=", buf2); + char tmp[128]; + (void) snprintf(tmp, sizeof(tmp), "address=%p offset=%d", + address, offset); + ERR_add_error_data(1, tmp); } int diff --git a/lib/libssl/src/crypto/asn1/asn1_lib.c b/lib/libssl/src/crypto/asn1/asn1_lib.c index 86cfdd3967d..f3b2f0480fb 100644 --- a/lib/libssl/src/crypto/asn1/asn1_lib.c +++ b/lib/libssl/src/crypto/asn1/asn1_lib.c @@ -373,7 +373,6 @@ ASN1_STRING_dup(const ASN1_STRING *str) int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) { - unsigned char *c; const char *data = _data; if (len < 0) { @@ -383,24 +382,19 @@ ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) len = strlen(data); } if ((str->length < len) || (str->data == NULL)) { - c = str->data; - if (c == NULL) - str->data = malloc(len + 1); - else - str->data = realloc(c, len + 1); - - if (str->data == NULL) { + unsigned char *tmp; + tmp = realloc(str->data, len + 1); + if (tmp == NULL) { ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE); - str->data = c; return (0); } + str->data = tmp; } str->length = len; if (data != NULL) { - memcpy(str->data, data, len); - /* an allowance for strings :-) */ - str->data[len]='\0'; + memmove(str->data, data, len); } + str->data[str->length]='\0'; return (1); } @@ -465,11 +459,10 @@ ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) void asn1_add_error(const unsigned char *address, int offset) { - char buf1[DECIMAL_SIZE(address) + 1], buf2[DECIMAL_SIZE(offset) + 1]; - - snprintf(buf1, sizeof buf1, "%lu", (unsigned long)address); - snprintf(buf2, sizeof buf2, "%d", offset); - ERR_add_error_data(4, "address=", buf1, " offset=", buf2); + char tmp[128]; + (void) snprintf(tmp, sizeof(tmp), "address=%p offset=%d", + address, offset); + ERR_add_error_data(1, tmp); } int -- 2.20.1