Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8292175

Optimization for ternary conditional operator with boxing cause NPE

XMLWordPrintable

    • generic
    • generic
    • Not verified

      ADDITIONAL SYSTEM INFORMATION :
      OS: windows 11 and CentOS 7
      Java 11.0.12

      A DESCRIPTION OF THE PROBLEM :
      When using following ternary conditional operator will throw NPE:

          public static void main(String[] args) {
              BigDecimal a = null;
              BigDecimal b = null;
              Double x = a!=null ?
                      a.doubleValue() : (
                          b!=null ?
                          b.doubleValue() :
                          null
                      );
              System.out.print(x);
          }

      the byte generated byte code as following:
        public static void main(java.lang.String[]);
          Code:
             0: aconst_null
             1: astore_1
             2: aconst_null
             3: astore_2
             4: aload_1
             5: ifnull 15
             8: aload_1
             9: invokevirtual #2 // Method java/math/BigDecimal.doubleValue:()D
            12: goto 33
            15: aload_2
            16: ifnull 29
            19: aload_2
            20: invokevirtual #2 // Method java/math/BigDecimal.doubleValue:()D
            23: invokestatic #3 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
            26: goto 30
            29: aconst_null
            30: invokevirtual #4 // Method java/lang/Double.doubleValue:()D
            33: invokestatic #3 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
            36: astore_3
            37: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
            40: aload_3
            41: invokevirtual #6 // Method java/io/PrintStream.print:(Ljava/lang/Object;)V
            44: return

      as above, see label 30 and 34: hotspot optimize the second ternary conditional operator with type inference assume it was a Double and invoked boxing / unboxing that caused NPE.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
          public static void main(String[] args) {
              BigDecimal a = null;
              BigDecimal b = null;
              Double x = a!=null ?
                      a.doubleValue() : (
                          b!=null ?
                          b.doubleValue() :
                          null
                      );
              System.out.print(x);
          }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      print null
      ACTUAL -
      NullPointException raised

      ---------- BEGIN SOURCE ----------
          public static void main(String[] args) {
              BigDecimal a = null;
              BigDecimal b = null;
              Double x = a!=null ?
                      a.doubleValue() : (
                          b!=null ?
                          b.doubleValue() :
                          null
                      );
              System.out.print(x);
          }
      ---------- END SOURCE ----------

      FREQUENCY : always


            adev Anupam Dev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: