about summary refs log tree commit diff
path: root/src/libcollections
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-06-13 17:14:59 -0400
committerGitHub <noreply@github.com>2017-06-13 17:14:59 -0400
commit78d5d3723567e57e82ba65d73d2bc8bbe7f5c017 (patch)
tree4143f38d1c339001b14a967c79c1aea3b3f90178 /src/libcollections
parent02179bd2515fcdc5cdfeecd84b04097f0c80daf2 (diff)
parent808a08a363591abf754fafd93ec3f44c686486ec (diff)
Rollup merge of #42428 - scottmcm:str-get-overflow, r=sfackler
Add overflow checking for `str::get` with inclusive ranges

Fixes https://github.com/rust-lang/rust/issues/42401

Two commits here:

1. The first makes `str::index` just call `SliceIndex<str>::index`.  It's intended to have no behavior change, except where the two methods were inconsistent.
2. The second actually adds the overflow checking to `get(_mut)` (and tests for it)
Diffstat (limited to 'src/libcollections')
-rw-r--r--src/libcollections/tests/lib.rs1
-rw-r--r--src/libcollections/tests/str.rs42
2 files changed, 43 insertions, 0 deletions
diff --git a/src/libcollections/tests/lib.rs b/src/libcollections/tests/lib.rs
index 5f5217b73c2..c6f0b4436bb 100644
--- a/src/libcollections/tests/lib.rs
+++ b/src/libcollections/tests/lib.rs
@@ -24,6 +24,7 @@
 #![feature(repr_align)]
 #![feature(slice_rotate)]
 #![feature(splice)]
+#![feature(str_checked_slicing)]
 #![feature(str_escape)]
 #![feature(test)]
 #![feature(unboxed_closures)]
diff --git a/src/libcollections/tests/str.rs b/src/libcollections/tests/str.rs
index c9b7104fec4..9d8ca38b20e 100644
--- a/src/libcollections/tests/str.rs
+++ b/src/libcollections/tests/str.rs
@@ -358,6 +358,48 @@ fn test_slice_fail() {
     &"中华Việt Nam"[0..2];
 }
 
+#[test]
+#[should_panic]
+fn test_str_slice_rangetoinclusive_max_panics() {
+    &"hello"[...usize::max_value()];
+}
+
+#[test]
+#[should_panic]
+fn test_str_slice_rangeinclusive_max_panics() {
+    &"hello"[1...usize::max_value()];
+}
+
+#[test]
+#[should_panic]
+fn test_str_slicemut_rangetoinclusive_max_panics() {
+    let mut s = "hello".to_owned();
+    let s: &mut str = &mut s;
+    &mut s[...usize::max_value()];
+}
+
+#[test]
+#[should_panic]
+fn test_str_slicemut_rangeinclusive_max_panics() {
+    let mut s = "hello".to_owned();
+    let s: &mut str = &mut s;
+    &mut s[1...usize::max_value()];
+}
+
+#[test]
+fn test_str_get_maxinclusive() {
+    let mut s = "hello".to_owned();
+    {
+        let s: &str = &s;
+        assert_eq!(s.get(...usize::max_value()), None);
+        assert_eq!(s.get(1...usize::max_value()), None);
+    }
+    {
+        let s: &mut str = &mut s;
+        assert_eq!(s.get(...usize::max_value()), None);
+        assert_eq!(s.get(1...usize::max_value()), None);
+    }
+}
 
 #[test]
 fn test_is_char_boundary() {