From 3f943fbf8451100d5335612a6165bb152ae569a4 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Wed, 26 Nov 2025 01:47:42 +0900 Subject: [PATCH] Fix ICE when include_str! reads absolute path binary files --- compiler/rustc_parse/src/lib.rs | 7 +++++++ src/tools/tidy/src/ui_tests.rs | 1 + .../ui/include-macros/invalid-utf8-binary-file.bin | Bin 0 -> 5 bytes tests/ui/include-macros/invalid-utf8-binary-file.rs | 10 ++++++++++ .../include-macros/invalid-utf8-binary-file.stderr | 10 ++++++++++ 5 files changed, 28 insertions(+) create mode 100644 tests/ui/include-macros/invalid-utf8-binary-file.bin create mode 100644 tests/ui/include-macros/invalid-utf8-binary-file.rs create mode 100644 tests/ui/include-macros/invalid-utf8-binary-file.stderr diff --git a/compiler/rustc_parse/src/lib.rs b/compiler/rustc_parse/src/lib.rs index edec44ca95019..0992bee32f326 100644 --- a/compiler/rustc_parse/src/lib.rs +++ b/compiler/rustc_parse/src/lib.rs @@ -130,6 +130,13 @@ pub fn utf8_error( }; 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), diff --git a/src/tools/tidy/src/ui_tests.rs b/src/tools/tidy/src/ui_tests.rs index 45c2e76d1c1c0..bf51810810a6c 100644 --- a/src/tools/tidy/src/ui_tests.rs +++ b/src/tools/tidy/src/ui_tests.rs @@ -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 diff --git a/tests/ui/include-macros/invalid-utf8-binary-file.bin b/tests/ui/include-macros/invalid-utf8-binary-file.bin new file mode 100644 index 0000000000000000000000000000000000000000..560c5cd2da23727f4f9e7b40d9bd96534390bd38 GIT binary patch literal 5 McmeZ`n!vyX00gK3LI3~& literal 0 HcmV?d00001 diff --git a/tests/ui/include-macros/invalid-utf8-binary-file.rs b/tests/ui/include-macros/invalid-utf8-binary-file.rs new file mode 100644 index 0000000000000..0bbdbac611fd0 --- /dev/null +++ b/tests/ui/include-macros/invalid-utf8-binary-file.rs @@ -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 + +#![doc = include_str!(concat!(env!("INVALID_UTF8_BIN")))] //~ ERROR: wasn't a utf-8 file + +fn main() {} diff --git a/tests/ui/include-macros/invalid-utf8-binary-file.stderr b/tests/ui/include-macros/invalid-utf8-binary-file.stderr new file mode 100644 index 0000000000000..4ac4def1b00a1 --- /dev/null +++ b/tests/ui/include-macros/invalid-utf8-binary-file.stderr @@ -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 +