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

JD.CAST.KEY

JD.CAST.KEY is reported when the type of key used to retrieve a collection element differs from the type of key used to put the object into the collection.

Vulnerability and risk

The expected element will not be found in the collection, since no elements were put into the collection with a key of this type.

Mitigation and prevention

Check if the collection is expected to contain keys of different types. Check if the key used to retrieve an element from the collection is of the correct type.

Example 1

11 public class JD_CAST_KEY_Sample_1 {
12 
13     HashMap len=new HashMap();
14 
15     void fill(File dir){
16       File[] list = dir.listFiles();
17       for (int i = 0; i < list.length; i++) {
18         File file = list[i];
19         len.put(file, new Long(file.length()));
20       }
21     }
22 
23     int getLength(String file){
24       Long l = (Long) len.get(file);
25       if (l!=null) return l.intValue();
26       return 0;
27     }
28 }

JD.CAST.KEY is reported for call to 'len.get(file)' on line 24: Suspicious key of type 'java.lang.String' used to retrieve a collection element. Object was put into the collection with key of type 'java.io.File'.

Related checkers