Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added compliant solutions

...

Code Block
bgColor#FFcccc
languagec
/* in a.c */
extern int i;   /* UB 15 */

int f(void) {
  return ++i;   /* UB 37 */
}

/* in b.c */
short i;   /* UB 15 */

Compliant Solution

This compliant solution has compatible declarations of the variable i

Code Block
bgColor#ccccff
langc
/* in a.c */
extern int i;   

int f(void) {
  return ++i;   
}

/* in b.c */
int i;   

Noncompliant Code Example (Array Declarations)

...

Code Block
bgColor#FFcccc
languagec
/* 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

This compliant solution declares a as an array in a.c and b.c

Code Block
bgColor#ccccff
langc
/* 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 (Function Declarations)

...

Code Block
bgColor#FFcccc
languagec
/* 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

This compliant solution has compatible declarations of the function f()

Code Block
bgColor#ccccff
langc
/* 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 (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.

...

Note: The identifier bash_groupname_completion_function referenced here was taken from GNU Bash version 3.2.

Compliant Solution

In this compliant solution the length of the identifier declaring the function pointer bash_groupname_completion() in bashline.h is less than 31 characters. 

Code Block
bgColor#ccccff
langc
/* 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_func; 

Exceptions

Anchor
DCL40-EX1
DCL40-EX1
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.

...