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

Vicente Romero vromero at openjdk.org
Wed Dec 14 15:32:59 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.

looks sensible

-------------

Marked as reviewed by vromero (Reviewer).

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


More information about the compiler-dev mailing list