C and C++


LANG.CAST.PC.FN2DATA : Conversion from Function Pointer

Summary

A value of function pointer type is cast or coerced to a non-function-pointer type.

Properties

Class Name Conversion from Function Pointer
Significance security
Mnemonic LANG.CAST.PC.FN2DATA
Categories
MisraC2023 MisraC2023:11.1 Conversions shall not be performed between a pointer to a function and any other type
Misra2012 Misra2012:11.1 Conversions shall not be performed between a pointer to a function and any other type
AUTOSARC++14 AUTOSARC++14:M5-2-6 A cast shall not convert a pointer to a function to any other pointer type, including a pointer to function type.
  AUTOSARC++14:M8-4-4 A function identifier shall either be used to call the function or it shall be preceded by &.
MisraC++2008 MisraC++2008:5-2-6 A cast shall not convert a pointer to a function to any other pointer type, including a pointer to function type.
  MisraC++2008:5-2-7 An object with pointer type shall not be converted to an unrelated pointer type, either directly or indirectly.
  MisraC++2008:8-4-4 A function identifier shall either be used to call the function or it shall be preceded by &.
MisraC++2023 MisraC++2023:8.2.4 Casts shall not be performed between a pointer to a function and any other type
CWE CWE:704 Incorrect Type Conversion or Cast
TS17961 TS17961:5.1-ptrcomp 5.1. Accessing an object through a pointer to an incompatible type
  TS17961:5.6-argcomp 5.6. Calling functions with incorrect arguments
JSF++ JSF++:182 Type casting from any type to or from pointers shall not be used.
  JSF++:183 Every possible measure should be taken to avoid type casting.
JPL JPL:30 Do not cast function pointers into other types.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Conversion from Function Pointer"

Example

typedef int (*TakesIntRetsInt)(int) ;

int PC_FN2DATA(TakesIntRetsInt f){
    int x = (int) f; /* 'Conversion from Function Pointer' warning issued here */
    int y = f;       /* 'Conversion from Function Pointer' warning issued here */
    return x + y;
}

See also

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.