Skip to content

Commit 3ce5a02

Browse files
committed
auto merge of #16940 : treeman/rust/fail-non-utf8, r=pnkfelix
Closes #16877.
2 parents b7d456d + 968b128 commit 3ce5a02

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/librustrt/unwind.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use core::prelude::*;
6161

6262
use alloc::boxed::Box;
6363
use collections::string::String;
64+
use collections::str::StrAllocating;
6465
use collections::vec::Vec;
6566
use core::any::Any;
6667
use core::atomic;
@@ -525,7 +526,8 @@ pub fn begin_unwind_fmt(msg: &fmt::Arguments, file_line: &(&'static str, uint))
525526
let mut v = Vec::new();
526527
let _ = write!(&mut VecWriter { v: &mut v }, "{}", msg);
527528

528-
begin_unwind_inner(box String::from_utf8(v).unwrap(), file_line)
529+
let msg = box String::from_utf8_lossy(v.as_slice()).into_string();
530+
begin_unwind_inner(msg, file_line)
529531
}
530532

531533
/// This is the entry point of unwinding for fail!() and assert!().

src/test/run-fail/fail-non-utf8.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
3+
// file at the top-level directory of this distribution and at
4+
// http://rust-lang.org/COPYRIGHT.
5+
//
6+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
// option. This file may not be copied, modified, or distributed
10+
// except according to those terms.
11+
12+
// Previously failed formating invalid utf8.
13+
// cc #16877
14+
15+
// error-pattern:failed at 'hello�'
16+
17+
struct Foo;
18+
impl std::fmt::Show for Foo {
19+
fn fmt(&self, fmtr:&mut std::fmt::Formatter) -> std::fmt::Result {
20+
// Purge invalid utf8: 0xff
21+
fmtr.write(&[104, 101, 108, 108, 111, 0xff])
22+
}
23+
}
24+
fn main() {
25+
fail!("{}", Foo)
26+
}

0 commit comments

Comments
 (0)