Skip to content

Commit

Permalink
Reject non-UTF-8 files when reading as str. Close #2918.
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Jul 25, 2012
1 parent 62d4f8f commit f8dc928
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})
}

Expand Down
Binary file added src/test/compile-fail/not-utf8.bin
Binary file not shown.
5 changes: 5 additions & 0 deletions src/test/compile-fail/not-utf8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// error-pattern: is not UTF-8

fn foo() {
#include("not-utf8.bin")
}

0 comments on commit f8dc928

Please sign in to comment.