From 3cfea33432f223b934bddc207224d90e6a39c280 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Thu, 4 Jan 2018 23:09:02 -0800 Subject: wherein careful doc-decoration arithmetic proves quite the ICE-breaker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This `horizontal_trim` function strips the leading whitespace from doc-comments that have a left-asterisk-margin: /** * You know what I mean— * * comments like this! */ The index of the column of asterisks is `i`, and if trimming is deemed possible, we slice each line from `i+1` to the end of the line. But if, in particular, `i` was 0 _and_ there was an empty line (as in the example given in the reporting issue), we ended up panicking trying to slice an empty string from 0+1 (== 1). Let's tighten our check to say that we can't trim when `i` is even the same as the length of the line, not just when it's greater. (Any such cases would panic trying to slice `line` from `line.len()+1`.) Resolves #47197. --- src/libsyntax/parse/lexer/comments.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index 23449ee69ab..49362f07799 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -101,7 +101,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String { break; } } - if i > line.len() { + if i >= line.len() { can_trim = false; } if !can_trim { -- cgit 1.4.1-3-g733a5