Skip to content

[java] Parsing failed in ParseLock#doParse(): IndexOutOfBoundsException #5041

Closed

Description

@Calvin-L

Affects PMD Version:

7.1.0

Description:

Parsing failed in ParseLock#doParse()
java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 2

Exception Stacktrace:

[ERROR] Parsing failed in ParseLock#doParse()
java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 2
    at jdk.internal.util.Preconditions.outOfBounds (Preconditions.java:64)
    at jdk.internal.util.Preconditions.outOfBoundsCheckIndex (Preconditions.java:70)
    at jdk.internal.util.Preconditions.checkIndex (Preconditions.java:266)
    at java.util.Objects.checkIndex (Objects.java:361)
    at java.util.ArrayList.get (ArrayList.java:427)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.TypeAnnotationHelper.handleEnclosingType (TypeAnnotationHelper.java:157)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.TypeAnnotationHelper.resolvePathStep (TypeAnnotationHelper.java:100)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.TypeAnnotationHelper.applySinglePath (TypeAnnotationHelper.java:95)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase$LazyMethodType.acceptAnnotationAfterParse (GenericSigBase.java:351)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.TypeAnnotationHelper$TypeAnnotationSetWithReferences.forEach (TypeAnnotationHelper.java:72)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase$LazyMethodType.doParse (GenericSigBase.java:256)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase$1.doParse (GenericSigBase.java:54)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.ParseLock.getFinalStatus (ParseLock.java:33)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.ParseLock.ensureParsed (ParseLock.java:22)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase.ensureParsed (GenericSigBase.java:76)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.GenericSigBase.getTypeParams (GenericSigBase.java:92)
    at net.sourceforge.pmd.lang.java.symbols.internal.asm.ExecutableStub.getTypeParameters (ExecutableStub.java:60)
    at net.sourceforge.pmd.lang.java.types.ClassMethodSigImpl.getTypeParameters (ClassMethodSigImpl.java:127)
    at net.sourceforge.pmd.lang.java.types.JMethodSig.isGeneric (JMethodSig.java:161)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.isPotentiallyApplicable (Infer.java:989)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.computeCompileTimeDecl (Infer.java:277)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.getCompileTimeDecl (Infer.java:254)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.goToInvocationWithFallback (Infer.java:187)
    at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.inferInvocationRecursively (Infer.java:176)
    at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.inferInvocation (PolyResolution.java:254)
    at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.polyTypeOtherCtx (PolyResolution.java:126)
    at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.computePolyType (PolyResolution.java:116)
    at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.handlePoly (LazyTypeResolver.java:267)
    at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:272)
    at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit (LazyTypeResolver.java:95)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor (ASTMethodCall.java:75)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:51)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
    at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror (AbstractJavaTypeNode.java:39)
    at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror (ASTMethodCall.java:22)
    at net.sourceforge.pmd.lang.java.ast.InternalApiBridge.lambda$forceTypeResolutionPhase$0 (InternalApiBridge.java:86)
    at java.util.Iterator.forEachRemaining (Iterator.java:133)
    at net.sourceforge.pmd.lang.ast.internal.IteratorBasedNStream.forEach (IteratorBasedNStream.java:102)
    at net.sourceforge.pmd.lang.java.ast.InternalApiBridge.forceTypeResolutionPhase (InternalApiBridge.java:84)
    at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.lambda$process$3 (JavaAstProcessor.java:134)
    at net.sourceforge.pmd.benchmark.TimeTracker.bench (TimeTracker.java:163)
    at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process (JavaAstProcessor.java:134)
    at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process (JavaAstProcessor.java:164)
    at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process (JavaAstProcessor.java:148)
    at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl (JavaParser.java:69)
    at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl (JavaParser.java:25)
    at net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeParserAdapter.parse (JjtreeParserAdapter.java:36)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.parse (PmdRunnable.java:112)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource (PmdRunnable.java:132)
    at net.sourceforge.pmd.lang.impl.PmdRunnable.run (PmdRunnable.java:80)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:539)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:635)
    at java.lang.Thread.run (Thread.java:840)

Code Sample demonstrating the issue:

This is happening in a large commercial project with many source files. PMD does not give any hints about which file failed to parse, so I don't know how to even begin minimizing the issue. If there is some flag I can enable that will cause PMD to output more information, I'd be happy to spend time putting together a minimal test case.

Steps to reproduce:

See note above---I cannot (yet) provide a minimal reproduction.

  1. mvn clean install

Running PMD through: Maven

MacOS 14.3.1
OpenJDK 17.0.10
Maven-pmd-plugin 3.22.0 using <targetJdk>17</targetJdk>

Activity

  1. added
    a:bug
    on May 24, 2024
  2. likeabantha commented on May 25, 2024

    @likeabantha

    @Calvin-L - Right, we need some data/ testcase to reproduce this scenario.

    https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/asm/TypeAnnotationHelper.java#L156

    selectedTypeIndex value is -1 'cause, enclosingTypes.size() & selectionDepth both these values are coming as 2

    and getting -1th index from list is causing IndexOutOfBoundsException

  3. oowekyala commented on May 25, 2024

    @oowekyala
    Member

    Can you run maven with the -X flag to enable debug output?

  4. Calvin-L commented on May 26, 2024

    @Calvin-L
    Author

    @oowekyala -X shows a few warnings like

    at FILE.java:LINE:COL: Cannot resolve symbol NAME
    

    and

    at FILE.java:LINE:COL: Cannot resolve ambiguous name NAME, treating it as ambiguous
    

    but these may be red herrings; many different files show up in the output but only one IndexOutOfBoundsException exception gets thrown. Also, other modules in this project have similar warnings without any IndexOutOfBoundsException exception.

    (I am on vacation until 6/10, and I plan to investigate further when I get back.)

  5. Calvin-L commented on Jun 11, 2024

    @Calvin-L
    Author

    I haven't had much success minimizing the issue. It seems incredibly brittle; seemingly-irrelevant changes to the source files cause the exception to disappear on me.

    As best I can tell, the problem involves

    • a getter
    • that returns an inner class
    • that has an annotation

    The IndexOutOfBounds exception is thrown when invoking PMD on a file that looks like

    public class Problem {
        public void method(Cls1 x) {
            x.getInner();
        }
    }

    where Cls1 (which is compiled and available on PMD's --aux-classpath) has

    public class Cls1 implements ... {
    
        private final Inner inner = ...;
    
        public @NonNull Inner getInner() {
            return inner;
        }
    
        public class Inner {
        }
    
        // MANY irrelevant-looking details follow that I cannot seem to remove
    }

    Debugging PMD itself, the failing call to handleEnclosingType is given

    • t = pkg.Cls1#Inner
    • path = .
    • i = 0
    • annot = NonNull

    Perhaps the bug is that the while loop does not check path.getLength? The docs don't say anything about what path.getStep() should return when the argument is out of bounds, and in my debugging I observed that

    • path.getLength() == 1
    • path.getStep(0) == TypePath.INNER_TYPE
    • path.getStep(1) == TypePath.INNER_TYPE <-- weird, but it's an out-of-bounds read, so I guess anything is legal output

    Because of what path.getStep(1) returns, the loop continues incrementing selectionDepth higher than it should. Should the loop condition include i + selectionDepth < path.getLength()?

    while (path != null && i + selectionDepth < path.getLength() && path.getStep(i + selectionDepth) == TypePath.INNER_TYPE) {
  6. kelunik commented on Jul 13, 2026

    @kelunik
    Contributor

    @Calvin-L Claude built a nice reproduction case for this, see #6866.

  7. added a commit that references this issue on Jul 16, 2026
    609dd4e
  8. added this to the 7.27.0 milestone on Jul 16, 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