Main Content

AUTOSAR C++14 Rule M5-2-8

An object with integer type or pointer to void type shall not be converted to an object with pointer type

Description

Rule Definition

An object with integer type or pointer to void type shall not be converted to an object with pointer type.

Rationale

Converting between integral types or a (void)* type to other pointer types can result in unspecified behavior.

Polyspace Implementation

The checker allows an exception on zero constants, such as 0x0, 0, or 0U.

Objects with pointer type include objects with pointer-to-function type.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, Polyspace flags the conversion of integers to pointers. Polyspace does not flag converting zero constants such as 0, 0x0, or 0U to pointers. Such conversions are equivalent to zero initialization of a pointer, which has a specified behavior.

void foo ()
{
  void *p1;
  void *p2;
  void *p3;

  p1 = static_cast < void *>(0x0);	//Compliant
  p2 = static_cast < void *>(0U);	//Compliant
  p3 = static_cast < void *>(0);	//Compliant

  p1 = (void *) 0xDEADBEEF;	//Noncompliant
  p2 = (void *) 03;		//Noncompliant
  p3 = (void *) 12345678U;	//Noncompliant
}

Check Information

Group: Expressions
Category: Required, Automated

Version History

Introduced in R2019a

expand all