CAST Engineering Dashboard - Violations - False Violation - Close the outermost stream ASAP

Description

You are getting a violation that seems wrong for you for quality rule -  Close the outermost stream ASAP .

Observed in CAST AIP

ReleaseYes/No
8.3.x

Observed on RDBMS

RDBMSYes/No
CSS

Step by Step Scenario

  1. Taking analysis and snapshot
  2. Observing a violation for ‘Close the outermost stream ASAP’ that seems to be wrong

Action Plan

  1. Check if you have closed the outermost stream both in the try block and in the finally block as shown in below source code. 
    Example -

private void Writing() throws IOException { DataOutputStream out = null; try { out = new DataOutputStream(new FileOutputStream(“data”)); out.writeInt(666); out.writeUTF(“Hello”); out.close(); //close in the try block out = null; } finally { if (out != null) { out.close(); // close in the finally blcok } }


   This will lead to a violation because you are not using the right remediation pattern to close the outputstream as indicated in the remediation section.
2. You should close it only once in the finally block without closing it in the try block. In other words, you should have instead.

   ```java
 private void correctWriting() throws IOException {
    DataOutputStream out = null;
     try {
         out = new DataOutputStream(new FileOutputStream("data"));
         out.writeInt(666);
         out.writeUTF("Hello");
     } finally {
         if (out != null) {
             out.close(); // FIX
         }
     }
     }

Impact on Analysis Results and Dashboard

Impact of issue: Impact after applying solution: To be decided

Notes/comments

Ticket # 3760

Related Pages

CAST Engineering Dashboard - Troubleshooting - False positive Wrong Violation or False negative No Violation