When a number is parsed, the leading one or two characters are used
to indicate whether the number should be interpreted as hexadecimal,
octal or decimal.
But because the parser accepts any digits regardless of the base, it
allows things like 039 to be treated as an octal number, despite '9'
not being a valid digit. The previous commit makes matters even
worse, allowing [a-fA-F] to be accepted for octal or decimal values.
Such errors are caught (but ignored) later when converting the
accepted string into a number in strtoull().
We are already looking at the first character or two to determine
the base, *after* scanning the number. Instead, determine the base
when the first one or two characters are first input, and restrict
which characters are accepted in the number based on that.
As a consequence, strtoul() will examine all of the characters
comprising the number (whereas previously it would stop if it
encountered invalid character for the base).
Finally, accept either "0x" or "0X" to indicate hexadecimal.
This doesn't actually change behavior much, but as long as we're
checking every character in a number for validity we might as well
be restrictive.
Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-25-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>