diff options
author | Thomas Lively <tlively@google.com> | 2024-06-18 20:08:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-19 03:08:25 +0000 |
commit | 765c61445550c6e4ecfd250e1893d776d570b4fd (patch) | |
tree | ab60cbac0ba59fa649d93e9b62a2c0b0b0915a69 /src/support/string.cpp | |
parent | 3acacac34c9ebe949fbc7d9eb3649266760104fe (diff) | |
download | binaryen-765c61445550c6e4ecfd250e1893d776d570b4fd.tar.gz binaryen-765c61445550c6e4ecfd250e1893d776d570b4fd.tar.bz2 binaryen-765c61445550c6e4ecfd250e1893d776d570b4fd.zip |
Validate that names are valid UTF-8 (#6682)
Add an `isUTF8` utility and use it in both the text and binary parsers.
Add missing checks for overlong encodings and overlarge code points in
our WTF8 reader, which the new utility uses. Re-enable the spec tests
that test UTF-8 validation.
Diffstat (limited to 'src/support/string.cpp')
-rw-r--r-- | src/support/string.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/support/string.cpp b/src/support/string.cpp index 31d0e9170..01fe4e522 100644 --- a/src/support/string.cpp +++ b/src/support/string.cpp @@ -195,9 +195,21 @@ std::optional<uint32_t> takeWTF8CodePoint(std::string_view& str) { } str = str.substr(1 + trailingBytes); + if (!valid) { return std::nullopt; } + + size_t expectedTrailing = u < 0x80 ? 0 + : u < 0x800 ? 1 + : u < 0x10000 ? 2 + : u < 0x110000 ? 3 + : -1; + if (trailingBytes != expectedTrailing) { + // Overlong encoding or overlarge code point. + return std::nullopt; + } + return u; } @@ -404,4 +416,14 @@ std::ostream& printEscapedJSON(std::ostream& os, std::string_view str) { return os << '"'; } +bool isUTF8(std::string_view str) { + while (str.size()) { + auto u = takeWTF8CodePoint(str); + if (!u || (0xD800 <= *u && *u < 0xE000)) { + return false; + } + } + return true; +} + } // namespace wasm::String |