Skip to content

[html] CPD throws exception on when HTML file is missing closing tag #5322

@stephen-carter-at-sf

Description

@stephen-carter-at-sf

Affects PMD Version:
7.7.0

Description:

When running CPD with the HTML language, sometimes it throws an exception stopping my execution.
Shouldn't this just be a processing error instead of a full blown exception?

See the stacktrace I'm getting when using the sample file below (which I reduced to be super small to show that it is having issues when there is a missing closing div tag).

Exception Stacktrace:

java.lang.RuntimeException: java.lang.IndexOutOfBoundsException: offset must be in range [0,10723], got -1                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.lang.html.cpd.HtmlCpdLexer.tokenize(HtmlCpdLexer.java:47)                                                                                                                                                                                                                                                                                                                                                                                     
 at net.sourceforge.pmd.cpd.CpdLexer.tokenize(CpdLexer.java:29)                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.cpd.CpdAnalysis.doTokenize(CpdAnalysis.java:146)                                                                                                                                                                                                                                                                                                                                                                                              
 at net.sourceforge.pmd.cpd.CpdAnalysis.performAnalysis(CpdAnalysis.java:172)                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 ... 4 more                                                                                                                                                                                                                                                                                                                                                                                                                                                           
 Caused by: java.lang.IndexOutOfBoundsException: offset must be in range [0,10723], got -1                                                                                                                                                                                                                                                                                                                                                                            
 	at net.sourceforge.pmd.util.AssertionUtil.mustBe(AssertionUtil.java:203)                                                                                                                                                                                                                                                                                                                                                                                             
 at net.sourceforge.pmd.util.AssertionUtil.requireInRange(AssertionUtil.java:193)                                                                                                                                                                                                                                                                                                                                                                                     
 	at net.sourceforge.pmd.util.AssertionUtil.requireInInclusiveRange(AssertionUtil.java:179)                                                                                                                                                                                                                                                                                                                                                                            
 at net.sourceforge.pmd.lang.document.SourceCodePositioner.lineColFromOffset(SourceCodePositioner.java:41)                                                                                                                                                                                                                                                                                                                                                            
 at net.sourceforge.pmd.lang.document.RootTextDocument.toLocation(RootTextDocument.java:69)                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.lang.ast.Node.getReportLocation(Node.java:117)                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.lang.html.cpd.HtmlCpdLexer.traverse(HtmlCpdLexer.java:58)                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.lang.html.cpd.HtmlCpdLexer.traverse(HtmlCpdLexer.java:61)                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.lang.html.cpd.HtmlCpdLexer.traverse(HtmlCpdLexer.java:61)                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.lang.html.cpd.HtmlCpdLexer.traverse(HtmlCpdLexer.java:61)                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.lang.html.cpd.HtmlCpdLexer.traverse(HtmlCpdLexer.java:61)                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 at net.sourceforge.pmd.lang.html.cpd.HtmlCpdLexer.traverse(HtmlCpdLexer.java:61)                                                                                                                                                                                                                                                                                                                                                                                     
 at net.sourceforge.pmd.lang.html.cpd.HtmlCpdLexer.tokenize(HtmlCpdLexer.java:43)                                                                                                                                                                                                                                                                                                                                                                                     
 	... 7 more        

Code Sample demonstrating the issue:

<!doctype html>
<html lang="en">
<body>
<div class='wrapper'>
</body>
</html>
    
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugPMD crashes or fails to analyse a file.in:cpdAffects the copy-paste detector

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions