Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2597. MISRA. Cast should not...
menu mobile close menu
Additional information
toggle menu Contents

V2597. MISRA. Cast should not convert pointer to function to any other pointer type.

Jul 07 2021

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development standard.

This rule applies only to C++. When a pointer to a function is cast to any other pointer, undefined behavior occurs. Type casting between pointers to functions of incompatible types causes undefined behavior when this function is called.

The code below violates this rule - all four type casts are invalid:

void foo(int32_t x); 
typedef void (*fp)(int16_t x); 

void bar() 
{ 
  fp fp1 = reinterpret_cast<fp>(&foo);
  fp fp2 = (fp)foo;
  void* vp = reinterpret_cast<void*>(fp1);
  char* chp = (char*)fp1;
}

Using such pointers to call the function may potentially cause segmentation errors.

This diagnostic is classified as:

  • MISRA-CPP-2008-5.2.6