core: Make str::as_bytes handle failure. Closes #2156

This commit is contained in:
Brian Anderson 2012-04-09 18:56:24 -07:00
parent 59abf93b79
commit 10236f8cd4
2 changed files with 20 additions and 4 deletions

View File

@ -1509,10 +1509,8 @@ let i = str::as_bytes(\"Hello World\") { |bytes| vec::len(bytes) };
~~~
"]
fn as_bytes<T>(s: str, f: fn([u8]) -> T) -> T unsafe {
let mut v: [u8] = ::unsafe::reinterpret_cast(s);
let r = f(v);
::unsafe::forget(v);
r
let v: *[u8] = ::unsafe::reinterpret_cast(ptr::addr_of(s));
f(*v)
}
#[doc = "
@ -2449,6 +2447,14 @@ mod tests {
assert (c == "AAA");
}
#[test]
#[ignore(cfg(target_os = "win32"))]
#[should_fail]
fn test_as_bytes_fail() {
// Don't double free
as_bytes("") {|_bytes| fail }
}
#[test]
fn test_as_buf() unsafe {
let a = "Abcdefg";

View File

@ -0,0 +1,10 @@
// error-pattern:explicit failure
// Don't double free the string
use std;
import io::{reader, reader_util};
fn main() {
io::with_str_reader("") { |rdr|
alt rdr.read_char() { '=' { } _ { fail } }
}
}