Reject non-UTF-8 files when reading as str. Close #2918.
This commit is contained in:
parent
62d4f8fe82
commit
f8dc9283ad
3 changed files with 10 additions and 1 deletions
|
|
@ -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")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
BIN
src/test/compile-fail/not-utf8.bin
Normal file
BIN
src/test/compile-fail/not-utf8.bin
Normal file
Binary file not shown.
5
src/test/compile-fail/not-utf8.rs
Normal file
5
src/test/compile-fail/not-utf8.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
// error-pattern: is not UTF-8
|
||||
|
||||
fn foo() {
|
||||
#include("not-utf8.bin")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue