From d576a9b241a21740226cd32dd0c7da06bf32b46a Mon Sep 17 00:00:00 2001 From: The 8472 Date: Tue, 22 Nov 2022 20:54:10 +0100 Subject: [PATCH] add test for issue 104726 --- library/alloc/tests/str.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs index 9689196ef21..4d182be02c9 100644 --- a/library/alloc/tests/str.rs +++ b/library/alloc/tests/str.rs @@ -1631,6 +1631,18 @@ fn strslice_issue_16878() { assert!(!"00abc01234567890123456789abc".contains("bcabc")); } +#[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() {