C and C++


LANG.CAST.PC.PV : Conversion: Void Pointer to Object Pointer

Summary

A pointer to void is cast or coerced to a pointer to object.

Exception: casting or coercing a null pointer constant (such as NULL) to pointer to object will not trigger a warning of this class.

Properties

Class Name Conversion: Void Pointer to Object Pointer
Significance style
Mnemonic LANG.CAST.PC.PV
Categories
MisraC2023 MisraC2023:11.5 A conversion should not be performed from pointer to void into pointer to object
Misra2012 Misra2012:11.5 A conversion should not be performed from pointer to void into pointer to object
Misra2004 Misra2004:11.2 Conversions shall not be performed between a pointer to object and any type other than an integral type, another pointer to object type or a pointer to void
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.
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++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
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
  TS17961:5.6-argcomp 5.6. Calling functions with incorrect arguments
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: Void Pointer to Object 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

#include <stddef.h>

void use_pointers(int *p1, int *p2);

void PC_PV(void *v){
    int *i;
    int *j;

    i = (int *) v;        /* 'Conversion: Void Pointer to Object Pointer' warning issued here */
    j = (int *) NULL;                  /* specific exception */
    
    use_pointers(i,j);
}

Relevant Configuration File Parameters

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