...
Attempting to compile a program with a function declarator that does not include the appropriate type information typically generates a warning but does not prevent program compilation. These warnings should be resolved. (See recommendation MSC00-C. Compile cleanly at high warning levels.)
Noncompliant Code Example (
...
Non-Prototype-Format Declarators)
Noncompliant code example uses the identifier-list form for parameter declarations.
Code Block | ||||
---|---|---|---|---|
| ||||
int max(a, b)
int a, b;
{
return a > b ? a : b;
}
|
Section 6.11.7 of the C99 standard, "Future language directions," states that "The C standard [ISO/IEC 9899:2011] 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, int
is the type specifier, max(int a, int b)
is the function declarator, and the block within the curly braces is the function body.
Code Block | ||||
---|---|---|---|---|
| ||||
int max(int a, int b) {
return a > b ? a : b;
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* file_a.c source file */
int func(int one, int two, int three){
printf("%d %d %d", one, two, three);
return 1;
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* file_b.c source file */
func(1, 2);
|
C99 eliminated implicit function declarations from the C language [ISO/IEC 9899:1999]. However, many compilers still allow the compilation of programs containing implicitly declared functions, although they may issue a warning message. These warnings should be resolved. (See recommendation MSC00-C. Compile cleanly at high warning levels.)
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* file_b.c source file */
int func(int, int, int);
func(1, 2, 3);
|
...
In this noncompliant code example, the function pointer fn_ptr
refers to the function add()
, which accepts three integer arguments. However, fn_ptr
is specified to accept two integer arguments. Setting fn_ptr
to refer to add()
results in unexpected program behavior. This example also violates the rule DCL35-C. Do not invoke a function using a type that does not match the function definition.
Code Block | ||||
---|---|---|---|---|
| ||||
int add(int x, int y, int z) {
return x + y + z;
}
int main(int argc, char *argv[]) {
int (*fn_ptr) (int, int);
int res;
fn_ptr = add;
res = fn_ptr(2, 3); /* incorrect */
/* ... */
return 0;
}
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
int add(int x, int y, int z) {
return x + y + z;
}
int main(int argc, char *argv[]) {
int (*fn_ptr) (int, int, int) ;
int res;
fn_ptr = add;
res = fn_ptr(2, 3, 4);
/* ... */
return 0;
}
|
...
Tool | Version | Checker | Description | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Section | |
| Section | 21 S | Section | Fully Implementedimplemented. | ||||||
Section | GCC |
|
| Section | Can detect violation of this recommendation when the | |||||||
Section | |
| decltype section | Fully Implementedimplemented. |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
ISO/IEC 9899:19992011 Forward and Section 6.9.1, "Function definitions"
ISO/IEC TR 24772 "IHN Type system" and "OTR Subprogram Signature Mismatchsignature mismatch"
MISRA Rule 8.2
Bibliography
[Spinellis 2006] Section 2.6.1, "Incorrect Routine routine or Argumentsarguments"
...