summaryrefslogtreecommitdiffstats
path: root/library/alloc/tests/str.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/alloc/tests/str.rs')
-rw-r--r--library/alloc/tests/str.rs38
1 files changed, 33 insertions, 5 deletions
diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs
index e30329aa1..4d182be02 100644
--- a/library/alloc/tests/str.rs
+++ b/library/alloc/tests/str.rs
@@ -1590,11 +1590,27 @@ fn test_bool_from_str() {
assert_eq!("not even a boolean".parse::<bool>().ok(), None);
}
-fn check_contains_all_substrings(s: &str) {
- assert!(s.contains(""));
- for i in 0..s.len() {
- for j in i + 1..=s.len() {
- assert!(s.contains(&s[i..j]));
+fn check_contains_all_substrings(haystack: &str) {
+ let mut modified_needle = String::new();
+
+ for i in 0..haystack.len() {
+ // check different haystack lengths since we special-case short haystacks.
+ let haystack = &haystack[0..i];
+ assert!(haystack.contains(""));
+ for j in 0..haystack.len() {
+ for k in j + 1..=haystack.len() {
+ let needle = &haystack[j..k];
+ assert!(haystack.contains(needle));
+ modified_needle.clear();
+ modified_needle.push_str(needle);
+ modified_needle.replace_range(0..1, "\0");
+ assert!(!haystack.contains(&modified_needle));
+
+ modified_needle.clear();
+ modified_needle.push_str(needle);
+ modified_needle.replace_range(needle.len() - 1..needle.len(), "\0");
+ assert!(!haystack.contains(&modified_needle));
+ }
}
}
}
@@ -1616,6 +1632,18 @@ fn strslice_issue_16878() {
}
#[test]
+fn strslice_issue_104726() {
+ // Edge-case in the simd_contains impl.
+ // The first and last byte are the same so it backtracks by one byte
+ // which aligns with the end of the string. Previously incorrect offset calculations
+ // lead to out-of-bounds slicing.
+ #[rustfmt::skip]
+ let needle = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaba";
+ let haystack = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
+ assert!(!haystack.contains(needle));
+}
+
+#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_strslice_contains() {
let x = "There are moments, Jeeves, when one asks oneself, 'Do trousers matter?'";