Two or more incompatible declarations of the same function or object must not appear in the same program because they result in undefined behavior. The C Standard, 6.2.7, mentions that two types may be distinct yet compatible and addresses precisely when two distinct types are compatible.
The C Standard identifies four situations in which undefined behavior (UB) may arise as a result of incompatible declarations of the same function or object:
UB | Description | Code |
---|---|---|
Two declarations of the same object or function specify types that are not compatible (6.2.7). | All noncompliant code in this guideline | |
31 | Two identifiers differ only in nonsignificant characters (6.4.2.1). | Excessively Long Identifiers |
An object has its stored value accessed other than by an lvalue of an allowable type (6.5). | Incompatible Object Declarations | |
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). | Incompatible Function Declarations |
Although the effect of two incompatible declarations simply appearing in the same program may be benign on most implementations, the effects of invoking a function through an expression whose type is incompatible with the function definition are typically catastrophic. Similarly, the effects of accessing an object using an lvalue of a type that is incompatible with the object definition may range from unintended information exposure to memory overwrite to a hardware trap.
Anchor | ||||
---|---|---|---|---|
|
In this noncompliant code example, the variable i
is declared to have type int
in file a.c
but defined to be of type short
in file b.c
. The declarations are incompatible, resulting in undefined behavior 15. Furthermore, accessing the object using an lvalue of an incompatible type, as shown in function f()
, is undefined behavior 37 with possible observable results ranging from unintended information exposure to memory overwrite to a hardware trap.
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* In a.c */ extern int i; int f(void) { return ++i; } /* In b.c */ int i; |
Anchor | ||||
---|---|---|---|---|
|
In this noncompliant code example, the variable a
is declared to have a pointer type in file a.c
but defined to have an array type in file b.c
. The two declarations are incompatible, resulting in undefined behavior 15. As before, accessing the object in function f()
is undefined behavior 37 with the typical effect of triggering a hardware trap.
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* In a.c */ extern int a[]; int f(unsigned int i, int x) { int tmp = a[i]; a[i] = x; return tmp; } /* In b.c */ int a[] = { 1, 2, 3, 4 }; |
Anchor | ||||
---|---|---|---|---|
|
In this noncompliant code example, the function f()
is declared in file a.c
with one prototype but defined in file b.c
with another. The two prototypes are incompatible, resulting in undefined behavior 15. Furthermore, invoking the function is undefined behavior 41 and typically has catastrophic consequences.
...
In this noncompliant code example, the function buginf()
is defined to take a variable number of arguments and expects them all to be signed integers , with a sentinel value of -1
:
...
Although this code appears to be well defined because of the prototype-less declaration of of buginf()
, it exhibits undefined behavior in accordance with the C Standard, 6.7.67.34, paragraph 15 14 [ISO/IEC 9899:20112024]:,
For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists , if both are present, shall agree in the number of parameters and in use of the final ellipsis terminator; corresponding parameters shall have compatible types. If one type has a parameter type list and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifier list, the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotionsIn the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.
Compliant Solution (Incompatible Variadic Function Declarations)
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* In a.c */ void buginf(const char *fmt, ...) { /* ... */ } /* In b.c */ void buginf(const char *fmt, ...); |
Anchor | ||||
---|---|---|---|---|
|
In this noncompliant code example, the length of the identifier declaring the function pointer bash_groupname_completion_function()
in the file bashline.h
exceeds by 3 the minimum implementation limit of 31 significant initial characters in an external identifier, introducing . This introduces the possibility of colliding with the bash_groupname_completion_funct
integer variable defined in file b.c
, which is exactly 31 characters long. On an implementation that exactly meets this limit, this is undefined behavior 31. It results in two incompatible declarations of the same function. (see See undefined behavior 15.) . In addition, invoking the function leads to undefined behavior 41 with typically catastrophic effects.
...
NOTE: The identifier bash_groupname_completion_function
referenced here was taken from GNU Bash, version 3.2.
Compliant Solution (Excessively Long Identifiers)
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* In bashline.h */ extern char * bash_groupname_completion(const char *, int); /* In a.c */ #include "bashline.h" void f(const char *s, int i) { bash_groupname_completion(s, i); } /* In b.c */ int bash_groupname_completion_funct; |
Risk Assessment
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL40-C | Low | Unlikely | Medium | P2 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| type-compatibility type-compatibility-link distinct-extern | Fully checked | ||||||
Axivion Bauhaus Suite |
| CertC-DCL40 | Fully implemented | ||||||
CodeSonar |
| LANG.STRUCT.DECL.IF LANG.STRUCT.DECL.IO | Inconsistent function declarations Inconsistent object declarations | ||||||
Coverity |
| MISRA C 2012 Rule 8.4 | Implemented | ||||||
Cppcheck Premium |
| premium-cert-dcl40-c | Partially implemented | ||||||
Helix QAC |
| C0776, C0778, C0779, C0789, C1510 C++1510 | Fully implemented | ||||||
Klocwork |
| MISRA.FUNC.NOPROT.DEF.2012 | Fully implemented | ||||||
LDRA tool suite | 8.5.4 | 1 X, 17 D | Partially implemented | ||||||
Parasoft |
C/C++test |
|
Related Guidelines
| CERT_C-DCL40-a | All declarations of an object or function shall have compatible types | |||||||
Parasoft Insure++ | Runtime analysis | ||||||||
PC-lint Plus |
| 18, 621, 793, 4376 | Fully supported | ||||||
Polyspace Bug Finder |
| Checks for declaration mismatch (rule fully covered) | |||||||
RuleChecker |
| type-compatibility type-compatibility-link distinct-extern
| Fully checked | ||||||
TrustInSoft Analyzer |
| incompatible declaration | Exhaustively verified. |
Related Guidelines
Key here (explains table format and definitions)
Taxonomy | Taxonomy item | Relationship |
---|---|---|
ISO/IEC TS 17961 | Declaring the same function or object in incompatible ways [funcdecl] | Prior to 2018-01-12: CERT: Unspecified Relationship |
MISRA C:2012 | Rule 8.4 (required) | Prior to 2018-01-12: CERT: Unspecified Relationship |
Bibliography
[Hatton 1995] | Section 2.8.3 |
[ISO/IEC 9899:2011] | J.2, "Undefined Behavior" |
[ISO/IEC 9899:2024] | 6.7. |
7. |
4 "Function Declarators |
J.2,
" |
...
...