[jdk20] Integrated: 8298727: Trees.getPath may crash for unnamed package

Jan Lahoda jlahoda at openjdk.org
Thu Dec 15 11:37:15 UTC 2022

On Wed, 14 Dec 2022 11:08:16 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Considering an empty `package-info.java` file is normally accepted by javac (as a `package-info` for the unnamed module). But, if `Trees.getPath` is used on the unnamed package, a NPE occurs:
> java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.tree.JCTree.accept(com.sun.tools.javac.tree.JCTree$Visitor)" because "tree" is null
> at jdk.compiler/com.sun.tools.javac.tree.TreeInfo.declarationFor(TreeInfo.java:811)
> at jdk.compiler/com.sun.tools.javac.model.JavacElements.getTreeAndTopLevel(JavacElements.java:779)
> at jdk.compiler/com.sun.tools.javac.model.JavacElements.getTreeAndTopLevel(JavacElements.java:799)
> at jdk.compiler/com.sun.tools.javac.api.JavacTrees.getPath(JavacTrees.java:331)
> at jdk.compiler/com.sun.tools.javac.api.JavacTrees.getPath(JavacTrees.java:321)
> The reason is that `Env.tree` is initialized to the package clause in `Enter.visitTopLevel`, but when the package clause is missing, `Env.tree` is `null`, leading to the error above.
> The proposed fix is to set `Env.tree` to the top-level tree in this case, to keep it non-null. As the empty `package-info` can be compiled, it seems reasonable for the model to work on is as well, so a non-null tree needs to be set, and there's no other tree that could be used, I believe.

This pull request has now been integrated.

Changeset: 22a6b591
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.org/jdk20/commit/22a6b5910290cb8a3876f94213ba60db86e60718
Stats:     137 lines in 2 files changed: 136 ins; 0 del; 1 mod

8298727: Trees.getPath may crash for unnamed package

Reviewed-by: vromero


PR: https://git.openjdk.org/jdk20/pull/31

More information about the compiler-dev mailing list