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 3 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
27 changes: 17 additions & 10 deletions main/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ PHPAPI void php_network_freeaddresses(struct sockaddr **sal)
}
/* }}} */

#ifdef HAVE_GETADDRINFO
static zend_always_inline report_getaddrinfo_failure(const char *host, int errcode, zend_string **error_string)
{
zend_string *msg = strpprintf(0, "php_network_getaddresses: getaddrinfo for %s failed: %s", host, PHP_GAI_STRERROR(errcode));
if (error_string) {
/* free error string received during previous iteration (if any) */
if (*error_string) {
zend_string_release_ex(*error_string, 0);
}
*error_string = zend_string_copy(msg);
}
php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(msg));
zend_string_release_ex(msg, 0);
}
#endif

/* {{{ php_network_getaddresses
* Returns number of addresses, 0 for none/error
*/
Expand Down Expand Up @@ -191,16 +207,7 @@ PHPAPI int php_network_getaddresses(const char *host, int socktype, struct socka
# endif

if ((n = getaddrinfo(host, NULL, &hints, &res))) {
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));
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));
}
report_getaddrinfo_failure(host, n, error_string);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in the } else if (res == NULL) { below could also call report_getaddrinfo_failure(), but the two legs have slightly different error messages: one shows errno, but not the other. I doubt that showing errno here is correct (since getaddrinfo() has apparently not failed, but I'm not sure. I'm not even sure if that code would ever be executed; isn't res supposed to be set if getaddrinfo() returns 0?

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