Why is this an issue?
Defining or declaring identifiers with reserved names may lead to undefined behavior. Similarly, defining macros, variables or functions/methods
with the same names as functions from the C standard library is likely to lead to unexpected results.
Additionally, such identifiers have the potential to thoroughly confuse people who are unfamiliar with the code base, possibly leading them to
introduce additional errors. Therefore reserved words and the names of C standard library functions should not be used as identifiers.
This rule applies to:
-
defined
- C standard library function names
- identifiers that contain two consecutive underscores
- identifiers that begin with an underscore, followed by an uppercase letter
- identifiers in the global namespace that start with an underscore
Noncompliant code example
#ifndef _MY_FILE
#define _MY_FILE // Noncompliant: starts with '_'
#define FIELD__VAL(field) ##field // Noncompliant: contains "__"
int free(void *pArg, int len) { // Noncompliant: free is a standard function
int __i; // Noncompliant: starts with "__"
//...
}
#endif
Compliant solution
#ifndef MY_FILE
#define MY_FILE
#define FIELD_VAL(field) ##field
int clean(void *pArg, int len) {
int i;
//...
}
#endif
Resources
- MISRA C:2004, 20.1 - Reserved identifiers, macros and functions in the standard library, shall not be defined redefined or undefined.
- MISRA C++:2008, 17-0-1 - Reserved identifiers, macros and functions in the standard library shall not be defined, redefined, or undefined.
- MISRA C:2012, 21.2 - A reserved identifier or macro name shall not be declared
- CERT, DCL37-C. - Do not declare or define a reserved identifier
- CERT, DCL51-CPP. - Do not declare or define a reserved identifier