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.IMAGEIO

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

Example 1

17     public boolean canDecodeInput(ImageInputStream stream) {
18         byte[] b = new byte[8];
19         try {
20             final long l = stream.length();
21             if (l > 655335) {
22                 return false;
23             }
24             stream.mark();
25             stream.readFully(b);
26             stream.reset();
27         } catch (IOException e) {
28             return false;
29         } finally {
30             try {
31                 stream.close();
32             } catch (IOException e) {
33                 // do nothing
34             }
35         }
36 
37         // Cast unsigned character constants prior to comparison
38         return (b[0] == (byte) 'm' && b[1] == (byte) 'y' &&
39                 b[2] == (byte) 'f' && b[3] == (byte) 'o' &&
40                 b[4] == (byte) 'r' && b[5] == (byte) 'm' &&
41                 b[6] == (byte) 'a' && b[7] == (byte) 't');
42     }
43 
44     public void decode(Object data) throws IOException {
45         final ImageInputStream stream = ImageIO.createImageInputStream(data);
46         if (canDecodeInput(stream)) {
47             try {
48                 final int[] colors = new int[(int) stream.length()];
49                 for (int i = 0; i < colors.length; i++) {
50                     colors[i] = stream.readInt();
51                 }
52             } catch (IOException e) {
53                 e.printStackTrace();
54             }
55         }
56 
57     }

UF.IMAGEIO is reported for the snippet on line 48: image input stream 'stream' is used after it was closed on line 31.

Example 2

17     public boolean canDecodeInput(ImageInputStream stream) {
18         byte[] b = new byte[8];
19         try {
20             final long l = stream.length();
21             if (l > 655335) {
22                 return false;
23             }
24             stream.mark();
25             stream.readFully(b);
26             stream.reset();
27         } catch (IOException e) {
28             return false;
29         }
30 
31         // Cast unsigned character constants prior to comparison
32         return (b[0] == (byte) 'm' && b[1] == (byte) 'y' &&
33                 b[2] == (byte) 'f' && b[3] == (byte) 'o' &&
34                 b[4] == (byte) 'r' && b[5] == (byte) 'm' &&
35                 b[6] == (byte) 'a' && b[7] == (byte) 't');
36     }
37 
38     public void decode(Object data) throws IOException {
39         final ImageInputStream stream = ImageIO.createImageInputStream(data);
40         try {
41             if (canDecodeInput(stream)) {
42                 try {
43                     final int[] colors = new int[(int) stream.length()];
44                     for (int i = 0; i < colors.length; i++) {
45                         colors[i] = stream.readInt();
46                     }
47                 } catch (IOException e) {
48                     e.printStackTrace();
49                 }
50             }
51         } finally {
52             stream.close();
53         }
54     }

The snippet from the previous section is fixed. Method 'canDecodeInput' no longer closes the stream. Now it is closed by the 'decode' method, which is the method that opened the stream. In general, it is good coding practice to release a resource with the same method that originally allocated the resource.