Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ pub fn utf8_error<E: EmissionGuarantee>(
};
let contents = String::from_utf8_lossy(contents).to_string();
let source = sm.new_source_file(PathBuf::from(path).into(), contents);

// Avoid out-of-bounds span from lossy UTF-8 conversion.
if start as u32 > source.normalized_source_len.0 {
err.note(note);
return;
}

let span = Span::with_root_ctxt(
source.normalized_byte_pos(start as u32),
source.normalized_byte_pos(start as u32),
Expand Down
1 change: 1 addition & 0 deletions src/tools/tidy/src/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ fn check_unexpected_extension(check: &mut RunningCheck, file_path: &Path, ext: &
"tests/ui/crate-loading/auxiliary/libfoo.rlib", // testing loading a manually created rlib
"tests/ui/include-macros/data.bin", // testing including data with the include macros
"tests/ui/include-macros/file.txt", // testing including data with the include macros
"tests/ui/include-macros/invalid-utf8-binary-file.bin", // testing including data with the include macros
"tests/ui/macros/macro-expanded-include/file.txt", // testing including data with the include macros
"tests/ui/macros/not-utf8.bin", // testing including data with the include macros
"tests/ui/macros/syntax-extension-source-utils-files/includeme.fragment", // more include
Expand Down
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/ui/include-macros/invalid-utf8-binary-file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ normalize-stderr: "at byte `\d+`" -> "at byte `$$BYTE`"
//@ normalize-stderr: "`[^`\n]*invalid-utf8-binary-file\.bin`" -> "`$DIR/invalid-utf8-binary-file.bin`"
//@ rustc-env:INVALID_UTF8_BIN={{src-base}}/include-macros/invalid-utf8-binary-file.bin

//! Ensure that ICE does not occur when reading an invalid UTF8 file with an absolute path.
//! regression test for issue <https://github.com/rust-lang/rust/issues/149304>
#![doc = include_str!(concat!(env!("INVALID_UTF8_BIN")))] //~ ERROR: wasn't a utf-8 file

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/include-macros/invalid-utf8-binary-file.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: `/invalid-utf8-binary-file.bin` wasn't a utf-8 file
--> $DIR/invalid-utf8-binary-file.rs:8:10
|
LL | #![doc = include_str!(concat!(env!("INVALID_UTF8_BIN")))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: invalid utf-8 at byte `$BYTE`

error: aborting due to 1 previous error

Loading