Reject non-UTF-8 files when reading as str. Close #2918.

This commit is contained in:
Graydon Hoare 2012-07-25 14:58:48 -07:00
parent 62d4f8fe82
commit f8dc9283ad
3 changed files with 10 additions and 1 deletions

View file

@ -687,7 +687,11 @@ fn seek_in_buf(offset: int, pos: uint, len: uint, whence: seek_style) ->
fn read_whole_file_str(file: ~str) -> result<~str, ~str> {
result::chain(read_whole_file(file), |bytes| {
result::ok(str::from_bytes(bytes))
if str::is_utf8(bytes) {
result::ok(str::from_bytes(bytes))
} else {
result::err(file + ~" is not UTF-8")
}
})
}

Binary file not shown.

View file

@ -0,0 +1,5 @@
// error-pattern: is not UTF-8
fn foo() {
#include("not-utf8.bin")
}