Skip to content

[java] java.lang.StackOverflowError while executing a PmdRunnable #5505

@dymul

Description

@dymul

Affects PMD Version:

PMD 7.10.0 (db70a20, 2025-01-31T09:22:09Z)

Description:

Unknown error occurred while executing a PmdRunnable: java.lang.StackOverflowError

Exception Stacktrace:

Exception in thread "main" java.lang.StackOverflowError
        at net.sourceforge.pmd.lang.java.types.SentinelType.acceptVisitor(SentinelType.java:66)
        at net.sourceforge.pmd.lang.java.types.TypeOps.isSameType(TypeOps.java:134)
        at net.sourceforge.pmd.lang.java.types.TypeOps.access$000(TypeOps.java:50)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.typeArgContains(TypeOps.java:769)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitClass(TypeOps.java:854)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitClass(TypeOps.java:586)
        at net.sourceforge.pmd.lang.java.types.JClassType.acceptVisitor(JClassType.java:312)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.isConvertible(TypeOps.java:656)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.isConvertible(TypeOps.java:598)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitTypeVar(TypeOps.java:808)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitTypeVar(TypeOps.java:586)
        at net.sourceforge.pmd.lang.java.types.JTypeVar.acceptVisitor(JTypeVar.java:103)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.isConvertible(TypeOps.java:656)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.isConvertible(TypeOps.java:598)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.typeArgContains(TypeOps.java:788)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitClass(TypeOps.java:854)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitClass(TypeOps.java:586)
        at net.sourceforge.pmd.lang.java.types.JClassType.acceptVisitor(JClassType.java:312)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.isConvertible(TypeOps.java:656)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.isConvertible(TypeOps.java:598)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitTypeVar(TypeOps.java:808)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitTypeVar(TypeOps.java:586)
        at net.sourceforge.pmd.lang.java.types.JTypeVar.acceptVisitor(JTypeVar.java:103)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.isConvertible(TypeOps.java:656)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.isConvertible(TypeOps.java:598)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.typeArgContains(TypeOps.java:788)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitClass(TypeOps.java:854)
        at net.sourceforge.pmd.lang.java.types.TypeOps$SubtypeVisitor.visitClass(TypeOps.java:586)
        at net.sourceforge.pmd.lang.java.types.JClassType.acceptVisitor(JClassType.java:312)
...

Code Sample demonstrating the issue:

package org.example;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

public class Main {

    public static <T extends Comparable<? super T>> Optional<T> getMaxElementCausesStackoverflow(Collection<? extends T> collection) {
        return collection == null || collection.isEmpty() ? Optional.empty() : Optional.of(Collections.max(collection));
    }

    public static <T extends Comparable<? super T>> Optional<T> getMaxElementIsFine(Collection<? extends T> collection) {
        return Optional.ofNullable(collection).filter(c -> !c.isEmpty()).map(Collections::max);
    }
}

Steps to reproduce:

pmd check -d ./Main.java -R rulesets/java/quickstart.xml

Running PMD through: CLI
Java version: 21.0.2, vendor: Amazon.com Inc
OS: macOS 14.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugPMD crashes or fails to analyse a file.in:type-resolutionAffects the type resolution code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions