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
C and C++


LANG.TYPE.ICT : Inappropriate Cast Type

Summary

The value of an expression is explicitly cast to an inappropriate essential type, as shown in the following table.

Essential Type of Expression Inappropriate Essential Type Categories for Cast
essentially Boolean essentially character, essentially enum, essentially signed, essentially unsigned, essentially real floating, essentially complex floating
essentially character essentially Boolean, essentially enum, essentially real floating, essentially complex floating
essentially enum essentially Boolean, essentially enum (except casts to exactly the same enum, which would be redundant)
essentially signed essentially Boolean, essentially enum
Exception: an integer constant expression with value 0 or 1 can be cast to an essentially Boolean type.
essentially unsigned essentially Boolean, essentially enum
Exception: an integer constant expression with value 0 or 1 can be cast to an essentially Boolean type.
essentially real floating essentially Boolean, essentially character, essentially enum
essentially complex floating essentially Boolean, essentially character, essentially enum

This warning class is based on the notion of essential type category as described in MISRA C:2012 Guidelines for the use of the C language in critical systems.

Properties

Class Name Inappropriate Cast Type
Significance style
Mnemonic LANG.TYPE.ICT
Categories
MisraC2025 MisraC2025:10.5 The value of an expression should not be cast to an inappropriate essential type
MisraC2023 MisraC2023:10.5 The value of an expression should not be cast to an inappropriate essential type
Misra2012 Misra2012:10.5 The value of an expression should not be cast to an inappropriate essential type
CWE CWE:704 Incorrect Type Conversion or Cast
JSF++ JSF++:184 Floating point numbers shall not be converted to integers unless such a conversion is a specified algorithmic requirement or is necessary for a hardware interface.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Inappropriate Cast Type"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

typedef enum {GO, PROCEED_WITH_CAUTION, STOP} action;
typedef enum {GREEN, ORANGE, RED} light;

char LANG_TYPE_ICT(action a, char ch){
  light lt;
  float f;

  if (ch)
    lt = a;                                    /* not an explicit cast ('Mixed enum Type' warning issued) */
  else
    lt = (light) a;       /* 'Inappropriate Cast Type' warning issued here */

  if (lt > 1)
    f = (float) ch;       /* 'Inappropriate Cast Type' warning issued here */
  else
    f = (float) (lt==0);  /* 'Inappropriate Cast Type' warning issued here */

  if (f > 5)
    return (char) a;                          /* permissible cast for this warning class */
  else
    return 'X';
}

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