[previous] MISRA C:2012  Rule-11.1:  (Required) [next] Conversions shall not be performed between a pointer to a function and any other type

Amplification:

A pointer to a function shall only be converted into or from a pointer to a function with a compatible type.

Exception:

  1. A null pointer constant may be converted into a pointer to a function;
  2. A pointer to a function may be converted into void;
  3. A function type may be implicitly converted into a pointer to that function type.

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:

Example Code:


#pragma PRQA_MESSAGES_OFF 2961,2992,2996,3112

#include <stdlib.h>
#include "misra.h"
#include "m3cmex.h"

typedef void (*fp16)(int16_t s);
typedef void (*fp32)(int32_t i);
typedef fp16 (*pfp16) (void);

extern int16_t rule_1101( void )
{
    void (*pf)(void) = NULL;
    int16_t  (*qf)(int32_t x) = NULL;
    pfp16 pfp1;

    fp16 fp1 = NULL;                                    /*      */      /* Compliant - exception */
    fp32 fp2 = (fp32)fp1;                               /* 0313 */      /* Non-compliant - function pointer to different function pointer */
    fp16 fp3 = (fp16)0x8000U;                           /* 0305 */      /* Non-compliant - integer to function pointer */
    fp16 fp4 = (fp16)1.0e6F;                            /* 0302 */      /* Non-compliant - float to function pointer */

    if (fp2 != NULL) { }                                /*      */      /* Compliant */

    (void) (pfp1 ());                                   /*      */      /* Compliant - exception - cast function pointer to void */

    (uint32_t)pf;                                       /* 0305 */
    (int32_t)pf;                                        /* 0305 */
    (float64_t)pf;                                      /* 0302 */
    (int16_t *)pf;                                      /* 0307 */
    (int16_t (*)(int32_t x))pf;                         /* 0313 */
    (void *       )pf;                                  /* 0307 */
    (void         )pf;

    return 0;
}




QAC messages that encompass this guideline:

0302 [u] Cast between a pointer to function and a floating type.
0305 [I] Cast between a pointer to function and an integral type.
0307 [u] Cast between a pointer to object and a pointer to function.
0313 Casting to different function pointer type.


Related rules:

Rule-1.3 There shall be no occurrence of undefined or critical unspecified behaviour



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents