lib: sync guard for Curl_getaddrinfo_ex() definition and use

`Curl_getaddrinfo_ex()` gets _defined_ with `HAVE_GETADDRINFO` set. But,
`hostip4.c` _used_ it with `HAVE_GETADDRINFO_THREADSAFE` set alone. It
meant a build with the latter, but without the former flag could result
in calling this function but not defining it, and failing to link.

Patch this by adding an extra check for `HAVE_GETATTRINFO` around the
call.

Before this patch, build systems prevented this condition. Now they
don't need to.

While here, simplify the related CMake logic on Windows by setting
`HAVE_GETADDRINFO_THREADSAFE` to the detection result of
`HAVE_GETADDRINFO`. This expresses the following intent clearer than
the previous patch and keeps the logic in a single block of code:
When we have `getaddrinfo()` on Windows, it's always threadsafe.

Follow-up to 67d88626d4

Reviewed-by: Jay Satiro
Closes #9734
This commit is contained in:
Viktor Szakats 2022-11-01 22:40:36 +00:00
parent b563a92cd6
commit edae6c66c7
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
3 changed files with 9 additions and 7 deletions

View File

@ -70,7 +70,6 @@ if(NOT UNIX)
set(HAVE_UTIME 1) set(HAVE_UTIME 1)
set(HAVE_RAND_EGD 0) set(HAVE_RAND_EGD 0)
set(HAVE_GMTIME_R 0) set(HAVE_GMTIME_R 0)
set(HAVE_GETADDRINFO_THREADSAFE 1)
set(HAVE_GETHOSTBYNAME_R 0) set(HAVE_GETHOSTBYNAME_R 0)
set(HAVE_SIGNAL 1) set(HAVE_SIGNAL 1)

View File

@ -1077,8 +1077,8 @@ check_symbol_exists(_strtoi64 "${CURL_INCLUDES}" HAVE__STRTOI64)
check_symbol_exists(strerror_r "${CURL_INCLUDES}" HAVE_STRERROR_R) check_symbol_exists(strerror_r "${CURL_INCLUDES}" HAVE_STRERROR_R)
check_symbol_exists(siginterrupt "${CURL_INCLUDES}" HAVE_SIGINTERRUPT) check_symbol_exists(siginterrupt "${CURL_INCLUDES}" HAVE_SIGINTERRUPT)
check_symbol_exists(getaddrinfo "${CURL_INCLUDES}" HAVE_GETADDRINFO) check_symbol_exists(getaddrinfo "${CURL_INCLUDES}" HAVE_GETADDRINFO)
if(NOT HAVE_GETADDRINFO) if(WIN32)
set(HAVE_GETADDRINFO_THREADSAFE OFF) set(HAVE_GETADDRINFO_THREADSAFE ${HAVE_GETADDRINFO})
endif() endif()
check_symbol_exists(freeaddrinfo "${CURL_INCLUDES}" HAVE_FREEADDRINFO) check_symbol_exists(freeaddrinfo "${CURL_INCLUDES}" HAVE_FREEADDRINFO)
check_symbol_exists(pipe "${CURL_INCLUDES}" HAVE_PIPE) check_symbol_exists(pipe "${CURL_INCLUDES}" HAVE_PIPE)

View File

@ -121,14 +121,15 @@ struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname,
int port) int port)
{ {
#if !defined(HAVE_GETADDRINFO_THREADSAFE) && defined(HAVE_GETHOSTBYNAME_R_3) #if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETADDRINFO_THREADSAFE)) && \
defined(HAVE_GETHOSTBYNAME_R_3)
int res; int res;
#endif #endif
struct Curl_addrinfo *ai = NULL; struct Curl_addrinfo *ai = NULL;
struct hostent *h = NULL; struct hostent *h = NULL;
struct hostent *buf = NULL; struct hostent *buf = NULL;
#if defined(HAVE_GETADDRINFO_THREADSAFE) #if defined(HAVE_GETADDRINFO) && defined(HAVE_GETADDRINFO_THREADSAFE)
struct addrinfo hints; struct addrinfo hints;
char sbuf[12]; char sbuf[12];
char *sbufptr = NULL; char *sbufptr = NULL;
@ -276,14 +277,16 @@ struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname,
h = NULL; /* set return code to NULL */ h = NULL; /* set return code to NULL */
free(buf); free(buf);
} }
#else /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */ #else /* (HAVE_GETADDRINFO && HAVE_GETADDRINFO_THREADSAFE) ||
HAVE_GETHOSTBYNAME_R */
/* /*
* Here is code for platforms that don't have a thread safe * Here is code for platforms that don't have a thread safe
* getaddrinfo() nor gethostbyname_r() function or for which * getaddrinfo() nor gethostbyname_r() function or for which
* gethostbyname() is the preferred one. * gethostbyname() is the preferred one.
*/ */
h = gethostbyname((void *)hostname); h = gethostbyname((void *)hostname);
#endif /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */ #endif /* (HAVE_GETADDRINFO && HAVE_GETADDRINFO_THREADSAFE) ||
HAVE_GETHOSTBYNAME_R */
if(h) { if(h) {
ai = Curl_he2ai(h, port); ai = Curl_he2ai(h, port);