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

FN on S2095: java.util.Properties.load(InputStream) should not close the stream passed as parameter

    Details

    • Type: False Negative
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.7
    • Component/s: Rules, Symbolic Execution
    • Labels:
      None

      Description

      From https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.InputStream)

      The specified stream remains open after this method returns.

      Therefore, the stream passed in parameter should be at least kept open (and even, marked as open if it is not already open).

      import java.io.File;
      import java.io.FileInputStream;
      import java.io.InputStream;
      import java.util.Properties;
      
      public class PropertyLoader {
      
          public String readProperty(String var) {
      
              InputStream stream = PropertyLoader.class.getResourceAsStream("program.properties");
              Properties properties = new Properties();
      
              try {
                  properties.load(stream); // should not be marked as close and issue should be raised.
      
              } catch(Exception ex) {
              }
      
              return properties.getProperty(var);
          }
      
          public int readInt(String filename) {
      
              int b = 0;
      
              try {
                  InputStream stream = new FileInputStream(new File(filename));
                  b = stream.read();
      
              } catch(Exception ex) {
              }
      
              return b;
          }
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alban.auzeill Alban Auzeill
                Reporter:
                nicolas.peru Nicolas Peru
              • Votes:
                1 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: