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

S2095: inconsistent behaviour when Closable object is returned or passed as argument

    Details

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

      Description

      Currently in unit test for this rule (RSPEC-2095) there are following lines:

        public void methodDispatch(List<Object> objects) {
          FileInputStream stream = new FileInputStream("myFile"); // Compliant since can be closed in method call
          dispatch(stream);
        }
      
        public InputStream methodReturned(List<Object> objects) {
          FileInputStream stream = new FileInputStream("myFile"); // Compliant since resource is returned (and can be closed elsewhere)
          return stream;
        }
      

      From that we can conclude that in order to avoid FP we don't report on Closable which is passed as argument or returned. And we should not expect anything here to change when call to a Closable is added. Still we have issues in the following code:

        public void methodDispatch(List<Object> objects) {
          FileInputStream stream = new FileInputStream("myFile");  // issue is raised now
          stream.read(); // this line is new compared to previous example
          dispatch(stream);
        }
      
        public InputStream methodReturned(List<Object> objects) {
          FileInputStream stream = new FileInputStream("myFile"); // issue is raised now
          stream.read(); // this line is new compared to previous example
          return stream;
        }
      

      I can't tell if it's FN in first case or FP in second but I believe these 2 set of cases should report consistently.

      Reported in community thread.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              elena.vilchik Elena Vilchik
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: