Skip to content

[java] CommentUtil.multiLinesIn() could lead to StringIndexOutOfBoundsException #1174

@phinehasz

Description

@phinehasz

pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/CommentUtil.java

Affects PMD Version: latest version

Description: I'm working to develop my own comment rule to find all code in comment , then I use CommentUtils. I tried to multiline the comment ,but found StringIndexOutOfBoundsException when testing by a test project. actually, it happens in line 108,when it goes to charAt(0)

if (line.charAt(0) == '*') {
                filteredLines.add(line.substring(1));
                continue;
}

because sometime we make a comment like this

/*
    sometime we use ctrl+shift+/ to make a multiLineComment(not use by /** )
     <--- here, thie line causes a string ""        
 */

In line 3,when using CommentUtil.multiLinesIn(),this variable string line = "",cause there is nothing!
so "".charAt(0) throws StringIndexOutOfBoundsException.
In my code,I add a StringUtil.isEmpty(line) to check it. In my comment rule, every empty string is also useful, because I use it to count beginLineNumber. I don't whether PMD provides a better way to multiLine the multiComment or JavaDocComment? I only find this CommentUtil useful, so I hope you guys to add a empty check beyond line 96 just like following code:

String line = rawLine.trim();
           
           if(StringUtil.isEmpty(line)){
           	continue;
           }
           
           if (line.startsWith("//")) {
               filteredLines.add(line.substring(2));
               continue;
           }
    ...

or we can use if("".equals(line)) to check it.

Metadata

Metadata

Assignees

Labels

a:bugPMD crashes or fails to analyse a file.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions