Skip to content

[cpp] CPD: IndexOutOfBoundsException when a duplication is at end of file with UTF8-BOM #6641

Closed

Description

@k-satoda

Affects PMD Version:

PMD 7.24.0

Description:

When CPD finds a duplication at end of file with UTF8-BOM, CPD fails with error like following:

[ERROR] Exception while running CPD.: Invalid range [0, 3[ (length 3) in string of length 2

Exception Stacktrace:

[DEBUG] java.lang.IndexOutOfBoundsException: Invalid range [0, 3[ (length 3) in string of length 2
	at net.sourceforge.pmd.lang.document.Chars.validateRange(Chars.java:533)
	at net.sourceforge.pmd.lang.document.Chars.slice(Chars.java:493)
	at net.sourceforge.pmd.lang.document.Chars.subSequence(Chars.java:457)
	at net.sourceforge.pmd.lang.document.RootTextDocument.sliceOriginalText(RootTextDocument.java:121)
	at net.sourceforge.pmd.cpd.SourceManager.getSlice(SourceManager.java:90)
	at net.sourceforge.pmd.cpd.CPDReport.getSourceCodeSlice(CPDReport.java:61)
	at net.sourceforge.pmd.cpd.SimpleRenderer.renderOn(SimpleRenderer.java:67)
	at net.sourceforge.pmd.cpd.SimpleRenderer.render(SimpleRenderer.java:41)
	at net.sourceforge.pmd.cpd.CpdAnalysis.performAnalysis(CpdAnalysis.java:176)
	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)

Steps to reproduce:

  1. Save this into two files (a.cpp, b.cpp) with UTF-8 with BOM. (The file size will be 5 bytes; 3(BOM) + 1(semicolon) + 1(newline))
;
  1. Run pmd cpd --verbose --minimum-tokens=1 --language=cpp --encoding=UTF-8 --dir=a.cpp --dir=b.cpp

Known workarounds:

  • Add some comments at the end of file.
  • Remove BOM if possible.

Activity

  1. added
    a:bug
    on May 5, 2026
  2. UncleOwen commented on May 5, 2026

    @UncleOwen
    Member

    Can confirm.

    In case anyone wants to work on this, here is a command line to create the input files:

    echo 77u/Owo= | base64 -d | tee a.cpp b.cpp
  3. added this to the 7.26.0 milestone on Jun 5, 2026
  4. added 3 commits that reference this issue on Jun 5, 2026
    419ebaa
    b9f50fa
    09ecf27
  5. added a commit that references this issue on Jun 12, 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