Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Unified Diff: src/wasm/utf8.cc

Issue 1967023004: [wasm] Add UTF-8 validation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: add titzer's comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/wasm/utf8.cc
diff --git a/src/wasm/utf8.cc b/src/wasm/utf8.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1ae58d4e17100639a1f7cc54f183336714d735bc
--- /dev/null
+++ b/src/wasm/utf8.cc
@@ -0,0 +1,42 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/wasm/utf8.h"
+
+#include "src/base/logging.h"
+
+#include "unicode/utf8.h"
+
+namespace {
+
+inline bool IsValidCharacter(uint32_t code_point) {
+ // Excludes non-characters (U+FDD0..U+FDEF, and all codepoints ending in
+ // 0xFFFE or 0xFFFF) from the set of valid code points.
+ return code_point < 0xD800u ||
+ (code_point >= 0xE000u && code_point < 0xFDD0u) ||
+ (code_point > 0xFDEFu && code_point <= 0x10FFFFu &&
+ (code_point & 0xFFFEu) != 0xFFFEu);
+}
+
+} // namespace
+
+namespace v8 {
+namespace internal {
+namespace wasm {
+
+bool IsValidUtf8(const uint8_t *buf, int32_t len) {
+ int32_t char_index = 0;
+ DCHECK(len >= 0);
+
+ while (char_index < len) {
+ int32_t code_point;
+ U8_NEXT(buf, char_index, len, code_point);
+ if (!IsValidCharacter(code_point)) return false;
+ }
+ return true;
+}
+
+} // namespace wasm
+} // namespace internal
+} // namespace v8
« src/wasm/utf8.h ('K') | « src/wasm/utf8.h ('k') | src/wasm/wasm-function-name-table.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698