JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A classcast might be incorrect at runtime.
You can annotate a method as predictable — that is, as always returning a type predictable by software developers — with the @com.juliasoft.julia.checkers.classcast.PredictableReturnType annotation. The analysis will not issue warnings for any casts of return values from methods with this annotation.
@com.juliasoft.julia.checkers.classcast.PredictableReturnType annotations have no effect in this case, since checks do not cover casts on method return values.
(Note that entry methods are determined with respect to the setting of JAVA_ANALYSIS_ENTRY_POINTS_MODE.)
Java checks explicit type downcasts at runtime. If they do not hold, a runtime exception is thrown. This checker verifies that casts will be satisfied at runtime, so that they will never throw an exception.
| Class Name | Risky Class Cast (Java) | |||
|---|---|---|---|---|
| Significance | reliability | |||
| Mnemonic | JAVA.CLASS.CAST | |||
| Categories |
|
|||
| Availability | Available for Java and Kotlin. |
|||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Risky Class Cast (Java)" |
public class TestCasts {
private Object f;
private Object g = "hi";
public void m1(Object o) {
if (((String) o).startsWith("hello")) // "Risky Class Cast (Java)" warning issued here
System.out.println("nice to meet you");
}
public void m2(String s, Object o) {
if (((String) o).startsWith("hello")) // "Risky Class Cast (Java)" warning issued here
System.out.println("nice to meet you");
}
public void m3(String s, Object o) {
if (((String) wrap(o)).startsWith("hello")) // "Risky Class Cast (Java)" warning issued here if JAVA_ANALYSIS_PEDANTIC_MODE=Yes
System.out.println("nice to meet you");
}
public void m4() {
if (((String) f).startsWith("hello")) // "Risky Class Cast (Java)" warning issued here if JAVA_ANALYSIS_PEDANTIC_MODE=Yes
System.out.println("nice to meet you");
}
public void m5() {
if (((String) g).startsWith("hello")) // ok: safe cast
System.out.println("nice to meet you");
}
private Object wrap(Object o) {
return o;
}
public void caller1() {
m1("hi");
m1(this);
}
public void caller2() {
m2("ciao", "hello");
m2("come stai", "how are you?");
}
public void caller3() {
m3("ciao", "hello");
m3("come stai", f = new Object());
}
}
For each classcast warning, verify that all types that will ever reach that program point are assignable to the type used for the cast.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.