mirror of
https://github.com/ksherlock/mpw.git
synced 2024-12-22 15:29:45 +00:00
better number parsing (M/K)
This commit is contained in:
parent
cb17bd3bce
commit
2717ad75c5
23
loader.cpp
23
loader.cpp
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user