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

conditional operator ?: chooses wrong branch

XMLWordPrintable

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7, 64 bit

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Also reproduced with the following configuration:
      Linux, openSUSE 12.1 64 bit,
       java version " 1.7.0_07 "
       OpenJDK Runtime Environment (IcedTea7 2.3.2) (suse-48.1-x86_64)
       OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)

      The problem may not be reproducible on every system, however it has been confirmed by multiple users and developers.

      A DESCRIPTION OF THE PROBLEM :
      When executing the method " executeTest() " from the following code snippet, the expected output is " test: second " .

      ===========

      private String returnSecond(String first, String second) {
          return alwaysTrue() ? second : first;
      }

      private boolean alwaysTrue() {
          return true;
      }

      private void executeTest() {
          String test = returnSecond(null, " second " );
          System.err.println( " test: " + test);
      }

      ===========

      However, the actual command line output is:

      >>>>>>>>>>>
      [...]
      test: second
      [...]
      test: null
      >>>>>>>>>>>

      I.e. sometimes the result is null and sometimes it is not null. Since the code is independent of any external classes / fields, the result should be the same for each invocation of the test.

      (For reference, the bugreports on our website: http://josm.openstreetmap.de/ticket/7789 and https://josm.openstreetmap.de/ticket/8101)

      REGRESSION. Last worked in version 6u31

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      I cannot provide a small self-contained demonstration of the problem, because the problem is no longer present, when executing the code out of context. It is unknown, what the specific conditions are, that triggers the bug. However, you can compile the complete software (open source) in order to reproduce the issue:

      (1) Download the code: https://nodeload.github.com/bastik/josm/zipball/ternary-operator-issue

      (2) Make sure you have Apache Ant installed and compile the project by calling " ant " from the command line

      (3) Execute the software with " java -jar dist/josm-custom.jar " .

      (4) Download the two files https://josm.openstreetmap.de/raw-attachment/ticket/8101/aaaa.osm and https://josm.openstreetmap.de/raw-attachment/ticket/8101/bb.osm

      (5) Click File > Open..., select the file aaaa.osm from the local file system

      (6) Click File > Open..., select the file bb.osm from the local file system

      -> You will see lines " test: second " and " test: null " on the command line. If you only see " test: second " , then the bug cannot be reproduced on the corresponding system.

      The code in question is in the class org.openstreetmap.josm.gui.mappaint.xml.XmlStyleSource. What is called executeTest() in the code snippet above, is actually called getNode(...) in the code. There is one additional condition that simply makes sure, that the command line is not flooded with output.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      (see description)
      ACTUAL -
      (see description)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      (none)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      (see description)
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Do not use the ternary operator, but if-then-else constructs.

            azeemj Azeem Jiva
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: