Skip to content

Commit

Permalink
Prevent ICE when calling parse_attribute without an attribute
Browse files Browse the repository at this point in the history
Fixes 5729

`parse_attribute` will panic if the first token is not a `#`. To prevent
this we return early instead of trying to parse an invalid attribute.
  • Loading branch information
ytmimi authored and calebcartwright committed Jun 19, 2023
1 parent a463f23 commit ac2ebd3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/parse/macros/cfg_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ fn parse_cfg_if_inner<'a>(
if !parser.eat_keyword(kw::If) {
return Err("Expected `if`");
}

if !matches!(parser.token.kind, TokenKind::Pound) {
return Err("Failed to parse attributes");
}

// Inner attributes are not actually syntactically permitted here, but we don't
// care about inner vs outer attributes in this position. Our purpose with this
// special case parsing of cfg_if macros is to ensure we can correctly resolve
Expand Down
2 changes: 1 addition & 1 deletion tests/rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn rustfmt_emits_error_on_line_overflow_true() {
#[test]
#[allow(non_snake_case)]
fn dont_emit_ICE() {
let files = ["tests/target/issue_5728.rs"];
let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs"];

for file in files {
let args = [file];
Expand Down
5 changes: 5 additions & 0 deletions tests/target/issue_5729.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cfg_if::cfg_if! {
if {
} else if #(&cpus) {
} else [libc::CTL_HW, libc::HW_NCPU, 0, 0]
}

0 comments on commit ac2ebd3

Please sign in to comment.