From 571c3e78bf2aa479c3abdecbd238d6b5381cc788 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Fri, 8 Jan 2021 11:28:53 -0500 Subject: [PATCH] Use gai_strerrorA on Windows. gai_strerror is one of the Windows functions which behaves differently whether UNICODE is defined. See https://docs.microsoft.com/en-us/windows/win32/intl/conventions-for-function-prototypes Call gai_strerrorA so that we behave consistently in both modes. This fixes the build failure in https://chromium-review.googlesource.com/c/chromium/src/+/2613519. It also fixes a type error in the connect BIO (built but not used in Chromium), which wasn't noticed because ERR_add_error_data is a variadic function and untyped. (The type error won't go out of bounds because we're interpreting a NUL-terminated WCHAR* as a NUL-terminated char*. The string will be misinterpreted, but it still will be terminated either at the NUL WCHAR or, more likely, the upper zero byte of the first Latin-1 character in the string.) The ERR_add_error_data call raises the question of which of our char* strings are UTF-8 and which are the POSIX locale / Windows code page (when those are not also UTF-8). This CL doesn't address this and only fixes the character width error. Realistically, calling code tosses char* to printf so often that non-UTF-8 locales are probably a lost cause. (Although right now we do not transform any OS error strings, so tossing them to printf works fine. The outputs of functions like ASN1_STRING_to_UTF8, not so much.) Change-Id: Ie789730658829bde90022605ade2c86b8a65c3de Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/44964 Reviewed-by: Adam Langley Commit-Queue: David Benjamin --- crypto/bio/socket_helper.c | 4 ++++ tool/transport_common.cc | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crypto/bio/socket_helper.c b/crypto/bio/socket_helper.c index d4209d0f9..fc751fd43 100644 --- a/crypto/bio/socket_helper.c +++ b/crypto/bio/socket_helper.c @@ -55,7 +55,11 @@ int bio_ip_and_port_to_socket_and_addr(int *out_sock, ret = getaddrinfo(hostname, port_str, &hint, &result); if (ret != 0) { OPENSSL_PUT_ERROR(SYS, 0); +#if defined(OPENSSL_WINDOWS) + ERR_add_error_data(1, gai_strerrorA(ret)); +#else ERR_add_error_data(1, gai_strerror(ret)); +#endif return 0; } diff --git a/tool/transport_common.cc b/tool/transport_common.cc index 88e91695f..b985221de 100644 --- a/tool/transport_common.cc +++ b/tool/transport_common.cc @@ -156,7 +156,12 @@ bool Connect(int *out_sock, const std::string &hostname_and_port) { int ret = getaddrinfo(hostname.c_str(), port.c_str(), &hint, &result); if (ret != 0) { - fprintf(stderr, "getaddrinfo returned: %s\n", gai_strerror(ret)); +#if defined(OPENSSL_WINDOWS) + const char *error = gai_strerrorA(ret); +#else + const char *error = gai_strerror(ret); +#endif + fprintf(stderr, "getaddrinfo returned: %s\n", error); return false; }