Repository navigation
Description
Affects PMD Version: 7.28.0
Description:
With --ignore-identifiers, CPD aborts the whole analysis with a NullPointerException in
JavaCpdLexer$ConstructorDetector.restoreConstructorToken. The crash depends on the order of files, not on any single
file: each file below tokenizes fine on its own, and the analysis passes without --ignore-identifiers.
The likely cause is that CpdAnalysis creates one CpdLexer per analysis, and JavaCpdLexer holds one
ConstructorDetector whose classMembersIndentations stack, storeNextIdentifier flag and prevIdentifier are never
reset between files. A type declaration that one file leaves open on that stack, without a name, is still on top when a
later file reaches a (, and restoreConstructorToken then calls equals on its null name.
We first saw this on the Eclipse Jetty sources (jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-common):
CPD over the full repository fails, and the four files involved were reduced to the three below. Issue #185 reported a
ConstructorDetector NPE with --ignore-identifiers in PMD 5.5.2; that case was fixed in 5.5.3, and this one appears
to be different.
Exception Stacktrace:
java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "java.util.Deque.peek().name" is null
at net.sourceforge.pmd.lang.java.cpd.JavaCpdLexer$ConstructorDetector.restoreConstructorToken(JavaCpdLexer.java:268)
at net.sourceforge.pmd.lang.java.cpd.JavaCpdLexer.processToken(JavaCpdLexer.java:58)
at net.sourceforge.pmd.lang.java.cpd.JavaCpdLexer.processToken(JavaCpdLexer.java:26)
at net.sourceforge.pmd.cpd.impl.CpdLexerBase.tokenize(CpdLexerBase.java:39)
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)
Code Sample demonstrating the issue:
Three files, each a fragment (they need not compile; CPD only tokenizes them).
A.java
return void.class;B.java
class Holder<T> implements Marker
{
}C.java
default void onCreated(Session session)list.txt
A.java
B.java
C.java
Steps to reproduce:
- Save the three files and
list.txtabove in one directory. - Run:
pmd cpd --minimum-tokens 100 --ignore-identifiers --language java --file-list list.txt --debug - CPD fails with the exception above.
Observations:
- Without
--ignore-identifiers, the same command completes. --ignore-literalsis not needed to reproduce.- With only
B.javaandC.java, the command completes. - Each file on its own completes.
Possible fix:
Create the ConstructorDetector per document rather than per lexer, for example when JavaCpdLexer sets up the token
stream for a file, so that no type-declaration state crosses a file boundary. Constructor detection is per type, so this
should not change what a successful run reports.
As a workaround outside PMD, a JavaLanguageModule subclass whose createCpdLexer returns a lexer that creates a fresh
JavaCpdLexer for each tokenize call avoids the crash, and finds the same matches as the shared lexer on input that
does not trigger it.
Running PMD through: CLI (also reproduced through the CpdAnalysis Java API)
Environment: PMD 7.28.0 (pmd-cli and pmd-java from Maven Central), Eclipse Temurin 25.0.4.1, macOS 26.7 on
Apple Silicon; the original failure was on an ubuntu-latest GitHub Actions runner with Temurin 25.
Activity
[-][java] CPD: NullPointerException in JavaCpdLexer$ConstructorDetector with --ignore-identifiers, caused by state carried between files[/-][+][java] CPD: Constructor detection state leaks between files with --ignore-identifiers[/+]