Do not call a function with the wrong number or type of arguments.
The C Standard identifies four distinct situations in which undefined behavior may arise as a result of invoking a function using a declaration that is incompatible with its definition or with incorrect types or numbers of arguments:
UB | Description |
A pointer is used to call a function whose type is not compatible with the pointed-to type (6.3.2.3). | |
For a call to a function without a function prototype in scope, the number of arguments does not equal the number of parameters (6.5.2.2). | |
For call to a function without a function prototype in scope where the function is defined with a function prototype, either the prototype ends with an ellipsis or the types of the arguments after promotion are not compatible with the types of the parameters (6.5.2.2). | |
A function is defined with a type that is not compatible with the type (of the expression) pointed to by the expression that denotes the called function (6.5.2.2). |
...
(See also undefined behavior 26 in Annex J of the C Standard.)
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> #include <string.h> char *(*fp) (); int main(void) { char *c; fp = strchr; c = fp(12, 2); printf("%s\n", c); return 0; } |
...
Tool | Version | Checker | Description | GCC | |||||
---|---|---|---|---|---|---|---|---|---|
Include Page | GCC_V | GCC_V | Can detect violation of this rule when the | ||||||
Compass/ROSE | can detect some violations of this rule. In particular, it ensures that all calls to | ||||||||
EDG | |||||||||
Fortify SCA | 5.0 | ||||||||
GCC |
| Can detect violation of this rule when the | |||||||
| 41 D | Partially implemented. | |||||||
PRQA QA-C |
| 3001 0674(C) | Partially implemented. | ||||||
Fortify SCA | V. 5.0 | ||||||||
EDG |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
CERT C++ Secure Coding Standard | EXP37-CPP. Call variadic functions with the arguments intended by the API | ||
---|---|---|---|
ISO/IEC TR 24772 | Subprogram signature mismatch [OTR] | ||
ISO/IEC TS 17961 | (Draft) Calling functions with incorrect arguments [argcomp | ]ISO/IEC TR 24772 | Subprogram signature mismatch [OTR] |
MISRA-C | Rule 16.6 (required): The number of arguments passed to a function shall match the number of parameters | ||
MITRE CWE | CWE-628, Function call with incorrectly specified arguments |
...
[CVE] | CVE-2006-1174 |
[ISO/IEC 9899:2011] | Foreword and Section 6.93.2.13, "Function DefinitionsPointers" |
---|---|
[Spinellis 2006] | Section 2.6.1, "Incorrect Routine or Arguments" |
...