>
>
>
V2597. MISRA. Cast should not convert p…


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

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-5.2.6