compiler may optimize away functions with side effects in if statements

XMLWordPrintable

    • Type: Bug
    • Resolution: Duplicate
    • Priority: P4
    • None
    • Affects Version/s: 1.1
    • Component/s: 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.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: