...
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 (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.
Code Block |
---|
|
/* inIn a.c */
extern int i; /* UB 15 */
int f(void) {
return ++i; /* UB 37 */
}
/* inIn b.c */
short i; /* UB 15 */
|
...
Code Block |
---|
|
/* inIn a.c */
extern int i;
int f(void) {
return ++i;
}
/* inIn b.c */
int i; |
Noncompliant Code Example (Array Declarations)
In this noncompliant code, the variable a
is declared to have array type in file a.c
but defined to have pointer 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.
Code Block |
---|
|
/* inIn 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;
}
/* inIn b.c */
int a[] = { 1, 2, 3, 4 }; /* UB 15 */
|
...
Code Block |
---|
|
/* inIn a.c */
extern int a[];
int f(unsigned i, int x) {
int tmp = a[i];
a[i] = x;
return tmp;
}
/* inIn b.c */
int a[] = { 1, 2, 3, 4 }; |
Noncompliant Code Example (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.
Code Block |
---|
|
/* inIn a.c */
extern int f(int a); /* UB 15 */
int g(int a) {
return f(a); /* UB 41 */
}
/* inIn b.c */
long f(long a) { /* UB 15 */
return a * 2;
}
|
...
Code Block |
---|
|
/* inIn a.c */
extern int f(int a);
int g(int a) {
return f(a);
}
/* inIn b.c */
int f(int a) {
return a * 2;
} |
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_func
integer variable defined in file b.c
, which is exactly 30 characters long. On an implementation that exactly meets this limit, the behavior of the program is undefined (see undefined behavior 15). In addition, invoking the function leads to undefined behavior 41 with typically catastrophic effects.
Code Block |
---|
|
/* inIn bash/bashline.h */
extern char* bash_groupname_completion_function(const char*, int); /* UB 15 */
/* inIn a.c */
#include <bashline.h>
void f(const char *s, int i) {
bash_groupname_completion_function(s, i); /* UB 41 */
}
/* inIn b.c */
int bash_groupname_completion_func; /* UB 15 */
|
...
Code Block |
---|
|
/* inIn bash/bashline.h */
extern char* bash_groupname_completion(const char*, int);
/* inIn a.c */
#include <bashline.h>
void f(const char *s, int i) {
bash_groupname_completion(s, i);
}
/* inIn b.c */
int bash_groupname_completion_func; |
...
DCL40-EX1: No diagnostic need be issued if a declaration that is incompatible with the definition occurs in a translation unit that does not contain any definition or uses of the function or object other than possibly additional declarations.
Code Block |
---|
|
/* a.c: */
int x = 0; /* theThe definition */
/* b.c: */
extern char x; /* incompatibleIncompatible declaration */
/* but no other references to 'x' */
|
Related Guidelines
...