about summary refs log tree commit diff
path: root/compiler/rustc_lexer/src
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-09-23 23:40:29 +0200
committerGitHub <noreply@github.com>2025-09-23 23:40:29 +0200
commitf984966325bbfd81b385a8b8b6d1141059749c96 (patch)
treed37901b53d0ca84eb0b9a15408ded2dad3249261 /compiler/rustc_lexer/src
parent7d0c283bbfbd94df02b7459f4facc67ff560263a (diff)
parent2d18c886f5754258109fad2e1e0c3bcca0cc9fa5 (diff)
downloadrust-f984966325bbfd81b385a8b8b6d1141059749c96.tar.gz
rust-f984966325bbfd81b385a8b8b6d1141059749c96.zip
Rollup merge of #146899 - Teapot4195:issue-146847-fix, r=nnethercote
Fix a crash/mislex when more than one frontmatter closing possibility is considered

When the less fortunate recovery path for frontmatters are taken, if the lexer considers more than one possible frontmatter closing possibility, the current index is entirely mis-tracked and can result in bump_bytes landing in the middle of a multichar unicode character.

This fixes it by tracking the actual base index and updating it as it considers additional closing possibilities.

fixes rust-lang/rust#146847
Diffstat (limited to 'compiler/rustc_lexer/src')
-rw-r--r--compiler/rustc_lexer/src/lib.rs4
1 files changed, 3 insertions, 1 deletions
diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs
index c29ab569f47..f6790f7ed1e 100644
--- a/compiler/rustc_lexer/src/lib.rs
+++ b/compiler/rustc_lexer/src/lib.rs
@@ -599,14 +599,16 @@ impl Cursor<'_> {
             if potential_closing.is_none() {
                 // a less fortunate recovery if all else fails which finds any dashes preceded by whitespace
                 // on a standalone line. Might be wrong.
+                let mut base_index = 0;
                 while let Some(closing) = rest.find("---") {
                     let preceding_chars_start = rest[..closing].rfind("\n").map_or(0, |i| i + 1);
                     if rest[preceding_chars_start..closing].chars().all(is_horizontal_whitespace) {
                         // candidate found
-                        potential_closing = Some(closing);
+                        potential_closing = Some(closing + base_index);
                         break;
                     } else {
                         rest = &rest[closing + 3..];
+                        base_index += closing + 3;
                     }
                 }
             }