C and C++


LANG.CAST.PC.AV : Cast: Arithmetic Type/Void Pointer

Summary

A cast between a pointer to void and an integer type.

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

Properties

Class Name Cast: Arithmetic Type/Void Pointer
Significance style
Mnemonic LANG.CAST.PC.AV
Categories
MisraC2023 MisraC2023:11.6 A cast shall not be performed between pointer to void and an arithmetic type
Misra2012 Misra2012:11.6 A cast shall not be performed between pointer to void and an arithmetic type
Misra2004 Misra2004:11.3 A cast should not be performed between a pointer type and an integral type
CWE CWE:704 Incorrect Type Conversion or Cast
TS17961 TS17961:5.1-ptrcomp 5.1. Accessing an object through a pointer to an incompatible type
CERT-C CERT-C:INT31-C Ensure that integer conversions do not result in lost or misinterpreted data
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="Cast: Arithmetic Type/Void Pointer"
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

void use_pointers(void* p1, void *p2, void *p3);
void use_ints(int x, int y);

void PC_AtoV(int i){
    void *pv = (void *) i; /* 'Cast: Arithmetic Type/Void Pointer' warning issued here */
    void *cpv = i;                  /* not a cast */
    void *pnull = (void *) 0;       /* specific exception */
    use_pointers(pv, cpv, pnull);
}

void PC_VtoA(void *pv){
    int i = (int) pv;      /* 'Cast: Arithmetic Type/Void Pointer' warning issued here */
    int ci = pv;                    /* not a cast */
    use_ints(i, ci);
}

Relevant Configuration File Parameters

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