...
Non-Compliant Code Example: (function pointers)
Wiki Markup |
---|
If a function pointer is set to refer to an incompatible function , invoking that function via the pointer may cause unexpected data to be taken from the process stack. As a result, unexpected data may be accessed by the called function \[[DCL35-C. Do not convert a function |
. pointer to a function of an incompatible type]\]. |
In this example, the function pointer Setting fn_ptr
is set to point refer to add()
strchr()
, which is defined elsewhere. However, because fn_ptr
is declared with an empty parameter type list, the compiler will not generate a warning if the wrong type, or number of arguments are supplied to fn_ptr
. This could result in an unexpected value being used as parameter z
in function add()
results.
Code Block |
---|
|
int add(int x, int y, int z) {
return x + y + z;
}
int main(int argc, extern char *argv[]) {
strchr();
int (*fn_ptr) ();
int, intmain(void) ;{
intchar res*c;
fn_ptr = add;
resc = fn_ptr(2"Hello", 3'H'); /* incorrect */
/* ... */
return 0;
}
|
...