Skip to content

Commit ad30afe

Browse files
authored
Unrolled build for #149321
Rollup merge of #149321 - reddevilmidzy:ice, r=petrochenkov Fix ICE when include_str! reads binary files ICE occurred when an invalid UTF8 file with an absolute path were included. resolve: #149304
2 parents 9050733 + 3f943fb commit ad30afe

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

compiler/rustc_parse/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ pub fn utf8_error<E: EmissionGuarantee>(
130130
};
131131
let contents = String::from_utf8_lossy(contents).to_string();
132132
let source = sm.new_source_file(PathBuf::from(path).into(), contents);
133+
134+
// Avoid out-of-bounds span from lossy UTF-8 conversion.
135+
if start as u32 > source.normalized_source_len.0 {
136+
err.note(note);
137+
return;
138+
}
139+
133140
let span = Span::with_root_ctxt(
134141
source.normalized_byte_pos(start as u32),
135142
source.normalized_byte_pos(start as u32),

src/tools/tidy/src/ui_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ fn check_unexpected_extension(check: &mut RunningCheck, file_path: &Path, ext: &
157157
"tests/ui/crate-loading/auxiliary/libfoo.rlib", // testing loading a manually created rlib
158158
"tests/ui/include-macros/data.bin", // testing including data with the include macros
159159
"tests/ui/include-macros/file.txt", // testing including data with the include macros
160+
"tests/ui/include-macros/invalid-utf8-binary-file.bin", // testing including data with the include macros
160161
"tests/ui/macros/macro-expanded-include/file.txt", // testing including data with the include macros
161162
"tests/ui/macros/not-utf8.bin", // testing including data with the include macros
162163
"tests/ui/macros/syntax-extension-source-utils-files/includeme.fragment", // more include
5 Bytes
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ normalize-stderr: "at byte `\d+`" -> "at byte `$$BYTE`"
2+
//@ normalize-stderr: "`[^`\n]*invalid-utf8-binary-file\.bin`" -> "`$DIR/invalid-utf8-binary-file.bin`"
3+
//@ rustc-env:INVALID_UTF8_BIN={{src-base}}/include-macros/invalid-utf8-binary-file.bin
4+
5+
//! Ensure that ICE does not occur when reading an invalid UTF8 file with an absolute path.
6+
//! regression test for issue <https://github.com/rust-lang/rust/issues/149304>
7+
8+
#![doc = include_str!(concat!(env!("INVALID_UTF8_BIN")))] //~ ERROR: wasn't a utf-8 file
9+
10+
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `/invalid-utf8-binary-file.bin` wasn't a utf-8 file
2+
--> $DIR/invalid-utf8-binary-file.rs:8:10
3+
|
4+
LL | #![doc = include_str!(concat!(env!("INVALID_UTF8_BIN")))]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: invalid utf-8 at byte `$BYTE`
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)