From 78f0cabb17555333a3577ef8af6169352f838234 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sun, 21 Mar 2021 22:33:06 +0200 Subject: [PATCH] LibJS: Use Utf8View for string prefix checks This commit replaces the usage of String::starts_with with Utf8View::starts_with, which first decodes the utf8 encoded string, and as such can take things like overlong encoded sequences into account (which could otherwise cause the prefix check to be inconsistent with the following code points check). --- Userland/Libraries/LibJS/Runtime/Value.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index a31e760e37..873a456385 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -1213,13 +1213,14 @@ TriState abstract_relation(GlobalObject& global_object, bool left_first, Value l auto x_string = x_primitive.as_string().string(); auto y_string = y_primitive.as_string().string(); - if (x_string.starts_with(y_string)) - return TriState::False; - if (y_string.starts_with(x_string)) - return TriState::True; - Utf8View x_code_points { x_string }; Utf8View y_code_points { y_string }; + + if (x_code_points.starts_with(y_code_points)) + return TriState::False; + if (y_code_points.starts_with(x_code_points)) + return TriState::True; + for (auto k = x_code_points.begin(), l = y_code_points.begin(); k != x_code_points.end() && l != y_code_points.end(); ++k, ++l) {