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

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

Example 1

25     public void scan(Name name, String... filters) {
26         try {
27             DirContext ctx = initContext();
28             try {
29                 for (final String filter : filters) {
30                     search(name, filter, ctx);
31                 }
32             } finally {
33                 ctx.close();
34             }
35         } catch (NamingException e) {
36             e.printStackTrace();
37         }
38     }
39 
40     private DirContext initContext() throws NamingException {
41         Hashtable env = new Hashtable(11);
42         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
43         env.put(Context.PROVIDER_URL, "ldap:///o=JndiSample,dc=example,dc=com");
44 
45         DirContext ctx = new InitialDirContext(env);
46         return ctx;
47     }
48 
49     private void search(Name name, String filter, DirContext ctx) throws NamingException {
50         try {
51             final SearchControls searchControls = new SearchControls();
52             searchControls.setReturningAttributes(necessaryItemAttributes);
53             searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
54             final NamingEnumeration<SearchResult> resultNamingEnumeration = ctx.search(name, filter, searchControls);
55         } catch (NamingException e) {
56             ctx.close();
57         }
58     }

UF.JNDI is reported for the snippet on line 30: JNDI context 'ctx' is accessed in the for-cycle in method 'scan' (see line 54). This is a problem because 'ctx' could be previously closed on one of the iterations as the result of an exception happening in method 'search' on line 56.

Example 2

25     public void scan(Name name, String... filters) {
26         try {
27             DirContext ctx = initContext();
28             try {
29                 for (final String filter : filters) {
30                     search(name, filter, ctx);
31                 }
32             } finally {
33                 ctx.close();
34             }
35         } catch (NamingException e) {
36             e.printStackTrace();
37         }
38     }
39 
40     private DirContext initContext() throws NamingException {
41         Hashtable env = new Hashtable(11);
42         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
43         env.put(Context.PROVIDER_URL, "ldap:///o=JndiSample,dc=example,dc=com");
44 
45         DirContext ctx = new InitialDirContext(env);
46         return ctx;
47     }
48 
49     private void search(Name name, String filter, DirContext ctx) throws NamingException {
50         final SearchControls searchControls = new SearchControls();
51         searchControls.setReturningAttributes(necessaryItemAttributes);
52         searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
53         final NamingEnumeration<SearchResult> resultNamingEnumeration = ctx.search(name, filter, searchControls);
54     }

The snippet from the previous section is fixed. Method 'search' no longer closes the stream. Now it is closed by the 'scan' 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.