gethostbyname inet_pton fix

when gethostbyname is provided an IP address in the form of a string, it
is supposed to write the address to h_name; the function instead was
writing an empty string. getaddrinfo does the correct thing by calling
inet_pton and returning on success, and the mistake is in gethostbyname.

The fix is to strlen(result->ai_canonname): If the inet_pton call in
getaddrinfo is successful, ai_canonname will point to valid memory
(non-NULL) but the string will be empty. in this case, the name (which
is an IP address string) will now correctly be copied to h_name of the
hostent structure.
This commit is contained in:
ahgamut 2021-07-04 19:39:57 +05:30
parent 1b9b3864b6
commit 2845e5322f

View file

@ -55,7 +55,7 @@ struct hostent *gethostbyname(const char *name) {
if (getaddrinfo(name, NULL, NULL, &result) || result == NULL) return NULL;
if (ptr0->h_name) free(ptr0->h_name);
if (result->ai_canonname) {
if (result->ai_canonname && strlen(result->ai_canonname) > 0) {
ptr0->h_name = strdup(result->ai_canonname);
} else {
ptr0->h_name = strdup(name);