Incorrect Conversion Of Numeric Types Critical

Converting from one data type to another, such as long to integer, data can be omitted or translated in a way that produces unexpected values. If the resulting values are used in a sensitive context, then dangerous behaviors may occur.

Detector ID
cpp/incorrect-conversion-of-numeric-types@v1.0
Category
Common Weakness Enumeration (CWE) external icon
Tags
-

Noncompliant example

1
2void incorrectConversionOfNumericTypesNoncompliant(int int_var) {
3    // Noncompliant: A primitive is cast to a primitive of a smaller size.
4    char char_var = int_var;
5}

Compliant example

1
2void incorrectConversionOfNumericTypesCompliant(int int_var) {
3    // Compliant: A primitive is cast to a primitive of a same size.
4    int char_var = int_var;
5}