Skip to content

Potential NPE in SelectJdkToolchainMojo.getJdkHome() from unchecked null chain #168

Description

@elharo

Summary

The getJdkHome() method in SelectJdkToolchainMojo has an unchecked null chain that can throw a NullPointerException. The equivalent method in ToolchainMojo correctly guards against nulls.

Location

SelectJdkToolchainMojo.java:264-268

private String getJdkHome(ToolchainPrivate toolchain) {
    return ((Xpp3Dom) toolchain.getModel().getConfiguration())
            .getChild("jdkHome")
            .getValue();
}

Problem

Each call in the chain can return null without a guard:

  1. toolchain.getModel() could return null
  2. getModel().getConfiguration() could return null (causing NPE on the cast)
  3. (Xpp3Dom) cast itself could throw ClassCastException if configuration is not Xpp3Dom
  4. .getChild("jdkHome") could return null
  5. .getValue() on null would throw NPE

Compare with the properly guarded version in ToolchainMojo.

Impact

Can cause NPE during IfSame mode execution, specifically on this line:

&& Objects.equals(getJdkHome(currentJdkToolchain), getJdkHome(toolchain)))

Suggested Fix

Add null guards matching the pattern used in ToolchainMojo, or better yet, reuse that method by extracting it to a shared utility.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions