Skip to content

Commit 2b9cd42

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

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ 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
351+
.label_opening = the opening here has {$len_opening} dashes...
350352
parse_frontmatter_unclosed = unclosed frontmatter
351353
.note = frontmatter opening here was not closed
352354

compiler/rustc_parse/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,16 @@ 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+
#[primary_span]
829+
pub span: Span,
830+
#[label(parse_label_opening)]
831+
pub opening: Span,
832+
pub len_opening: usize,
833+
}
834+
825835
#[derive(Diagnostic)]
826836
#[diag(parse_leading_plus_not_supported)]
827837
pub(crate) struct LeadingPlusNotSupported {

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,13 @@ 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+
let span = self.mk_sp(frontmatter_opening_pos, self.pos);
671+
let opening = self.mk_sp(frontmatter_opening_pos, frontmatter_opening_end_pos);
672+
self.dcx().emit_err(errors::FrontmatterTooManyDashes { span, opening, len_opening });
673+
}
674+
668675
if !rest.trim_matches(is_horizontal_whitespace).is_empty() {
669676
let span = self.mk_sp(last_line_start_pos, self.pos);
670677
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: too many `-` symbols: frontmatter openings may be delimited by up to 255 `-` symbols
2+
--> $DIR/fence-too-many-dashes.rs:1:1
3+
|
4+
LL | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5+
| ^---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6+
| |
7+
| _the opening here has 256 dashes...
8+
| |
9+
LL | |
10+
LL | | [dependencies]
11+
LL | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
12+
| |________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________^
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)