gpsd-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gpsd-dev] [PATCH v1 1/1] gps2udp: atoi doesn't set errno. use strtoul t


From: Ferry Huberts
Subject: [gpsd-dev] [PATCH v1 1/1] gps2udp: atoi doesn't set errno. use strtoul to detect errors.
Date: Fri, 3 Jan 2014 21:10:07 +0100

From: Ferry Huberts <address@hidden>

Signed-off-by: Ferry Huberts <address@hidden>
---
 gps2udp.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/gps2udp.c b/gps2udp.c
index 8549a12..6b1ac6c 100644
--- a/gps2udp.c
+++ b/gps2udp.c
@@ -78,6 +78,23 @@ static bool aisonly = false;
 }
 /address@hidden@*/
 
+bool readUL(const char * str, unsigned long * dst) {
+       char * endPtr = NULL;
+       unsigned long value;
+
+       assert(str != NULL);
+       assert(dst != NULL);
+
+       value = strtol(str, &endPtr, 10);
+       if (!((endPtr != str) && (*str != '\0') && (*endPtr == '\0'))) {
+               /* invalid conversion */
+               return false;
+       }
+
+       *dst = value;
+       return true;
+}
+
 static int send_udp (char *nmeastring, size_t ind)
 {
     char message [255];
@@ -138,7 +155,7 @@ static int open_udp(char **hostport)
    {
        char *hostname = NULL;
        char *portname = NULL;
-       int  portnum;
+       unsigned long  portnum;
        struct hostent *hp;
 
        /* parse argument */
@@ -151,8 +168,9 @@ static int open_udp(char **hostport)
           return (-1);
        }
 
-       portnum = atoi(portname);
-       if (errno != 0) {
+       bool validPort = readUL(portname, &portnum);
+       validPort = validPort && portnum > 0 && portnum < 65536;
+       if (!validPort) {
           (void)fprintf(stderr, "gps2udp: syntax is [-u hostname:port] [%s] is 
not a valid port number\n",portname);
           return (-1);
        }
-- 
1.8.4.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]