...
Functions that are appropriately declared (as in DCL07-C. Include the appropriate type information in function declarators) will typically fail compilation if they are supplied with the wrong number or types of arguments. However, there are cases where in which supplying the incorrect arguments to a function will, at best, generate compiler warnings. These warnings should be resolved but do not prevent program compilation.(See MSC00-C. Compile cleanly at high warning levels.)
...
In this compliant solution, the function pointer fp
points to a function returning char *
, with the correct number and type of arguments:
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* inIn another source file */ void copy(char *dst, const char *src) { if (!strcpy(dst, src)) { /* reportReport error */ } } /* inIn this source file -- no copy prototype in scope */ void copy(); void g(const char *s) { char buf[20]; copy(buf, s, sizeof buf); /* violationViolation */ /* ... */ } |
Compliant Solution
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* inIn another source file */ void copy(char *dst, const char *src) { if (!strcpy(dst, src)) { /* reportReport error */ } } /* copyCopy 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); /* ... */ } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* inIn another source file */ void buginf(const char *fmt, ...) { /* ... */ } /* inIn this source file -- no buginf prototype in scope */ void buginf(); void h(void) { buginf("bug in function %s, line %d\n", __func__, __LINE__); /* violationViolation */ /* ... */ } |
Compliant Solution
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* inIn 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__); /* ... */ } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* inIn another source file */ long f(long x) { return x < 0 ? -x : x; } /* inIn this source file -- no f prototype in scope */ int g(int x) { return f(x); /* violationViolation */ } |
Compliant Solution
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 | ||||
---|---|---|---|---|
| ||||
/* inIn 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); } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
fd = open(ms, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC); |
Note that, technically, it is also incorrect to pass a third argument to open()
when not creating a new file (that is, with the O_CREAT
flag not set). A POSIX implementation could, if it wished, return an EINVAL error in this case. However, in practice, it is unlikely to cause a problem.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Compass/ROSE | can Can detect some violations of this rule. In particular, it ensures that all calls to | ||||||||
| CC2.EXP37 | 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 |
...