Skip to content

[html] HtmlCpdLexer giving IndexOutOfBoundsException when script contains unescaped closing tag #6135

Closed

Description

@stephen-carter-at-sf

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

  1. added
    a:bug
    on Oct 13, 2025
  2. changed the title [-]HtmlCpdLexer giving IndexOutofBoundsException when script is large[/-] [+][html] HtmlCpdLexer giving IndexOutofBoundsException when script is large[/+] on May 16, 2026
  3. UncleOwen commented on Aug 17, 2026

    @UncleOwen
    Member

    Here's a shorter reproduction:

    <html>
    <head>
    <script type="text/javascript">
    (function(e, t) {
        function() {
            var t = {
                html5Clone: i !== "<:nav></:nav>",
            };
        };
    }();
    </script>
    </head>
    </html>
    

    Produces basically the same stacktrace:

    [ERROR] Exception while running CPD.: java.lang.IndexOutOfBoundsException: offset must be in range [0,189], got -1
    [DEBUG] java.lang.RuntimeException: java.lang.IndexOutOfBoundsException: offset must be in range [0,189], 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)
            at net.sourceforge.pmd.cli.commands.internal.CpdCommand.doExecute(CpdCommand.java:136)
            at net.sourceforge.pmd.cli.commands.internal.CpdCommand.doExecute(CpdCommand.java:27)
            at net.sourceforge.pmd.cli.internal.PmdRootLogger.executeInLoggingContext(PmdRootLogger.java:55)
            at net.sourceforge.pmd.cli.commands.internal.AbstractAnalysisPmdSubcommand.execute(AbstractAnalysisPmdSubcommand.java:188)
            at net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand.call(AbstractPmdSubcommand.java:30)
            at net.sourceforge.pmd.cli.commands.internal.AbstractPmdSubcommand.call(AbstractPmdSubcommand.java:16)
            at picocli.CommandLine.executeUserObject(CommandLine.java:2031)
            at picocli.CommandLine.access$1500(CommandLine.java:148)
            at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2469)
            at picocli.CommandLine$RunLast.handle(CommandLine.java:2461)
            at picocli.CommandLine$RunLast.handle(CommandLine.java:2423)
            at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2277)
            at picocli.CommandLine$RunLast.execute(CommandLine.java:2425)
            at picocli.CommandLine.execute(CommandLine.java:2174)
            at net.sourceforge.pmd.cli.PmdCli.mainWithoutExit(PmdCli.java:25)
            at net.sourceforge.pmd.cli.PmdCli.main(PmdCli.java:29)
    Caused by: java.lang.IndexOutOfBoundsException: offset must be in range [0,189], got -1
            at net.sourceforge.pmd.util.AssertionUtil.mustBe(AssertionUtil.java:224)
            at net.sourceforge.pmd.util.AssertionUtil.requireInRange(AssertionUtil.java:214)
            at net.sourceforge.pmd.util.AssertionUtil.requireInInclusiveRange(AssertionUtil.java:200)
            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.tokenize(HtmlCpdLexer.java:43)
            ... 21 more
    
  4. UncleOwen commented on Aug 17, 2026

    @UncleOwen
    Member

    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.

  5. changed the title [-][html] HtmlCpdLexer giving IndexOutofBoundsException when script is large[/-] [+][html] HtmlCpdLexer giving IndexOutofBoundsException when script contains unescaped closing tag[/+] on Sep 4, 2026
  6. added this to the 7.28.0 milestone on Sep 9, 2026
  7. added a commit that references this issue on Sep 9, 2026
    9ade26b
  8. changed the title [-][html] HtmlCpdLexer giving IndexOutofBoundsException when script contains unescaped closing tag[/-] [+][html] HtmlCpdLexer giving IndexOutOfBoundsException when script contains unescaped closing tag[/+] on Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions