From 2a2e9fa56af8cce11803a9d96c650acb6e8852bd Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 17 Jul 2015 08:24:21 -0600 Subject: [PATCH] gethostbyname_r: Fix test of return value from inet_pton(). Zero does not mean success --- libc/net/lib_inetpton.c | 14 +++++--------- libc/netdb/lib_gethostbynamer.c | 25 +++++++++++++++++++++---- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/libc/net/lib_inetpton.c b/libc/net/lib_inetpton.c index f1be56bc5e1..c69af48bc15 100644 --- a/libc/net/lib_inetpton.c +++ b/libc/net/lib_inetpton.c @@ -88,10 +88,8 @@ * address (32 bits for AF_INET, 128 bits for AF_INET6). * * Returned Value: - * The inet_pton() function returns 1 if the conversion succeeds, with the - * address pointed to by dest in network byte order. It will return 0 if the - * input is not a valid IPv4 dotted-decimal string or a valid IPv6 address - * string, or -1 with errno set to EAFNOSUPPOR] if the af argument is unknown. + * inet_ipv4_pton() will returns 1 if the conversion succeeds. It will + * return 0 if the input is not a valid IPv4 dotted-decimal string string. * ****************************************************************************/ @@ -200,10 +198,8 @@ static int inet_ipv4_pton(FAR const char *src, FAR void *dest) * address (32 bits for AF_INET, 128 bits for AF_INET6). * * Returned Value: - * The inet_pton() function returns 1 if the conversion succeeds, with the - * address pointed to by dest in network byte order. It will return 0 if the - * input is not a valid IPv4 dotted-decimal string or a valid IPv6 address - * string, or -1 with errno set to EAFNOSUPPOR] if the af argument is unknown. + * inet_ipv6_pton() will returns 1 if the conversion succeeds. It will + * return 0 if the input is not a valid IPv6 address string. * ****************************************************************************/ @@ -384,7 +380,7 @@ static int inet_ipv6_pton(FAR const char *src, FAR void *dest) * The inet_pton() function returns 1 if the conversion succeeds, with the * address pointed to by dest in network byte order. It will return 0 if the * input is not a valid IPv4 dotted-decimal string or a valid IPv6 address - * string, or -1 with errno set to EAFNOSUPPORT] if the af argument is + * string, or -1 with errno set to EAFNOSUPPORT if the af argument is * unknown. * ****************************************************************************/ diff --git a/libc/netdb/lib_gethostbynamer.c b/libc/netdb/lib_gethostbynamer.c index e90d10b3ccb..b23a5a95050 100644 --- a/libc/netdb/lib_gethostbynamer.c +++ b/libc/netdb/lib_gethostbynamer.c @@ -123,7 +123,9 @@ static int lib_numeric_address(FAR const char *name, FAR struct hostent *host, memset(host, 0, sizeof(struct hostent)); memset(info, 0, sizeof(struct hostent_info_s)); - /* If the address contains a colon, then it might be a numeric IPv6 */ + /* If the address contains a colon, then it might be a numeric IPv6 + * address + */ if (strchr(name, ':') != NULL) { @@ -136,7 +138,13 @@ static int lib_numeric_address(FAR const char *name, FAR struct hostent *host, } ret = inet_pton(AF_INET6, name, ptr); - if (ret < 0) + + /* The inet_pton() function returns 1 if the conversion succeeds. It + * will return 0 if the input is not a valid IP address string, or -1 + * if the address family argument is unsupported. + */ + + if (ret < 1) { /* Conversion failed. Must not be a IPv6 address */ @@ -145,7 +153,10 @@ static int lib_numeric_address(FAR const char *name, FAR struct hostent *host, host->h_addrtype = AF_INET6; } - /* If the address contains a colon, then it might be a numeric IPv6 */ + + /* If the address contains a colon, then it might be a numeric IPv6 + * address. + */ else if (strchr(name, '.') != NULL) { @@ -158,7 +169,13 @@ static int lib_numeric_address(FAR const char *name, FAR struct hostent *host, } ret = inet_pton(AF_INET, name, ptr); - if (ret < 0) + + /* The inet_pton() function returns 1 if the conversion succeeds. It + * will return 0 if the input is not a valid IP address string, or -1 + * if the address family argument is unsupported. + */ + + if (ret < 1) { /* Conversion failed. Must not be an IPv4 address */