Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MISRAC2012-Rule-11.8

In this section:
Synopsis

(Required) A cast shall not remove any const or volatile qualification from the type pointed to by a pointer A cast that removes a const or volatile qualification was found. This violates the principle of type qualification. Changes to the qualification of the pointer during the cast were not checked for.

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

A cast that removes a const or volatile qualification was found. This check is identical to MISRAC2004-11.5, MISRAC++2008-5-2-5, MISRAC++2023-8.2.3.

Coding standards
MISRA C:2004 11.5

(Required) A cast shall not be performed that removes any const or volatile qualification from the type addressed by a pointer.

MISRA C++ 2008 5-2-5

(Required) A cast shall not remove any const or volatile qualification from the type of a pointer or reference.

MISRA C++ 2023 8.2.3

(Required) A cast shall not remove any const or volatile qualification from the type accessed via a pointer or by reference

Code examples

The following code example fails the check and will give a warning:

typedef unsigned short uint16_t;

void example(void) {

	uint16_t x;
	const uint16_t *    pci;      /* pointer to const int */
	uint16_t *          pi;       /* pointer to int */

	pi = (uint16_t *)pci; // not compliant

}

The following code example passes the check and will not give a warning about this issue:

typedef unsigned short uint16_t;

void example(void) {

	uint16_t x;
	uint16_t * const    cpi = &x; /* const pointer to int */
	uint16_t *          pi;       /* pointer to int */

	pi = cpi; // compliant - no cast required

}