Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

UF.IN

UF (Use Freed) issues are reported when there is an attempt to use resources after they were released. The UF.IN warning indicates an attempt to use an input stream after it was closed.

Example 1

18     public boolean checkMeta(InputStream stream) {
19         byte[] b = new byte[8];
20         try {
21             final long l = stream.available();
22             if (l < 8) {
23                 return false;
24             }
25             stream.read(b);
26         } catch (IOException e) {
27             return false;
28         } finally {
29             try {
30                 stream.close();
31             } catch (IOException e) {
32                 // do nothing
33             }
34         }
35 
36         // Cast unsigned character constants prior to comparison
37         return (b[0] == (byte) 'm' && b[1] == (byte) 'y' &&
38                 b[2] == (byte) 'f' && b[3] == (byte) 'o' &&
39                 b[4] == (byte) 'r' && b[5] == (byte) 'm' &&
40                 b[6] == (byte) 'a' && b[7] == (byte) 't');
41     }
42 
43     public void printOut(final URL url, final PrintStream ps) throws IOException {
44         final InputStream stream = url.openStream();
45         if (checkMeta(stream)) {
46             try {
47                 ps.println("content:");
48                 byte[] b = new byte[1024];
49                 int i;
50                 while ((i = stream.read(b)) > 0) {
51                     ps.print(new String(b, 0, i));
52                 }
53                 ps.println();
54             } catch (IOException e) {
55                 e.printStackTrace();
56             }
57         }
58     }

UF.IN is reported for the snippet on line 50: input stream 'stream' is used after it was closed on line 30.

Example 2

18     public boolean checkMeta(InputStream stream) {
19         byte[] b = new byte[8];
20         try {
21             final long l = stream.available();
22             if (l < 8) {
23                 return false;
24             }
25             stream.read(b);
26         } catch (IOException e) {
27             return false;
28         }
29 
30         // Cast unsigned character constants prior to comparison
31         return (b[0] == (byte) 'm' && b[1] == (byte) 'y' &&
32                 b[2] == (byte) 'f' && b[3] == (byte) 'o' &&
33                 b[4] == (byte) 'r' && b[5] == (byte) 'm' &&
34                 b[6] == (byte) 'a' && b[7] == (byte) 't');
35     }
36 
37     public void printOut(final URL url, final PrintStream ps) throws IOException {
38         final InputStream stream = url.openStream();
39         try {
40             if (checkMeta(stream)) {
41                 try {
42                     ps.println("content:");
43                     byte[] b = new byte[1024];
44                     int i;
45                     while ((i = stream.read(b)) > 0) {
46                         ps.print(new String(b, 0, i));
47                     }
48                     ps.println();
49                 } catch (IOException e) {
50                     e.printStackTrace();
51                 }
52             }
53         } finally {
54             try {
55                 stream.close();
56             } catch (IOException e) {
57                 // do nothing
58             }
59         }
60     }

The snippet from the previous section is fixed. Method 'checkMeta' no longer closes the stream. Now it is closed by the 'printOut' method, which is the method that opened the stream. In general, it is good coding practice to release resources with the same methods in which they were originally allocated.