Skip to content

Commit 10236f8

Browse files
committed
core: Make str::as_bytes handle failure. Closes #2156
1 parent 59abf93 commit 10236f8

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/libcore/str.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,10 +1509,8 @@ let i = str::as_bytes(\"Hello World\") { |bytes| vec::len(bytes) };
15091509
~~~
15101510
"]
15111511
fn as_bytes<T>(s: str, f: fn([u8]) -> T) -> T unsafe {
1512-
let mut v: [u8] = ::unsafe::reinterpret_cast(s);
1513-
let r = f(v);
1514-
::unsafe::forget(v);
1515-
r
1512+
let v: *[u8] = ::unsafe::reinterpret_cast(ptr::addr_of(s));
1513+
f(*v)
15161514
}
15171515

15181516
#[doc = "
@@ -2449,6 +2447,14 @@ mod tests {
24492447
assert (c == "AAA");
24502448
}
24512449

2450+
#[test]
2451+
#[ignore(cfg(target_os = "win32"))]
2452+
#[should_fail]
2453+
fn test_as_bytes_fail() {
2454+
// Don't double free
2455+
as_bytes("") {|_bytes| fail }
2456+
}
2457+
24522458
#[test]
24532459
fn test_as_buf() unsafe {
24542460
let a = "Abcdefg";

src/test/run-fail/issue-2156.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// error-pattern:explicit failure
2+
// Don't double free the string
3+
use std;
4+
import io::{reader, reader_util};
5+
6+
fn main() {
7+
io::with_str_reader("") { |rdr|
8+
alt rdr.read_char() { '=' { } _ { fail } }
9+
}
10+
}

0 commit comments

Comments
 (0)