MISRA 2012 Rule 17.3 states that a function should never be implicitly declared. However, in this case at line no 6, pf_func is violating the MISRA 2012 17.3 Rule.
typedef unsigned long long uint64;
typedef void (*FOREACH_FUNC)(uint64 ull_key);
void main(FOREACH_FUNC pf_func)
{
uint64 var;
pf_func(var); /*Violation reported on this line*/
}
Here, pf_func is causing violation on MISRA 17.3 Rule. Is this violation valid or is this a bug in the static analysis tool that I am using. Moreover, is there any alternate solution to avoid this violation without changing the workflow of the code?
However, when I modify the code to this-
typedef unsigned long long uint64;
typedef void (*FOREACH_FUNC)(uint64 ull_key);
FOREACH_FUNC pf_func(uint64 ull_key);
void main()
{
uint64 var;
pf_func(var);
}
there is no violation reported for rule no 17.3. I am not able to understand the working of function pointer here. Is this the correct or ethical way to solve this issue? Or is there any fault in the static analysis tool itself?
Rule 17.3 is "a function shall not be declared implicitly" and refers to the old C90 way of allowing function calls even when no prototype format declaration is present.
Your code does not do this. All it does is to call a function through a function pointer. So there is no MISRA violation, this is a false positive and a tool bug in your static analyser.
However, assuming you use C99 or later, it is preferred to use stdint.h
over home-brewed integer types.