Skip to content

Commit f8dc928

Browse files
committed
Reject non-UTF-8 files when reading as str. Close #2918.
1 parent 62d4f8f commit f8dc928

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

src/libcore/io.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,11 @@ fn seek_in_buf(offset: int, pos: uint, len: uint, whence: seek_style) ->
687687
688688
fn read_whole_file_str(file: ~str) -> result<~str, ~str> {
689689
result::chain(read_whole_file(file), |bytes| {
690-
result::ok(str::from_bytes(bytes))
690+
if str::is_utf8(bytes) {
691+
result::ok(str::from_bytes(bytes))
692+
} else {
693+
result::err(file + ~" is not UTF-8")
694+
}
691695
})
692696
}
693697

src/test/compile-fail/not-utf8.bin

2.96 KB
Binary file not shown.

src/test/compile-fail/not-utf8.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern: is not UTF-8
2+
3+
fn foo() {
4+
#include("not-utf8.bin")
5+
}

0 commit comments

Comments
 (0)