Two or more incompatible declarations of the same function or object must not appear in the same program because they result in undefined behavior. Subclause 6.2.7 of the C standard 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, Incompatible Array 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, Excessively Long Identifiers |
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.
Noncompliant Code Example (Incompatible Object Declarations)
In this noncompliant code example, 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 undefined behavior 15. Furthermore, accessing the object using an lvalue of an incompatible type as done in function f()
results in undefined behavior 37 with possible observable results ranging from unintended information exposure to memory overwrite to a hardware trap.
/* In a.c */ extern int i; /* UB 15 */ int f(void) { return ++i; /* UB 37 */ } /* In b.c */ short i; /* UB 15 */
Compliant Solution (Incompatible Object Declarations)
This compliant solution has compatible declarations of the variable i
:
/* In a.c */ extern int i; int f(void) { return ++i; } /* In b.c */ int i;
Noncompliant Code Example (Incompatible Array Declarations)
In this noncompliant code, the variable a
is declared to have pointer type in file a.c
but defined to have array type in file b.c
. The two declarations are incompatible, resulting in undefined behavior 15. As before, accessing the object in function f()
results in undefined behavior 37 with the typical effect of triggering a hardware trap.
/* In a.c */ extern int *a; /* UB 15 */ int f(unsigned i, int x) { int tmp = a[i]; /* UB 37: read access */ a[i] = x; /* UB 37: write access */ return tmp; } /* In b.c */ int a[] = { 1, 2, 3, 4 }; /* UB 15 */
Compliant Solution (Incompatible Array Declarations)
This compliant solution declares a
as an array in a.c
and b.c
:
/* In a.c */ extern int a[]; int f(unsigned i, int x) { int tmp = a[i]; a[i] = x; return tmp; } /* In b.c */ int a[] = { 1, 2, 3, 4 };
Noncompliant Code Example (Incompatible Function Declarations)
In this noncompliant code example, 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 results in undefined behavior 41 with typically catastrophic effects.
/* In a.c */ extern int f(int a); /* UB 15 */ int g(int a) { return f(a); /* UB 41 */ } /* In b.c */ long f(long a) { /* UB 15 */ return a * 2; }
Compliant Solution (Incompatible Function Declarations)
This compliant solution has compatible declarations of the function f()
:
/* In a.c */ extern int f(int a); int g(int a) { return f(a); } /* In b.c */ int f(int a) { return a * 2; }
Noncompliant Code Example (Incompatible Variadic Function Declarations)
In this noncompliant 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
:
/* In a.c */ void buginf(const char *fmt, ...) { /* ... */ } /* In b.c */ void buginf();
While this code appears to be well-defined due to the prototype-less declaration of buginf()
,this code exhibits undefined behavior per subclause 6.7.6.3 paragraph 15 [ISO/IEC 9899:2011]:
For two function types to be compatible, both shall specify compatible return types.146) Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the 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 promotions.
Compliant Solution (Incompatible Variadic Function Declarations)
In this compliant solution, the prototype for the function buginf()
is included in the scope in the source file where it will be used:
/* In a.c */ void buginf(const char *fmt, ...) { /* ... */ } /* In b.c */ void buginf(const char *fmt, ...);
Noncompliant Code Example (Excessively Long Identifiers)
In this noncompliant code example, the length of the identifier declaring the function pointer bash_groupname_completion_function()
in file bashline.h
exceeds by 3 the minimum implementation limit of 31 significant initial characters in an external identifier, introducing 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 a violation of undefined behavior 31. This results in two incompatible declarations of the same function (see undefined behavior 15). In addition, invoking the function leads to undefined behavior 41 with typically catastrophic effects.
/* In bash/bashline.h */ extern char* bash_groupname_completion_function(const char*, int); /* UB 15, UB 31 */ /* In a.c */ #include <bashline.h> void f(const char *s, int i) { bash_groupname_completion_function(s, i); /* UB 41 */ } /* In b.c */ int bash_groupname_completion_funct; /* UB 15, UB 31 */
Note: The identifier bash_groupname_completion_function
referenced here was taken from GNU Bash version 3.2.
Compliant Solution (Excessively Long Identifiers)
In this compliant solution, the length of the identifier declaring the function pointer bash_groupname_completion()
in bashline.h
is less than 32 characters. Consequently it cannot clash with bash_groupname_completion_funct
on any compliant platform.
/* In bash/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 |
---|---|---|---|
8.5.4 | 1 X | Fully implemented | |
PRQA QA-C | 8.1 | 1510 | Fully implemented |
Related Guidelines
ISO/IEC TS 17961 | Declaring the same function or object in incompatible ways [funcdecl] |
Bibliography
[Hatton 1995] | Section 2.8.3 |
[ISO/IEC 9899:2011] | Subclause J.2, "Undefined behavior" |