[ntpsec commit] Use POSIX strtoll/strtoul rather than a homegrown octal/ hex decoder.

Eric S. Raymond esr at ntpsec.org
Fri Oct 23 08:55:18 UTC 2015


Module:    ntpsec
Branch:    master
Commit:    d111c7c55929dabc30e71d5e3efcdf774e2742b5
Changeset: http://git.ntpsec.org/ntpsec/commit/?id=d111c7c55929dabc30e71d5e3efcdf774e2742b5

Author:    Eric S. Raymond <esr at thyrsus.com>
Date:      Fri Oct 23 04:54:16 2015 -0400

Use POSIX strtoll/strtoul rather than a homegrown octal/hex decoder.

---

 libntp/octtoint.c | 36 ------------------------------------
 libntp/wscript    |  1 -
 ntpq/ntpq.c       | 20 ++++++++------------
 3 files changed, 8 insertions(+), 49 deletions(-)

diff --git a/libntp/octtoint.c b/libntp/octtoint.c
deleted file mode 100644
index d18a773..0000000
--- a/libntp/octtoint.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * octtoint - convert an ascii string in octal to an unsigned
- *	      long, with error checking
- */
-#include <config.h>
-#include <stdio.h>
-#include <ctype.h>
-
-#include "ntp_stdlib.h"
-
-bool
-octtoint(
-	const char *str,
-	u_long *ival
-	)
-{
-	register u_long u;
-	register const char *cp;
-
-	cp = str;
-
-	if (*cp == '\0')
-	    return false;
-
-	u = 0;
-	while (*cp != '\0') {
-		if (!isdigit((unsigned char)*cp) || *cp == '8' || *cp == '9')
-		    return 0;
-		if (u >= 0x20000000)
-		    return 0;	/* overflow */
-		u <<= 3;
-		u += *cp++ - '0';	/* ascii dependent */
-	}
-	*ival = u;
-	return true;
-}
diff --git a/libntp/wscript b/libntp/wscript
index 0cdc186..86a1938 100644
--- a/libntp/wscript
+++ b/libntp/wscript
@@ -42,7 +42,6 @@ def build(ctx):
 		"ntp_worker.c",
 		"numtoa.c",
 		"numtohost.c",
-		"octtoint.c",
 		"prettydate.c",
 		"recvbuff.c",
 		"refnumtoa.c",
diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c
index d3182fd..3b80bbc 100644
--- a/ntpq/ntpq.c
+++ b/ntpq/ntpq.c
@@ -2004,12 +2004,10 @@ decodeint(
 	long *val
 	)
 {
-	if (*str == '0') {
-		if (*(str+1) == 'x' || *(str+1) == 'X')
-		    return hextoint(str+2, (u_long *)val);
-		return octtoint(str, (u_long *)val);
-	}
-	return atoint(str, val);
+	errno = 0;
+	/* magic 0 enables hex/octal recognition */
+	*val = strtoll(str, NULL, 0);
+	return !(errno == EINVAL || errno == ERANGE);
 }
 
 
@@ -2022,12 +2020,10 @@ decodeuint(
 	u_long *val
 	)
 {
-	if (*str == '0') {
-		if (*(str + 1) == 'x' || *(str + 1) == 'X')
-			return (hextoint(str + 2, val));
-		return (octtoint(str, val));
-	}
-	return (atouint(str, val));
+	errno = 0;
+	/* magic 0 enables hex/octal recognition */
+	*val = strtoul(str, NULL, 0);
+	return !(errno == EINVAL || errno == ERANGE);
 }
 
 



More information about the vc mailing list