RFR: 8272776: NullPointerException not reported

Jan Lahoda jlahoda at openjdk.java.net
Tue Aug 31 07:32:39 UTC 2021


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`.

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

Commit messages:
 - 8272776: NullPointerException not reported

Changes: https://git.openjdk.java.net/jdk/pull/5312/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=5312&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8272776
  Stats: 18 lines in 2 files changed: 14 ins; 1 del; 3 mod
  Patch: https://git.openjdk.java.net/jdk/pull/5312.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/5312/head:pull/5312

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


More information about the compiler-dev mailing list