From 5ef8c456834cf39134980fee42601825d6f30387 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Tue, 31 Aug 2021 18:00:17 +0000 Subject: [PATCH] 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 Reviewed-by: David Neto Kokoro: Kokoro Commit-Queue: Ryan Harrison --- src/reader/wgsl/lexer.cc | 21 ++++++++++++++------- src/reader/wgsl/lexer_test.cc | 7 ++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/reader/wgsl/lexer.cc b/src/reader/wgsl/lexer.cc index e4be0bc09c..74afcf6b66 100644 --- a/src/reader/wgsl/lexer.cc +++ b/src/reader/wgsl/lexer.cc @@ -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); diff --git a/src/reader/wgsl/lexer_test.cc b/src/reader/wgsl/lexer_test.cc index 38482d1dc7..2bc076586a 100644 --- a/src/reader/wgsl/lexer_test.cc +++ b/src/reader/wgsl/lexer_test.cc @@ -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;