C and C++


LANG.CAST.PC.CONST2PTR : Coercion: Integer Constant to Pointer

Summary

An integer constant expression C is coerced to a pointer by being used in one of the following contexts.

Note that if your definition of NULL is an integer constant expression (without cast to pointer type), using NULL in these contexts will trigger warnings of this class. For example:

#define NULL 0

void irreg_null(void){
    int *p = NULL;  /* 'Coercion: Integer Constant to Pointer' warning issued here  */
}

(A warning would not be issued in this location if the code used the more typical #define NULL ((void *)0).)

Properties

Class Name Coercion: Integer Constant to Pointer
Significance style
Mnemonic LANG.CAST.PC.CONST2PTR
Categories
MisraC2023 MisraC2023:11.9 The macro NULL shall be the only permitted form of integer null pointer constant
Misra2012 Misra2012:11.9 The macro NULL shall be the only permitted form of integer null pointer constant
AUTOSARC++14 AUTOSARC++14:M4-10-2 Literal zero (0) shall not be used as the null-pointer-constant.
MisraC++2008 MisraC++2008:4-10-2 Literal zero (0) shall not be used as the null-pointer-constant.
MisraC++2023 MisraC++2023:7.11.1 nullptr shall be the only form of the null-pointer-constant
CWE CWE:587 Assignment of a Fixed Address to a Pointer
TS17961 TS17961:5.6-argcomp 5.6. Calling functions with incorrect arguments
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="Coercion: Integer Constant to 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 <stdlib.h>

#define ZERO 0

void use_pointers(int *x, int *y, int *z);

int const2ptrs(int i, int *ptr){
    int y = 0;
    int *p1;
    int *p2;
    int *p3;

    if (ptr == 0){return 0;}   /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    
    p1 = NULL;                            /* macro NULL (standard definition) */
    p2 = 0;                    /* 'Coercion: Integer Constant to Pointer' warning issued here  */
    p3 = (int *) 0;                       /* explicit cast (not coercion) */
    use_pointers(p1,p2,p3);
    
    p1 = 0x080484e2;           /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    p2 = (i - y) ? ptr : 0;    /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    p3 = (i > y) ? 0 : ptr;    /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    use_pointers(p1,p2,p3);

    p1 = y;                               /* not a constant expression */
    p2 = i;                               /* not a constant expression */
    p3 = ZERO;                 /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    use_pointers(p1,p2,p3);

    
    int a = (i - y) ? ptr : 0; /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    int b = (i > y) ? 0 : ptr; /* 'Coercion: Integer Constant to Pointer'  warning issued here  */

    return a+b;
}

Relevant Configuration File Parameters

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