scanner: Stricter strtouint

This helps catch bad version numbers.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
Demi Marie Obenour 2022-11-08 01:50:01 -05:00 committed by Demi Marie Obenour
parent 3dc97759fd
commit 60b34a6fcb

View file

@ -616,20 +616,26 @@ strtouint(const char *str)
{ {
long int ret; long int ret;
char *end; char *end;
int prev_errno = errno; int prev_errno;
if (str[0] == '0')
return str[1] ? -1 : 0;
if (str[0] < '1' || str[0] > '9')
return -1;
prev_errno = errno;
errno = 0; errno = 0;
ret = strtol(str, &end, 10); ret = strtol(str, &end, 10);
if (errno != 0 || end == str || *end != '\0') if (errno != 0 || end == str || *end != '\0') {
return -1; errno = prev_errno;
/* check range */
if (ret < 0 || ret > INT_MAX) {
return -1; return -1;
} }
errno = prev_errno; errno = prev_errno;
return (int)ret;
if (ret <= 0)
abort(); /* caught by syntax checks */
return ret > INT_MAX ? -1 : ret;
} }
/* Check that the provided string will produce valid "C" identifiers. /* Check that the provided string will produce valid "C" identifiers.