From ecc0f5bc3a61283bdd71654e708c045ae244cd85 Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Mon, 3 Sep 2012 13:44:37 -0400 Subject: [PATCH 1/2] fix str::eq_slice off-by-one error --- 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 b3f58ce2558ff..6466253ef68d0 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -667,7 +667,7 @@ pure fn eq_slice(a: &str, b: &str) -> bool { unsafe { libc::memcmp(ap as *libc::c_void, bp as *libc::c_void, - alen as libc::size_t) == 0 + (alen - 1) as libc::size_t) == 0 } } } From 846e00b98ec0149411ab2e2471fbe3d2f333ecf3 Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Mon, 3 Sep 2012 13:47:10 -0400 Subject: [PATCH 2/2] add test for eq_slice fix --- src/libcore/str.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 6466253ef68d0..0e5f956d866f1 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -2225,6 +2225,13 @@ mod tests { assert (!eq(&~"foo", &~"bar")); } + #[test] + fn test_eq_slice() { + assert (eq_slice(view("foobar", 0, 3), "foo")); + assert (eq_slice(view("barfoo", 3, 6), "foo")); + assert (!eq_slice("foo1", "foo2")); + } + #[test] fn test_le() { assert (le(&"", &""));