Fix assert in MSVC debug builds when parsing unicode

On MSVC debug builds (VS 2022), running Tint against the
"unicode\identifiers.wgsl" test was triggering the following assert:

```
Program: C:\src\tint\out\build\x64-Debug\tint.exe
File: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp
Line: 36

Expression: c >= -1 && c <= 255
```

std::isdigit, isxdigit, and isspace specify that the behaviour is
undefined if the argument's value is neither representable as unsigned
char nor equal to EOF. For example, see:
https://en.cppreference.com/w/cpp/string/byte/isdigit. As suggested on
cppreference, to safely use these functions, we should first convert the
char argument to unsigned char.

Bug: tint:1437
Change-Id: I80e061820cfd87aca51758ae2e3b59306b157b04
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/83180
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano 2022-03-09 18:48:28 +00:00 committed by Tint LUCI CQ
parent 41f8d2ad52
commit b86895da5f
1 changed files with 3 additions and 3 deletions

View File

@ -29,7 +29,7 @@ namespace wgsl {
namespace {
bool is_whitespace(char c) {
return std::isspace(c);
return std::isspace(static_cast<unsigned char>(c));
}
uint32_t dec_value(char c) {
@ -115,11 +115,11 @@ bool Lexer::is_null() const {
}
bool Lexer::is_digit(char ch) const {
return std::isdigit(ch);
return std::isdigit(static_cast<unsigned char>(ch));
}
bool Lexer::is_hex(char ch) const {
return std::isxdigit(ch);
return std::isxdigit(static_cast<unsigned char>(ch));
}
bool Lexer::matches(size_t pos, std::string_view substr) {