...
Wiki Markup |
---|
Attempting to compile a program with a function declarator that does not include the appropriate type information typically generates a warning. These warnings should be resolved \[[MSC00-A. Compile cleanly at high warning levels]\], but do not prevent program compilation. |
Non-Compliant Code Example (malloc()
)
Wiki Markup |
---|
The following example is based on rule \[[MEM02-A. Do not cast the return value from malloc()]\]. The header file {{stdlib.h}} contains the function prototype for {{malloc()}}. Failing to include {{stdlib.h}} causes {{malloc()}} to be improperly defined. |
Code Block | ||
---|---|---|
| ||
char *p = malloc(10); |
Compliant Solution: (malloc()
)
Including stdlib.h
ensures the function prototype for malloc()
is declared.
Code Block | ||
---|---|---|
| ||
#include <stdlib.h> /* ... */ char *p = malloc(10); |
Non-Compliant Code Example: (non-prototype-format declarators)
The non-compliant code example uses the identifier-list form for the parameter declarations.
...
Section 6.11 of the C99 standards, "Future language directions", states that "The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature."
Compliant Solution: (non-prototype-format declarators)
In this compliant solution, extern
is the storage-class specifier and int
is the type specifier; max(int a, int b)
is the function declarator; and the block within {} is the function body.
Code Block | ||
---|---|---|
| ||
extern int max(int a, int b) { return a > b ? a : b; } |
Non-Compliant Code Example: (function prototypes)
Failure to specify function prototypes results in a function being implicitly defined. Without a function prototype, the compiler assumes the the correct number and type of parameters have been supplied to a function. This can result in unintended and undefined behavior.
...
Wiki Markup |
---|
C99 eliminated implicit function declarations from the C language \[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\]. However, many compilers allow compilation of programs containing implicitly defined functions, although they may issue a warning message. These warnings should be resolved \[[MSC00-A. Compile cleanly at high warning levels]\], but do not prevent program compilation. |
Compliant Solution: (function prototypes)
To correct this example, the appropriate function prototype for func()
should be specified.
Code Block | ||
---|---|---|
| ||
int func(int, int, int); /* ... */ func(1, 2); /* ... */ int func(int one, int two, int three){ printf("%d %d %d", one, two, three); return 1; } |
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]\]. |
Non-Compliant Code Example: (function pointers)
In this example, the function pointer fn_ptr
is set to refer to strchrthe function add()
, which is defined elsewhereaccepts three integer arguments. 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 resultsspecified to accept two integer arguments. Setting fn_ptr
to refer to add()
results in an unexpected program behavior.
Code Block | ||
---|---|---|
| ||
externint add(int x, int y, int z) { return x + y + z; } int main(int argc, char *strchr(); argv[]) { int (*fn_ptr) (); int, main(voidint) {; charint *cres; fn_ptr = add; cres = fn_ptr("Hello"2, 'H'3); /* incorrect */ /* ... */ return 0; } |
Compliant Solution: (function pointers)
Correcting this example requires requires two modifications. First, the string.h
header file is included to make the prototype for strchr()
visible to the program. NextTo correct this example, the declaration of fn_ptr
is changed to be compatible with strchr()
accept three arguments.
Code Block | ||
---|---|---|
| ||
#include <string.h> int add(int x, int y, int z) { return x + y + z; } int main(int argc, char *argv[]) { int (*fn_ptr) (charint, *int, int) ; int main(void) { int char *cres; fn_ptr = add; cres = fn_ptr("Hello"2, 3, 'H'4); /* incorrect */ /* ... */ return 0; } |
Risk Assessment
Failing to include type information for function declarators can result in unexpected or unintended program behavior.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL31-C | 1 (low) | 1 (unlikely) | 3 (low) | P3 | L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Forward, Section 6.9.1, "Function definitions" \[[Spinellis 06|AA. C References#Spinellis 06]\] Section 2.6.1, "Incorrect Routine or Arguments" |