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

Modelization of try-catch flow does not take into account method invocations which can not throw errors or runtime exceptions

    Details

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

      Description

      FPs are potentially raised when dealing with runtime exception or errors thrown by called method.

      In the following example, the constructor of the BufferedInputStream will throw an IllegalArgumentException with a buffer size <= 0. If it does throw the exception, then the FileInputStream opened in its parameter won't be closed. The issue would then be correct.

      However, when using a constant non-zero positive value as size of the buffer of the BufferedInputStream, its construction can not fail and the FileInputStream will be correctly closed, even if the next read() operation fails.

      public void withCatchBlock() {
          InputStream inputStream = null;
          try {
              inputStream = new BufferedInputStream(new FileInputStream("test.txt"), 1024); // FP telling to close the FileInputStream
              inputStream.read(new byte[25]);
          } catch (IOException e) {
              LOGGER.error("Oops", e);
          } finally {
              IOUtils.closeQuietly(inputStream);
          }
      }
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated: