From d8a22d8bd764af422a5598b745b8f1474d07f400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 8 Jul 2017 19:29:08 +0200 Subject: [PATCH 1/2] style: Fix starts_with_ignore_ascii_case. In particular, fix a panic when the input is not ASCII and we happen to index in something that is not a char boundary. This fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1379380 --- components/style/str.rs | 2 +- tests/unit/style/str.rs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/components/style/str.rs b/components/style/str.rs index 96305a59f1f..70fbe538533 100644 --- a/components/style/str.rs +++ b/components/style/str.rs @@ -149,5 +149,5 @@ pub fn str_join(strs: I, join: &str) -> String /// Returns true if a given string has a given prefix with case-insensitive match. pub fn starts_with_ignore_ascii_case(string: &str, prefix: &str) -> bool { string.len() > prefix.len() && - string[0..prefix.len()].eq_ignore_ascii_case(prefix) + string.as_bytes()[0..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes()) } diff --git a/tests/unit/style/str.rs b/tests/unit/style/str.rs index dafbd8fb7da..439d32a5f10 100644 --- a/tests/unit/style/str.rs +++ b/tests/unit/style/str.rs @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use style::str::{split_html_space_chars, str_join}; +use style::str::{split_html_space_chars, str_join, starts_with_ignore_ascii_case}; #[test] pub fn split_html_space_chars_whitespace() { @@ -33,3 +33,8 @@ pub fn test_str_join_many() { let expected = "-alpha--beta-gamma-"; assert_eq!(actual, expected); } + +#[test] +pub fn test_starts_with_ignore_ascii_case_char_boundary() { + assert!(!starts_with_ignore_ascii_case("aaaaa💩", "-webkit-")); +} From 9e00efc8a57b43bf12022286155e77a1dd043bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 8 Jul 2017 19:41:28 +0200 Subject: [PATCH 2/2] style: Make starts_with_ignore_ascii_case not lie if the strings are the same length. --- components/style/str.rs | 2 +- tests/unit/style/str.rs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/components/style/str.rs b/components/style/str.rs index 70fbe538533..92febb40824 100644 --- a/components/style/str.rs +++ b/components/style/str.rs @@ -148,6 +148,6 @@ pub fn str_join(strs: I, join: &str) -> String /// Returns true if a given string has a given prefix with case-insensitive match. pub fn starts_with_ignore_ascii_case(string: &str, prefix: &str) -> bool { - string.len() > prefix.len() && + string.len() >= prefix.len() && string.as_bytes()[0..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes()) } diff --git a/tests/unit/style/str.rs b/tests/unit/style/str.rs index 439d32a5f10..154cf894158 100644 --- a/tests/unit/style/str.rs +++ b/tests/unit/style/str.rs @@ -34,6 +34,12 @@ pub fn test_str_join_many() { assert_eq!(actual, expected); } +#[test] +pub fn test_starts_with_ignore_ascii_case_basic() { + assert!(starts_with_ignore_ascii_case("-webkit-", "-webkit-")); + assert!(starts_with_ignore_ascii_case("-webkit-foo", "-webkit-")); +} + #[test] pub fn test_starts_with_ignore_ascii_case_char_boundary() { assert!(!starts_with_ignore_ascii_case("aaaaa💩", "-webkit-"));