Skip to content

gai_strerror() is not thread-safe on Windows #15568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions main/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ const struct in6_addr in6addr_any = {0}; /* IN6ADDR_ANY_INIT; */
#endif

#ifdef HAVE_GETADDRINFO
#ifdef HAVE_GAI_STRERROR
# define PHP_GAI_STRERROR(x) (gai_strerror(x))
#else
# define PHP_GAI_STRERROR(x) (php_gai_strerror(x))
# if !defined(PHP_WIN32) && !defined(HAVE_GAI_STRERROR)
/* {{{ php_gai_strerror */
static const char *php_gai_strerror(int code)
{
Expand Down Expand Up @@ -127,7 +124,7 @@ static const char *php_gai_strerror(int code)
return "Unknown error";
}
/* }}} */
#endif
# endif
#endif

/* {{{ php_network_freeaddresses */
Expand Down Expand Up @@ -191,16 +188,26 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka
# endif

if ((n = getaddrinfo(host, NULL, &hints, &res))) {
# if defined(PHP_WIN32)
char *gai_error = php_win32_error_to_msg(n);
# elif defined(HAVE_GAI_STRERROR)
const char *gai_error = gai_strerror(n);
# else
const char *gai_error = php_gai_strerror(n)
# endif
if (error_string) {
/* free error string received during previous iteration (if any) */
if (*error_string) {
zend_string_release_ex(*error_string, 0);
}
*error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo for %s failed: %s", host, PHP_GAI_STRERROR(n));
*error_string = strpprintf(0, "php_network_getaddresses: getaddrinfo for %s failed: %s", host, gai_error);
php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(*error_string));
} else {
php_error_docref(NULL, E_WARNING, "php_network_getaddresses: getaddrinfo for %s failed: %s", host, PHP_GAI_STRERROR(n));
php_error_docref(NULL, E_WARNING, "php_network_getaddresses: getaddrinfo for %s failed: %s", host, gai_error);
}
# if PHP_WIN32
php_win32_error_msg_free(gai_error);
# endif
return 0;
} else if (res == NULL) {
if (error_string) {
Expand Down
6 changes: 1 addition & 5 deletions win32/build/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,9 @@ STDOUT.WriteBlankLines(1);
/* Can we build with IPv6 support? */
ARG_ENABLE("ipv6", "Disable IPv6 support (default is turn it on if available)", "yes");

var main_network_has_ipv6 = 0;
AC_DEFINE('HAVE_GAI_STRERROR', 1);
if (PHP_IPV6 == "yes") {
main_network_has_ipv6 = CHECK_HEADER_ADD_INCLUDE("wspiapi.h", "CFLAGS") ? 1 : 0;
}
if (main_network_has_ipv6) {
STDOUT.WriteLine("Enabling IPv6 support");
AC_DEFINE('HAVE_GAI_STRERROR', 1);
AC_DEFINE('HAVE_IPV6', 1);
}

Expand Down
Loading