Skip to content

Commit b6fd615

Browse files
committed
Auto merge of #28932 - barosl:empty-comment, r=alexcrichton
Previously, `/**/` was incorrectly regarded as a doc comment because it starts with `/**` and ends with `*/`. However, this caused an ICE because some code assumed that the length of a doc comment is at least 5. This commit adds an additional check to `is_block_doc_comment` that tests the length of the input. Fixes #28844.
2 parents 87cd2c0 + c7fa52d commit b6fd615

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/libsyntax/parse/lexer/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,8 +1385,9 @@ pub fn is_doc_comment(s: &str) -> bool {
13851385
}
13861386

13871387
pub fn is_block_doc_comment(s: &str) -> bool {
1388-
let res = (s.starts_with("/**") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'*')
1389-
|| s.starts_with("/*!");
1388+
let res = ((s.starts_with("/**") && *s.as_bytes().get(3).unwrap_or(&b' ') != b'*')
1389+
|| s.starts_with("/*!"))
1390+
&& s.len() >= 5; // Prevent `/**/` from being parsed as a doc comment
13901391
debug!("is {:?} a doc comment? {}", s, res);
13911392
res
13921393
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// `/**/` was previously regarded as a doc comment because it starts with `/**` and ends with `*/`.
12+
// This could break some internal logic that assumes the length of a doc comment is at least 5,
13+
// leading to an ICE.
14+
15+
fn main() {
16+
println!(/**/); //~ ERROR unexpected end
17+
}

0 commit comments

Comments
 (0)