...
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()
, results in is 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 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 is undefined behavior 37 with the typical effect of triggering a hardware trap.
Code Block | ||||
---|---|---|---|---|
| ||||
/* In a.c */
extern int *a; /* UB 15 */
int f(unsigned int 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 */
|
...
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 }; |
...
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 is undefined behavior 41, and typically with has catastrophic effectsconsequences.
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; } |
...
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. It 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.
Code Block | ||||
---|---|---|---|---|
| ||||
/* In bashline.h */ /* UB 15, UB 31 */ extern char * bash_groupname_completion_function(const char *, int); /* 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 */ |
NoteNOTE: The identifier bash_groupname_completion_function
referenced here was taken from GNU Bash version 3.2.
...
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.
...