From 218516255724f6589f6f991f12e3c7c692f0db02 Mon Sep 17 00:00:00 2001 From: "Gary E. Miller" Date: Fri, 3 Jan 2014 11:58:18 -0800 Subject: lastcha2udp: fix test for valid port number Found by: Ferry Huberts atoi() never returns errno set, yet errno was tested for. strtol() does set errno, and does other checks. --- gps2udp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'gps2udp.c') diff --git a/gps2udp.c b/gps2udp.c index 8549a129..bcc5a743 100644 --- a/gps2udp.c +++ b/gps2udp.c @@ -138,6 +138,7 @@ static int open_udp(char **hostport) { char *hostname = NULL; char *portname = NULL; + char *endptr = '\0'; int portnum; struct hostent *hp; @@ -151,8 +152,9 @@ static int open_udp(char **hostport) return (-1); } - portnum = atoi(portname); - if (errno != 0) { + errno = 0; + portnum = strtol(portname, &endptr, 10); + if (0 == portnum || '\0' != *endptr || 0 != errno) { (void)fprintf(stderr, "gps2udp: syntax is [-u hostname:port] [%s] is not a valid port number\n",portname); return (-1); } -- cgit v1.2.1