From b57294eff7af0774b207b5e34c3cfcf0aa080a6d Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Wed, 20 Nov 2019 17:47:54 -0600 Subject: [PATCH 1/4] Fix for vasprintf with AIX --- src/ssl.c | 4 +-- wolfssl/wolfcrypt/types.h | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index 32727a0ee..2ae379dc3 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -26,7 +26,7 @@ #include #if defined(OPENSSL_EXTRA) && !defined(_WIN32) - /* turn on GNU extensions for vasprintf with wolfSSL_BIO_printf */ + /* turn on GNU extensions for XVASPRINTF with wolfSSL_BIO_printf */ #undef _GNU_SOURCE #define _GNU_SOURCE #endif @@ -26151,7 +26151,7 @@ int wolfSSL_BIO_printf(WOLFSSL_BIO* bio, const char* format, ...) case WOLFSSL_BIO_SSL: { char* pt = NULL; - ret = vasprintf(&pt, format, args); + ret = XVASPRINTF(&pt, format, args); if (ret > 0 && pt != NULL) { wolfSSL_BIO_write(bio, pt, ret); } diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 16d7bb01e..3d6f1e752 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -493,6 +493,58 @@ #endif /* _MSC_VER || __CYGWIN__ || __MINGW32__ */ #endif /* USE_WINDOWS_API */ + /* XVASPRINTF is used in ssl.c for wolfSSL_BIO_printf */ + #if (defined(sun) || defined(__sun) || defined(_AIX)) + #include + static WC_INLINE + int xvasprintf(char **ret, const char *format, va_list args) + { + int count; + char *buffer; + va_list copy; + va_copy(copy, args); + + *ret = NULL; + + count = vsnprintf(NULL, 0, format, args); + if (count >= 0) + { + buffer = malloc(count + 1); + if (buffer == NULL) + { + count = -1; + } + else + { + count = vsnprintf(buffer, count + 1, format, copy); + if (count < 0) + { + free(buffer); + count = -1; + } + else + { + *ret = buffer; + } + } + } + va_end(copy); + + return count; + } + #define XVASPRINTF xvasprintf + #else + #ifndef XVASPRINTF + #if defined(NO_FILESYSTEM) && (defined(OPENSSL_EXTRA) || \ + defined(HAVE_PKCS7)) && !defined(NO_STDIO_FILESYSTEM) + /* case where stdio is not included else where but is needed + for vasprintf */ + #include + #endif + #define XVASPRINTF vasprintf + #endif + #endif /* defined(sun) || defined(__sun) || defined(_AIX) */ + #if defined(WOLFSSL_CERT_EXT) || defined(HAVE_ALPN) /* use only Thread Safe version of strtok */ #if defined(USE_WOLF_STRTOK) From dc25b79db65c367e84e273060d465b9af20c532b Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Fri, 22 Nov 2019 15:10:13 -0600 Subject: [PATCH 2/4] Fix from review --- wolfssl/wolfcrypt/types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 3d6f1e752..04800a5f3 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -494,9 +494,14 @@ #endif /* USE_WINDOWS_API */ /* XVASPRINTF is used in ssl.c for wolfSSL_BIO_printf */ + #ifdef __GNUC__ + #define VASPRINTFCHECK __attribute__((format(printf, 2, 0))) + #else + #define VASPRINTFCHECK + #endif /* __GNUC__ */ #if (defined(sun) || defined(__sun) || defined(_AIX)) #include - static WC_INLINE + VASPRINTFCHECK static WC_INLINE int xvasprintf(char **ret, const char *format, va_list args) { int count; From 806db8096c86ee3da529573edf48c6e3e2789b25 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Wed, 27 Nov 2019 11:00:28 -0600 Subject: [PATCH 3/4] Replace use of vasprintf --- src/ssl.c | 24 ++++++++++++----- wolfssl/wolfcrypt/types.h | 57 --------------------------------------- 2 files changed, 18 insertions(+), 63 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index 2ae379dc3..145e5a347 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -26150,14 +26150,26 @@ int wolfSSL_BIO_printf(WOLFSSL_BIO* bio, const char* format, ...) #if defined(OPENSSL_EXTRA) && !defined(_WIN32) case WOLFSSL_BIO_SSL: { + int count; char* pt = NULL; - ret = XVASPRINTF(&pt, format, args); - if (ret > 0 && pt != NULL) { - wolfSSL_BIO_write(bio, pt, ret); - } - if (pt != NULL) { - free(pt); + va_list copy; + + va_copy(copy, args); + count = vsnprintf(NULL, 0, format, args); + if (count >= 0) + { + pt = XMALLOC(count + 1, bio->heap, DYNAMIC_TYPE_TMP_BUFFER); + if (pt != NULL) + { + count = vsnprintf(pt, count + 1, format, copy); + if (count >= 0) + { + ret = wolfSSL_BIO_write(bio, pt, count); + } + XFREE(pt, bio->heap, DYNAMIC_TYPE_TMP_BUFFER); + } } + va_end(copy); } break; #endif diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 04800a5f3..16d7bb01e 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -493,63 +493,6 @@ #endif /* _MSC_VER || __CYGWIN__ || __MINGW32__ */ #endif /* USE_WINDOWS_API */ - /* XVASPRINTF is used in ssl.c for wolfSSL_BIO_printf */ - #ifdef __GNUC__ - #define VASPRINTFCHECK __attribute__((format(printf, 2, 0))) - #else - #define VASPRINTFCHECK - #endif /* __GNUC__ */ - #if (defined(sun) || defined(__sun) || defined(_AIX)) - #include - VASPRINTFCHECK static WC_INLINE - int xvasprintf(char **ret, const char *format, va_list args) - { - int count; - char *buffer; - va_list copy; - va_copy(copy, args); - - *ret = NULL; - - count = vsnprintf(NULL, 0, format, args); - if (count >= 0) - { - buffer = malloc(count + 1); - if (buffer == NULL) - { - count = -1; - } - else - { - count = vsnprintf(buffer, count + 1, format, copy); - if (count < 0) - { - free(buffer); - count = -1; - } - else - { - *ret = buffer; - } - } - } - va_end(copy); - - return count; - } - #define XVASPRINTF xvasprintf - #else - #ifndef XVASPRINTF - #if defined(NO_FILESYSTEM) && (defined(OPENSSL_EXTRA) || \ - defined(HAVE_PKCS7)) && !defined(NO_STDIO_FILESYSTEM) - /* case where stdio is not included else where but is needed - for vasprintf */ - #include - #endif - #define XVASPRINTF vasprintf - #endif - #endif /* defined(sun) || defined(__sun) || defined(_AIX) */ - #if defined(WOLFSSL_CERT_EXT) || defined(HAVE_ALPN) /* use only Thread Safe version of strtok */ #if defined(USE_WOLF_STRTOK) From 1026c4359de06f6834319a09bdb8df749b273c6a Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Wed, 27 Nov 2019 14:47:48 -0600 Subject: [PATCH 4/4] Cast XMALLOC --- src/ssl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ssl.c b/src/ssl.c index 145e5a347..24f066663 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -26158,7 +26158,8 @@ int wolfSSL_BIO_printf(WOLFSSL_BIO* bio, const char* format, ...) count = vsnprintf(NULL, 0, format, args); if (count >= 0) { - pt = XMALLOC(count + 1, bio->heap, DYNAMIC_TYPE_TMP_BUFFER); + pt = (char*)XMALLOC(count + 1, bio->heap, + DYNAMIC_TYPE_TMP_BUFFER); if (pt != NULL) { count = vsnprintf(pt, count + 1, format, copy);