...
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 Void DCL35violates EXP37-C. Call functions with the correct number and type of arguments.:
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; } |
...
Failing to include type information for function declarators can result in unexpected or unintended program behavior.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL07-C | Low | Unlikely | Low | P3 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| function-prototype implicit-function-declaration | Partially checked | ||||||
Axivion Bauhaus Suite |
| CertC-DCL07 | |||||||
CodeSonar |
| LANG.FUNCS.PROT LANG.STRUCT.DECL.IMPT | Incomplete function prototype Implicit Type | ||||||
| CC2.DCL07 | Fully implemented | |||||||
GCC |
|
Can detect violation of this recommendation when the | |||||||||
Helix QAC |
| C1304, C2050, C3331, C3335, C3408, C3450 | |||||||
Klocwork |
| MISRA.FUNC.PROT_FORM.KR.2012 MISRA.FUNC.NOPROT.DEF MISRA.CAST.FUNC_PTR.2012 | |||||||
LDRA tool suite |
| 21 S | Fully implemented |
3335
3450
0563
2050
PC-lint Plus |
| 718, 746, 936, 9074 | Fully supported | ||||||
Polyspace Bug Finder |
| Checks for:
Rec. fully covered. | |||||||
RuleChecker |
| function-prototype implicit-function-declaration | Partially checked | ||||||
SonarQube C/C++ Plugin |
| S819, S930 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
ISO/IEC TR 24772:2013 | Type System [IHN] Subprogram Signature Mismatch [OTR] |
ISO/IEC TS 17961 | Using a tainted value as an argument to an unprototyped function pointer [taintnoproto] |
MISRA C:2012 | Rule 8. |
2 (required) |
Bibliography
[ISO/IEC 9899:2011] | Subclause 6.11.7, "Function Definitions" |
[Spinellis 2006] | Section 2.6.1, "Incorrect Routine or Arguments" |
...
...