Skip to content

Commit aa06bf4

Browse files
committed
auto merge of #12357 : chromatic/rust/gh_11976_fail_bounds_check_str, r=alexcrichton
Fixes #11976.
2 parents a886549 + 96102b3 commit aa06bf4

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/libstd/unstable/lang.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
//! Runtime calls emitted by the compiler.
1212
13-
use c_str::ToCStr;
13+
use c_str::CString;
14+
use libc::c_char;
15+
use cast;
16+
use option::Some;
1417

1518
#[cold]
1619
#[lang="fail_"]
@@ -23,7 +26,14 @@ pub fn fail_(expr: *u8, file: *u8, line: uint) -> ! {
2326
pub fn fail_bounds_check(file: *u8, line: uint, index: uint, len: uint) -> ! {
2427
let msg = format!("index out of bounds: the len is {} but the index is {}",
2528
len as uint, index as uint);
26-
msg.with_c_str(|buf| fail_(buf as *u8, file, line))
29+
30+
let file_str = match unsafe { CString::new(file as *c_char, false) }.as_str() {
31+
// This transmute is safe because `file` is always stored in rodata.
32+
Some(s) => unsafe { cast::transmute::<&str, &'static str>(s) },
33+
None => "file wasn't UTF-8 safe"
34+
};
35+
36+
::rt::begin_unwind(msg, file_str, line)
2737
}
2838

2939
#[lang="malloc"]

0 commit comments

Comments
 (0)