From 2845e5322f191aa735cd1e32c4774a82d3af4bf0 Mon Sep 17 00:00:00 2001 From: ahgamut <41098605+ahgamut@users.noreply.github.com> Date: Sun, 4 Jul 2021 19:39:57 +0530 Subject: [PATCH] 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. --- libc/dns/gethostbyname.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libc/dns/gethostbyname.c b/libc/dns/gethostbyname.c index 27e39a438..0153771ed 100644 --- a/libc/dns/gethostbyname.c +++ b/libc/dns/gethostbyname.c @@ -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);