diff mbox

[3/5] parse_size(): check for invalid suffix

Message ID 1350937068-10080-4-git-send-email-kreijack@inwind.com (mailing list archive)
State New, archived
Headers show

Commit Message

Goffredo Baroncelli Oct. 22, 2012, 8:17 p.m. UTC
From: Goffredo Baroncelli <kreijack@inwind.it>

Check that the suffix for the parse_size() input is of only
one character.
---
 utils.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
diff mbox

Patch

diff --git a/utils.c b/utils.c
index 732c782..de75309 100644
--- a/utils.c
+++ b/utils.c
@@ -1226,6 +1226,11 @@  u64 parse_size(char *s)
 	char c;
 	u64 mult = 1;
 
+	if( len <= 0 ){
+		fprintf(stderr, "ERROR: size value is empty\n");
+		exit(50);
+	}
+
 	if (!isdigit(s[len - 1])) {
 		c = tolower(s[len - 1]);
 		switch (c) {
@@ -1242,6 +1247,13 @@  u64 parse_size(char *s)
 			exit(1);
 		}
 		s[len - 1] = '\0';
+		len--;
+	}
+	if( len > 0 && !isdigit(s[len - 1])) {
+		fprintf(stderr, "ERROR: Illegal size value contains "
+			"non-digit character %c in wrong position\n",
+			s[len-1]);
+		exit(51);
 	}
 	return strtoull(s, NULL, 0) * mult;
 }