better number parsing (M/K)

This commit is contained in:
Kelvin Sherlock 2013-02-05 22:46:17 -05:00
parent cb17bd3bce
commit 2717ad75c5

View File

@ -278,12 +278,33 @@ bool parse_number(const char *input, uint32_t *dest)
errno = 0;
value = strtol(input, &end, base);
if (errno || value < 0)
if (errno || value < 0 || input == end)
{
fprintf(stderr, "%s - invalid input\n", input);
return false;
}
// M/K
if (*end)
{
int old = value;
if (strcasecmp(end, "M") == 0)
value *= 1024 * 1024;
else if (strcasecmp(end, "K") == 0)
value *= 1024;
else
{
fprintf(stderr, "%s - invalid input\n", input);
return false;
}
if (value < old)
{
// overflow
fprintf(stderr, "%s - invalid input\n", input);
return false;
}
}
if (dest) *dest = value;
return true;
}