[jdk20] RFR: 8298727: Trees.getPath may crash for unnamed package
Jan Lahoda
jlahoda at openjdk.org
Wed Dec 14 11:17:45 UTC 2022
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.
-------------
Commit messages:
- 8298727: Trees.getPath may crash for unnamed package
Changes: https://git.openjdk.org/jdk20/pull/31/files
Webrev: https://webrevs.openjdk.org/?repo=jdk20&pr=31&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8298727
Stats: 137 lines in 2 files changed: 136 ins; 0 del; 1 mod
Patch: https://git.openjdk.org/jdk20/pull/31.diff
Fetch: git fetch https://git.openjdk.org/jdk20 pull/31/head:pull/31
PR: https://git.openjdk.org/jdk20/pull/31
More information about the compiler-dev
mailing list