Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-2694

break from within try block of try-finally should not always lead to method exit

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 5.1.1, 5.2
    • Fix Version/s: 5.3
    • Component/s: CFG
    • Labels:
      None

      Description

      In the following code, the CFG in badly built (but was correct prior to fix of SONARJAVA-2515).
      When the break is called, from within the try block, we correctly reach the finally block, but from there we should also reach the last throw statement.

      This is currently not the case.

      abstract class A {
      
        public void testCodeWithForLoop() {
          RuntimeException e = null;
      
          for (int i = 0; i < 2;) {
            try {
              e = new RuntimeException(); // FP S1854 (dead store)
              break;
            } finally {
              doSomething();
            }
          }
      
          throw e;
        }
      
        abstract void doSomething();
      }
      

      Prior to the previous fix, we got the followling CFG (B1 being the throw statement):

      After the fix, we got the following CFG (B1 is not reached anymore from the finally block):

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                christophe.zurn Christophe Zurn
                Reporter:
                michael.gumowski Michael Gumowski
              • Votes:
                1 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: