Skip to content

Commit 70b6d77

Browse files
committed
fix(parse): Limit frontmatter fences to 255 dashes
Like raw string literals.
1 parent 67094fc commit 70b6d77

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ parse_frontmatter_invalid_opening_preceding_whitespace = invalid preceding white
347347
parse_frontmatter_length_mismatch = frontmatter close does not match the opening
348348
.label_opening = the opening here has {$len_opening} dashes...
349349
.label_close = ...while the close has {$len_close} dashes
350+
parse_frontmatter_too_many_dashes = too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols, but found {$len_opening}
350351
parse_frontmatter_unclosed = unclosed frontmatter
351352
.note = frontmatter opening here was not closed
352353

compiler/rustc_parse/src/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,12 @@ pub(crate) struct FrontmatterLengthMismatch {
822822
pub len_close: usize,
823823
}
824824

825+
#[derive(Diagnostic)]
826+
#[diag(parse_frontmatter_too_many_dashes)]
827+
pub(crate) struct FrontmatterTooManyDashes {
828+
pub len_opening: usize,
829+
}
830+
825831
#[derive(Diagnostic)]
826832
#[diag(parse_leading_plus_not_supported)]
827833
pub(crate) struct LeadingPlusNotSupported {

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,11 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
665665
});
666666
}
667667

668+
// Only up to 255 `-`s are allowed in code fences
669+
if u8::try_from(len_opening).is_err() {
670+
self.dcx().emit_err(errors::FrontmatterTooManyDashes { len_opening });
671+
}
672+
668673
if !rest.trim_matches(is_horizontal_whitespace).is_empty() {
669674
let span = self.mk_sp(last_line_start_pos, self.pos);
670675
self.dcx().emit_err(errors::FrontmatterExtraCharactersAfterClose { span });
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2+
//~? ERROR: too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols
23
// ignore-tidy-linelength
34
[dependencies]
45
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
56

67
#![feature(frontmatter)]
78

8-
//@ check-pass
9-
109
// check that we limit fence lengths
1110

1211
fn main() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols, but found 256
2+
3+
error: aborting due to 1 previous error
4+

0 commit comments

Comments
 (0)