C and C++


LANG.CAST.PC.INT : Conversion: Pointer/Integer

Summary

A cast or coercion from a pointer to an integer, or from an integer to a pointer.

Exception: casting or coercing constant integer 0 to a pointer will not trigger a warning of this class.

Properties

Class Name Conversion: Pointer/Integer
Significance style
Mnemonic LANG.CAST.PC.INT
Categories
MisraC2023 MisraC2023:11.4 A conversion should not be performed between a pointer to object and an integer type
Misra2012 Misra2012:11.4 A conversion should not be performed between a pointer to object and an integer type
Misra2004 Misra2004:11.3 A cast should not be performed between a pointer type and an integral type
AUTOSARC++14 AUTOSARC++14:M5-2-8 An object with integer type or pointer to void type shall not be converted to an object with pointer type.
  AUTOSARC++14:M5-2-9 A cast shall not convert a pointer type to an integral type.
MisraC++2008 MisraC++2008:5-2-7 An object with pointer type shall not be converted to an unrelated pointer type, either directly or indirectly.
  MisraC++2008:5-2-8 An object with integer type or pointer to void type shall not be converted to an object with pointer type.
  MisraC++2008:5-2-9 A cast should not convert a pointer type to an integral type.
MisraC++2023 MisraC++2023:8.2.6 An object with integral, enumerated, or pointer to void type shall not be converted to an object with pointer type
  MisraC++2023:8.2.7 A cast should not convert a pointer type to an integral type
CWE CWE:704 Incorrect Type Conversion or Cast
TS17961 TS17961:5.6-argcomp 5.6. Calling functions with incorrect arguments
  TS17961:5.10-intptrconv 5.10. Converting a pointer to integer or integer to pointer
CERT-C CERT-C:INT31-C Ensure that integer conversions do not result in lost or misinterpreted data
  CERT-C:INT36-C Converting a pointer to integer or integer to pointer
JSF++ JSF++:182 Type casting from any type to or from pointers shall not be used.
  JSF++:183 Every possible measure should be taken to avoid type casting.
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="Conversion: Pointer/Integer"
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

char* PC_ItoP(int i){
    char *p = (char *) i;        /* 'Conversion: Pointer/Integer' warning issued here */
    char *pnull = (char *) 0;                   /* specific exception */
    char *pnullc = 0;                           /* specific exception */
    return p;
}

int PC_PtoI(char *c){
    int i = (int) c;             /* 'Conversion: Pointer/Integer' warning issued here */
    return i;
}

Relevant Configuration File Parameters

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