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

NullPointerException are not branching to Exception catch block

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 5.1.1
    • Fix Version/s: None
    • Component/s: Rules, Symbolic Execution
    • Labels:
      None

      Description

      Even if a method parameter is not annotated with @Nullable, we could call a method with a null parameter. In such case, any member select on that null object will trigger a NullPointerException.

      If this occur within a try-catch statement, catching Exception, then the engine should not assume that the variable is necessarily not null, as the null-path is still an option till within the body of the catch.

      abstract class A {
      
        public A foo(A a) {
          try {
            a.toString(); // if 'a' is null, it's going to branch to Exception
            return a;
          } catch (MyRunTimeException e) {
            return foo();
          } catch (Exception e) {
            String data = a != null ? a.toString() : null;
      //                  ^^^^^^^^^ FP S2583 condition always true
            System.out.println(data);
            return foo();
          }
        }
      
        abstract void verify() throws MyRunTimeException;
        abstract A foo();
        public static class MyRunTimeException extends RuntimeException {  }
      }
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              michael.gumowski Michael Gumowski
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: