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

compiler may optimize away functions with side effects in if statements

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1
    • tools
    • None
    • sparc
    • solaris_2.5

      [never 3 dec 96]
      In cases where the compiler can confirm that the result of an || is true it doesn't evaluate the || properly, as in "if (foo() || true)" in which the call to foo is never evaluated. I found this on a web page at http://www.cs.arizona.edu/sumatra/hallofshame/opt-if-bug.html. They give an example but I have a shorter one. The following snippet of code prints test2 instead of throwing an exception.

      class test {
          static boolean foo() throws Exception {
            throw new Exception();
          }

          static public void main(String argv[]) throws Exception {
            if (foo() || true) {
      System.out.println("test2");
            }
          }
      }

      This happens irrespective of whether you pass -g or -O when you compile it.

            wmaddoxsunw William Maddox (Inactive)
            never Tom Rodriguez
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: