From 631886760203135f6cedb861466604e428217c71 Mon Sep 17 00:00:00 2001 From: Goffredo Baroncelli Date: Mon, 29 Oct 2012 18:53:18 +0100 Subject: [PATCH] parse_size(): replace atoll() with strtoull() Replace the function atoll with strtoull(); Check that the suffix for the parse_size() input is of only one character. Signed-off-by: Goffredo Baroncelli --- utils.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/utils.c b/utils.c index 705be7b..aa2e574 100644 --- a/utils.c +++ b/utils.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -1222,12 +1223,18 @@ scan_again: u64 parse_size(char *s) { - int len = strlen(s); + int i; char c; u64 mult = 1; - if (!isdigit(s[len - 1])) { - c = tolower(s[len - 1]); + for (i=0 ; s[i] && isdigit(s[i]) ; i++) ; + if (!i) { + fprintf(stderr, "ERROR: size value is empty\n"); + exit(50); + } + + if (s[i]) { + c = tolower(s[i]); switch (c) { case 'g': mult *= 1024; @@ -1238,11 +1245,17 @@ u64 parse_size(char *s) case 'b': break; default: - fprintf(stderr, "Unknown size descriptor %c\n", c); + fprintf(stderr, "ERROR: Unknown size descriptor " + "'%c'\n", c); exit(1); } - s[len - 1] = '\0'; } - return atoll(s) * mult; + if (s[i] && s[i+1]) { + fprintf(stderr, "ERROR: Illegal suffix contains " + "character '%c' in wrong position\n", + s[i+1]); + exit(51); + } + return strtoull(s, NULL, 10) * mult; } -- 2.7.4