Repository navigation
Closed
Description
Affects PMD Version: latest
Make sure, to test with the latest PMD version.
Description:
CPD is giving the following error when an HTML file contains a large script block.
Exception Stacktrace:
Caused by: java.lang.RuntimeException: java.lang.IndexOutOfBoundsException: offset must be in range [0,93872], 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:150)
at net.sourceforge.pmd.cpd.CpdAnalysis.tokenizeFiles(CpdAnalysis.java:225)
at net.sourceforge.pmd.cpd.CpdAnalysis.findMatches(CpdAnalysis.java:198)
at net.sourceforge.pmd.cpd.CpdAnalysis.performAnalysis(CpdAnalysis.java:164)
Code Sample demonstrating the issue:
Run CPD on the attatched HTML file.
NPSP_Robot_Library.html
which is an abbreviated version of https://github.com/SalesforceFoundation/NPSP/blob/main/robot/Cumulus/tests/NPSP_Robot_Library.html
Activity
[-]HtmlCpdLexer giving IndexOutofBoundsException when script is large[/-][+][html] HtmlCpdLexer giving IndexOutofBoundsException when script is large[/+]UncleOwen commentedon Aug 17, 2026
Here's a shorter reproduction:
Produces basically the same stacktrace:
UncleOwen commentedon Aug 17, 2026
This is what I learned today: PMD uses JSoup to parse HTML. To be precise:
Parser.xmlParser(). Which of course cannot parse the script block (the<[CDATA[...]]>is missing.We could try switching to
Parser.htmlParser(), but that breaks all the tests. And probably isn't something we can do in a minor release.[-][html] HtmlCpdLexer giving IndexOutofBoundsException when script is large[/-][+][html] HtmlCpdLexer giving IndexOutofBoundsException when script contains unescaped closing tag[/+][html] Fix #6135: HtmlCpdLexer giving IndexOutOfBoundsException when …
[-][html] HtmlCpdLexer giving IndexOutofBoundsException when script contains unescaped closing tag[/-][+][html] HtmlCpdLexer giving IndexOutOfBoundsException when script contains unescaped closing tag[/+]