From a4bbb5bab4ea49bc0f4cb9638f27a7eb68e1bdb6 Mon Sep 17 00:00:00 2001 From: nham Date: Mon, 18 Aug 2014 17:51:51 -0400 Subject: [PATCH 1/2] Fix underflow bug in core::str::Searcher::new for haystacks of length < 20 --- src/libcore/str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 095605326c7f5..800e2dcc27893 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -562,7 +562,7 @@ enum Searcher { impl Searcher { fn new(haystack: &[u8], needle: &[u8]) -> Searcher { // FIXME: Tune this. - if needle.len() > haystack.len() - 20 { + if needle.len() + 20 > haystack.len() { Naive(NaiveSearcher::new()) } else { let searcher = TwoWaySearcher::new(needle); From 3b0084e834efb461a72391166a667cedb5581f53 Mon Sep 17 00:00:00 2001 From: nham Date: Mon, 18 Aug 2014 21:43:43 -0400 Subject: [PATCH 2/2] Add a test for the fix of issue 16589 --- src/libcoretest/lib.rs | 1 + src/libcoretest/str.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 src/libcoretest/str.rs diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 3864321586ca6..7866d2f4a111c 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -29,4 +29,5 @@ mod ptr; mod raw; mod result; mod slice; +mod str; mod tuple; diff --git a/src/libcoretest/str.rs b/src/libcoretest/str.rs new file mode 100644 index 0000000000000..bac8d509b1314 --- /dev/null +++ b/src/libcoretest/str.rs @@ -0,0 +1,14 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[test] +fn strslice_issue_16589() { + assert!("bananas".contains("nana")); +}