From e24e9d998838177448222a10d94a18def655178c Mon Sep 17 00:00:00 2001 From: Hajimu UMEMOTO Date: Sun, 23 Jul 2006 15:31:03 +0000 Subject: [PATCH] do not overload the port number on to the return value of str2number(). this could result in an unexpected code path. Obtained from: KAME MFC after: 1 week --- lib/libc/net/getaddrinfo.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/libc/net/getaddrinfo.c b/lib/libc/net/getaddrinfo.c index c7bdfa3b70cf..11742b90aa69 100644 --- a/lib/libc/net/getaddrinfo.c +++ b/lib/libc/net/getaddrinfo.c @@ -226,7 +226,7 @@ typedef union { u_char buf[MAXPACKET]; } querybuf; -static int str2number(const char *); +static int str2number(const char *, int *); static int explore_null(const struct addrinfo *, const char *, struct addrinfo **); static int explore_numeric(const struct addrinfo *, const char *, @@ -341,7 +341,7 @@ freeaddrinfo(struct addrinfo *ai) } static int -str2number(const char *p) +str2number(const char *p, int *portp) { char *ep; unsigned long v; @@ -351,9 +351,10 @@ str2number(const char *p) ep = NULL; errno = 0; v = strtoul(p, &ep, 10); - if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) - return v; - else + if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) { + *portp = v; + return 0; + } else return -1; } @@ -1327,7 +1328,7 @@ get_port(struct addrinfo *ai, const char *servname, int matchonly) { const char *proto; struct servent *sp; - int port; + int port, error; int allownumeric; if (servname == NULL) @@ -1356,8 +1357,8 @@ get_port(struct addrinfo *ai, const char *servname, int matchonly) return EAI_SOCKTYPE; } - port = str2number(servname); - if (port >= 0) { + error = str2number(servname, &port); + if (error == 0) { if (!allownumeric) return EAI_SERVICE; if (port < 0 || port > 65535)