diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index 7055e60956a9c..8a556a73dbd0b 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -347,6 +347,7 @@ parse_frontmatter_invalid_opening_preceding_whitespace = invalid preceding white parse_frontmatter_length_mismatch = frontmatter close does not match the opening .label_opening = the opening here has {$len_opening} dashes... .label_close = ...while the close has {$len_close} dashes +parse_frontmatter_too_many_dashes = too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols, but found {$len_opening} parse_frontmatter_unclosed = unclosed frontmatter .note = frontmatter opening here was not closed diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 62a333fbf81d7..b0236c3538cc2 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -822,6 +822,12 @@ pub(crate) struct FrontmatterLengthMismatch { pub len_close: usize, } +#[derive(Diagnostic)] +#[diag(parse_frontmatter_too_many_dashes)] +pub(crate) struct FrontmatterTooManyDashes { + pub len_opening: usize, +} + #[derive(Diagnostic)] #[diag(parse_leading_plus_not_supported)] pub(crate) struct LeadingPlusNotSupported { diff --git a/compiler/rustc_parse/src/lexer/mod.rs b/compiler/rustc_parse/src/lexer/mod.rs index 51019db7c00e4..c62c8acced700 100644 --- a/compiler/rustc_parse/src/lexer/mod.rs +++ b/compiler/rustc_parse/src/lexer/mod.rs @@ -665,6 +665,11 @@ impl<'psess, 'src> Lexer<'psess, 'src> { }); } + // Only up to 255 `-`s are allowed in code fences + if u8::try_from(len_opening).is_err() { + self.dcx().emit_err(errors::FrontmatterTooManyDashes { len_opening }); + } + if !rest.trim_matches(is_horizontal_whitespace).is_empty() { let span = self.mk_sp(last_line_start_pos, self.pos); self.dcx().emit_err(errors::FrontmatterExtraCharactersAfterClose { span }); diff --git a/tests/ui/frontmatter/fence-too-many-dashes.rs b/tests/ui/frontmatter/fence-too-many-dashes.rs new file mode 100644 index 0000000000000..abb64748d3d02 --- /dev/null +++ b/tests/ui/frontmatter/fence-too-many-dashes.rs @@ -0,0 +1,11 @@ +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//~? ERROR: too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols +// ignore-tidy-linelength +[dependencies] +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +#![feature(frontmatter)] + +// check that we limit fence lengths + +fn main() {} diff --git a/tests/ui/frontmatter/fence-too-many-dashes.stderr b/tests/ui/frontmatter/fence-too-many-dashes.stderr new file mode 100644 index 0000000000000..09ed9ffa7a421 --- /dev/null +++ b/tests/ui/frontmatter/fence-too-many-dashes.stderr @@ -0,0 +1,4 @@ +error: too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols, but found 256 + +error: aborting due to 1 previous error +