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 The C Standard, 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 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 Excessively Long Identifiers |
...
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 done shown 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 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 results in undefined behavior 41 with , typically with catastrophic effects.
Code Block | ||||
---|---|---|---|---|
| ||||
/* 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; } |
...
This compliant solution has compatible declarations of prototypes for the function f()
:
Code Block | ||||
---|---|---|---|---|
| ||||
/* 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; } |
...
Although this code appears to be well defined because of the prototype-less declaration of buginf()
, it exhibits undefined behavior per subclause in accordance with the C Standard, 6.7.6.3, paragraph 15 , of the C Standard [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.
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* In bash/bashline.h */ /* UB 15, UB 31 */ extern char * bash_groupname_completion_function(const char *, int); /* UB 15, UB 31 */ /* In a.c */ #include <bashline"bashline.h>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.
...
Code Block | ||||
---|---|---|---|---|
| ||||
/* In bash/bashline.h */ extern char * bash_groupname_completion(const char *, int); /* In a.c */ #include <bashline"bashline.h>h" void f(const char *s, int i) { bash_groupname_completion(s, i); } /* In b.c */ int bash_groupname_completion_funct; |
...