RFR: 8272776: NullPointerException not reported

Vicente Romero vromero at openjdk.java.net
Thu Sep 2 15:23:07 UTC 2021

On Tue, 31 Aug 2021 07:25:12 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Code like:
> public void foo(Object o) {
>         switch (o) {
>           default: break;
>           case String s :System.out.println(s); break;
>         } 
> }
> public static void main(String[] args) {
>         (new X()).foo(null); // NPE expected, but prints null
> }
> Should lead to a NPE when executed, because `default` does not cover `null` and there is no total pattern in the switch, but the NPE is not thrown. The reason is that when generating the code, a `hasTotalPattern` flag is used, which means "has a total pattern or default". The code needs to check whether there is a real total pattern (which covers `null`) or not. That is done by looking for the `default` case label, but the code only checks the last case for `default`. It needs to check all cases to differentiate between a switch with a real total pattern and a switch with a `default`.

looks sensible


Marked as reviewed by vromero (Reviewer).

PR: https://git.openjdk.java.net/jdk/pull/5312

More information about the compiler-dev mailing list