changeset 443:41b5ac08208f

Make atolx() error_exit() if fed a string that doesn't convert entirely into an integer.
author Rob Landley <rob@landley.net>
date Thu, 09 Feb 2012 06:09:27 -0600
parents ab2636878ba0
children d3ca5e15e457
files lib/lib.c
diffstat 1 files changed, 7 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Wed Feb 08 19:29:39 2012 -0600
+++ b/lib/lib.c	Thu Feb 09 06:09:27 2012 -0600
@@ -480,14 +480,18 @@
 
 // atol() with the kilo/mega/giga/tera/peta/exa extensions.
 // (zetta and yotta don't fit in 64 bits.)
-long atolx(char *c)
+long atolx(char *numstr)
 {
-	char *suffixes="kmgtpe", *end;
-	long val = strtol(c, &c, 0);
+	char *c, *suffixes="kmgtpe", *end;
+	long val = strtol(numstr, &c, 0);
 
 	if (*c) {
 		end = strchr(suffixes, tolower(*c));
 		if (end) val *= 1024L<<((end-suffixes)*10);
+		else {
+			while (isspace(*c)) c++;
+			if (*c) error_exit("not integer: %s", numstr);
+		}
 	}
 
 	return val;