From fb1bd83e90b0e58a072075d09eb87c7b3681a306 Mon Sep 17 00:00:00 2001 From: ahgamut <41098605+ahgamut@users.noreply.github.com> Date: Sat, 10 Jul 2021 10:24:34 +0530 Subject: [PATCH] updated parseport to use strtoimax strtoimax has an extra parameter endptr to store where the parsing was terminated. endptr is used in parseport to check if the provided string was valid. --- libc/dns/getaddrinfo.c | 7 +++---- libc/dns/prototxt.c | 4 ++-- libc/dns/servicestxt.c | 4 ++-- libc/sock/parseport.c | 16 +++++++++++++--- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/libc/dns/getaddrinfo.c b/libc/dns/getaddrinfo.c index 015cc61d5..0d6865b22 100644 --- a/libc/dns/getaddrinfo.c +++ b/libc/dns/getaddrinfo.c @@ -53,7 +53,7 @@ int getaddrinfo(const char *name, const char *service, if (!name && !service) return EAI_NONAME; if (!name && (hints->ai_flags & AI_CANONNAME)) return EAI_BADFLAGS; - if (service) { + if (service && (port = parseport(service)) == -1) { if (hints->ai_socktype == SOCK_STREAM) strcpy(proto, "tcp"); else if (hints->ai_socktype == SOCK_DGRAM) @@ -62,9 +62,8 @@ int getaddrinfo(const char *name, const char *service, strcpy(proto, ""); if ((port = LookupServicesByName(service, proto, sizeof(proto), NULL, 0, - NULL)) == -1) { - if ((port = parseport(service)) == -1) return EAI_NONAME; - } + NULL)) == -1) + return EAI_NONAME; } if (!(ai = newaddrinfo(port))) return EAI_MEMORY; if (service) ai->ai_addr4->sin_port = htons(port); diff --git a/libc/dns/prototxt.c b/libc/dns/prototxt.c index 5195deeed..e974ab442 100644 --- a/libc/dns/prototxt.c +++ b/libc/dns/prototxt.c @@ -67,7 +67,7 @@ static textwindows noinline char *GetNtProtocolsTxtPath(char *pathbuf, * @param bufsize is the size of buf * @param filepath is the location of the protocols file * (if NULL, uses /etc/protocols) - * @returns 0 on success, -1 on error + * @return 0 on success, -1 on error * * @note aliases are not read from the file. */ @@ -129,7 +129,7 @@ int LookupProtoByNumber(const int protonum, char *buf, size_t bufsize, * @param bufsize is the size of buf * @param filepath is the location of protocols file * (if NULL, uses /etc/protocols) - * @returns -1 on error, or + * @return -1 on error, or * positive protocol number * * @note aliases are read from file for comparison, but not returned. diff --git a/libc/dns/servicestxt.c b/libc/dns/servicestxt.c index 8784fa11d..24742066a 100644 --- a/libc/dns/servicestxt.c +++ b/libc/dns/servicestxt.c @@ -72,7 +72,7 @@ static textwindows noinline char *GetNtServicesTxtPath(char *pathbuf, * @param bufsize is the size of buf * @param filepath is the location of the services file * (if NULL, uses /etc/services) - * @returns 0 on success, -1 on error + * @return 0 on success, -1 on error * * @note aliases are not read from the file. */ @@ -149,7 +149,7 @@ int LookupServicesByPort(const int servport, char *servproto, * @param bufsize is the size of buf * @param filepath is the location of services file * (if NULL, uses /etc/services) - * @returns -1 on error, or + * @return -1 on error, or * positive port number * * @note aliases are read from file for comparison, but not returned. diff --git a/libc/sock/parseport.c b/libc/sock/parseport.c index 0a215b747..4b9d1b65f 100644 --- a/libc/sock/parseport.c +++ b/libc/sock/parseport.c @@ -20,7 +20,17 @@ #include "libc/sock/sock.h" #include "libc/sysv/errfuns.h" -int parseport(const char *service) { - int port = atoi(service); - return (0 <= port && port <= 65535) ? port : einval(); +/* parses string to port number. + * + * @param service is a NULL-terminated string + * @return valid port number or einval() + * + * @see strtoimax + */ +int parseport(const char* service) { + char* end; + int port = strtoimax(service, &end, 0); + if (!service || end == service || *end != '\0' || port < 0 || port > 65535) + return einval(); + return port; }