Integrated: 8272776: NullPointerException not reported

Jan Lahoda jlahoda at openjdk.java.net
Fri Sep 3 09:35:28 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`.

This pull request has now been integrated.

Changeset: 93eec9a1
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.java.net/jdk/commit/93eec9a103de7f4d9a87eac5b295c9a50702ee94
Stats:     18 lines in 2 files changed: 14 ins; 1 del; 3 mod

8272776: NullPointerException not reported

Reviewed-by: vromero

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

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


More information about the compiler-dev mailing list