Check for leading zeros on decimal integer literals

Fixes:tint:1124
Change-Id: I6cab684423081889d27b266628089c55918e1f9f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/62320
Auto-Submit: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
This commit is contained in:
Ryan Harrison 2021-08-31 18:00:17 +00:00 committed by Tint LUCI CQ
parent b09723e58b
commit 5ef8c45683
2 changed files with 20 additions and 8 deletions

View File

@ -593,9 +593,19 @@ Token Lexer::try_integer() {
}
auto first = end;
while (end < len_ && is_digit(content_->data[end])) {
end++;
// If the first digit is a zero this must only be zero as leading zeros
// are not allowed.
auto next = first + 1;
if (next < len_) {
if (content_->data[first] == '0' && is_digit(content_->data[next])) {
return {Token::Type::kError, source,
"integer literal (" +
content_->data.substr(start, end - 1 - start) +
"...) has leading 0s"};
}
}
while (end < len_ && is_digit(content_->data[end])) {
auto digits = end - first;
if (digits > kMaxDigits) {
return {Token::Type::kError, source,
@ -603,12 +613,9 @@ Token Lexer::try_integer() {
content_->data.substr(start, end - 1 - start) +
"...) has too many digits"};
}
}
// If the first digit is a zero this must only be zero as leading zeros
// are not allowed.
if (content_->data[first] == '0' && (end - first != 1))
return {};
end++;
}
pos_ = end;
location_.column += (end - start);

View File

@ -407,7 +407,12 @@ TEST_P(IntegerTest_Invalid, Parses) {
}
INSTANTIATE_TEST_SUITE_P(LexerTest,
IntegerTest_Invalid,
testing::Values("2147483648", "4294967296u"));
testing::Values("2147483648",
"4294967296u",
"01234",
"0000",
"-00",
"00u"));
struct TokenData {
const char* input;