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
Java


JAVA.CLASS.CAST : Risky Class Cast (Java)

Summary

A classcast might be incorrect at runtime.

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

Properties

Class Name Risky Class Cast (Java)
Significance reliability
Mnemonic JAVA.CLASS.CAST
Categories
CWE CWE:704 Incorrect Type Conversion or Cast
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)"

Example

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());
  }
}

Resolution

For each classcast warning, verify that all types that will ever reach that program point are assignable to the type used for the cast.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.