MISRA.CAST.FUNC_PTR.2012Conversion performed between a pointer to a function and another incompatible type.
MISRA C 2012 Rule 11.1: Conversions shall not be performed between a pointer to a function and any other typeC90 [Undefined 24, 27-29], C99 [Undefined 21, 23, 39, 41] Category: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationA pointer to a function shall only be converted into or from a pointer to a function with a compatible type. RationaleThe conversion of a pointer to a function into or from any of
If a function is called by means of a pointer whose type is not compatible with the called function, the behaviour is undefined. Conversion of a pointer to a function into a pointer to a function with a different type is permitted by The Standard. Conversion of an integer into a pointer to a function is also permitted by The Standard. However, both are prohibited by this rule in order to avoid the undefined behaviour that would result from calling a function using an incompatible pointer type. Exception
Note: Exception 3 covers the implicit conversions described in C90 Section 6.2.2.1 and C99 Section 6.3.2.1. These conversions commonly occur when:
Exampletypedef void ( *fp16 ) ( int16_t n ); typedef void ( *fp32 ) ( int32_t n ); #include <stdlib.h> /* To obtain macro NULL */ fp16 fp1 = NULL; /* Compliant - exception 1 */ fp32 fp2 = ( fp32 ) fp1; /* Non-compliant - function * pointer into different * function pointer */ if ( fp2 != NULL ) /* Compliant - exception 1 */ { } fp16 fp3 = ( fp16 ) 0x8000; /* Non-compliant - integer into * function pointer */ fp16 fp4 = ( fp16 ) 1.0e6F; /* Non-compliant - float into * function pointer */ In the following example, the function call returns a pointer to a function type. Casting the return value into void is compliant with this rule. typedef fp16 ( *pfp16 ) ( void ); pfp16 pfp1; ( void ) ( *pfp1 ( ) ); /* Compliant - exception 2 - cast function * pointer into void */ The following examples show compliant implicit conversions from a function type into a pointer to that function type. extern void f ( int16_t n ); f ( 1 ); /* Compliant - exception 3 - implicit conversion * of f into pointer to function */ fp16 fp5 = f; /* Compliant - exception 3 */ |