...
In this compliant solution, the function pointer fp
points to a function returning char *
, with the correct number and type of arguments.:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <string.h> char *(*fp) (const char *, int); int main(void) { char *c; fp = strchr; c = fp("Hello",'H'); printf("%s\n", c); return 0; } |
...
In this noncompliant example, the function copy()
is defined to take two arguments but is called with three arguments.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* in another source file */ void copy(char *dst, const char *src) { if (!strcpy(dst, src)) { /* report error */ } } /* in this source file -- no copy prototype in scope */ void copy(); void g(const char *s) { char buf[20]; copy(buf, s, sizeof buf); /* violation */ /* ... */ } |
...
In this compliant solution, the prototype for the copy()
function is included in the scope in the source file where it is used, and the copy()
function is passed the correct number and type of arguments.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* in another source file */ void copy(char *dst, const char *src) { if (!strcpy(dst, src)) { /* report error */ } } /* copy prototype in scope in this source file */ void copy(char *dst, const char *src); void g(const char *s) { char buf[20]; copy(buf, s); /* ... */ } |
...
In this noncompliant example, the function buginf()
is defined to take a variable number of arguments but is declared in another file with no prototype and is called.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* in another source file */ void buginf(const char *fmt, ...) { /* ... */ } /* in this source file -- no buginf prototype in scope */ void buginf(); void h(void) { buginf("bug in function %s, line %d\n", __func__, __LINE__); /* violation */ /* ... */ } |
...
In this compliant solution, the prototype for the function buginf()
is included in the scope in the source file where it is used.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* in another source file */ void buginf(const char *fmt, ...) { /* ... */ } /* buginf prototype in scope in this source file */ void buginf(const char *fmt, ...); void h(void) { buginf("bug in function %s, line %d\n", __func__, __LINE__); /* ... */ } |
...
In this noncompliant example, the function f()
is defined to take an argument of type long
, but f()
is called from another file with an argument of type int
.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* in another source file */ long f(long x) { return x < 0 ? -x : x; } /* in this source file -- no f prototype in scope */ int g(int x) { return f(x); /* violation */ } |
...
In this compliant solution, the prototype for the function f()
is included in the scope in the source file where it is used, and the function f()
is correctly called with an argument of type int
.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* in another source file */ long f(long x) { return x < 0 ? -x : x; } /* f prototype in scope in this source file */ long f(long x); int g(long x) { return f(x); } |
...
In this noncompliant code example from a vulnerability in the useradd()
function of the shadow-utils
package CVE-2006-1174, the third argument to open()
has been accidentally omitted.:
Code Block | ||||
---|---|---|---|---|
| ||||
fd = open(ms, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC); |
...
To correct this example, a third argument is specified in the call to open()
.:
Code Block | ||||
---|---|---|---|---|
| ||||
/* ... */ fd = open(ms, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC, file_access_permissions); if (fd == -1){ /* Handle error */ } /* ... */ |
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Compass/ROSE | can detect some violations of this rule. In particular, it ensures that all calls to | ||||||||
| callargs | Partially implemented. | |||||||
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. |
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:2013 | Subprogram Signature Mismatch [OTR] |
ISO/IEC TS 17961 (Draft) | Calling functions with incorrect arguments [argcomp] |
MISRA -CC:2012 | Rule 8.2 (required) Rule 16.617.3 (mandatory) |
MITRE CWE | CWE-628, Function call with incorrectly specified arguments |
...