Immutable (constant) values should be declared as enumeration constants, const
-qualified objects (unmodifiable lvalues), or as macro definitions using #define
.
Enforcing invariants such as object immutability using existing language mechanisms helps ensures the safety and security of applications. For example, ISO/IEC PDTR 24772 \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] recommends labeling parameters as constants to avoid unintentional modification. |
In general, it is preferable to declare immutable values as const
-qualified objects rather than as macro definitions. Using a const
declared value means that the compiler is able to check the type of the object, the object has scope, and (certain) debugging tools can show the name of the object. However, for integer constants, it is preferable to use an enumeration constant instead of a const
-qualified object, as this eliminates the possibility of taking the address of the integer constant and does not require that storage is allocated for the value.
Unfortunately, const
-qualified objects cannot be used where compile-time integer constants are required, namely to define the
case
constant#define:
const:
If any of these are required, then an integer constant (which would be an rvalue) must be used.
Method |
Evaluated at |
Consumes Memory |
Viewable by Debuggers |
Type Checking |
Compile-time constant expression |
---|---|---|---|---|---|
Enumerations |
compile time |
no |
yes |
yes |
no |
|
run time |
yes |
yes |
yes |
no |
Macros |
preprocessor |
no |
no |
no |
yes |
This recommendation is related to EXP07-A. Do not diminish the benefits of constants by assuming their values in expressions.
A preprocessing directive of the form:
#
define
identifier replacement-list
defines an _object-like_ macro that causes each subsequent instance of the macro name to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]. |
In this non-compliant code example, PI
is defined as an object-like macro. Following the definition, each subsequent occurrence of the string "PI" is replaced by the string "3.14159f" by textual substitution.
#define PI 3.14159f float degrees; float radians; /* ... */ radians = degrees * PI / 180; |
In this compliant solution, pi
is declared as a const
-qualified object, allowing the constant to have scope and to have its value inspected by a debugger.
const float pi = 3.14159f; float degrees; float radians; /* ... */ radians = degrees * pi / 180; |
While inadequate in some ways, this is the best that can be done for non-integer constants.
In this non-compliant code example, max
is declared as a const
-qualified object. While declaring non-integer constants as const
-qualified objects is the best that can be done in C, for integer constants we can do better. Declaring immutable integer values as const
-qualified objects still allows the programmer to take the address of the object. Also, const
-qualified integers cannot be used in locations where an integer constant is required, such as the value of a case
constant.
const int max = 15; int a[max]; /* invalid declaration outside of a function */ const int *p; p = &max; /* a const-qualified object can have its address taken */ |
Most C compilers allocate memory for const
-qualified objects.
enum
)This compliant solution declares max
as an enumeration constant rather than a const
-qualified object or a macro definition.
enum { max = 15 }; int a[max]; /* OK */ const int *p; p = &max; /* error: '&' on constant */ |
DCL00-EX1 Constant values that may be passed as compile-time arguments must be macro definitions, as shown by this example:
#ifndef MYPORTNUMBER /* might be passed on compile line */ # define MYPORTNUMBER 1234 #endif |
Using ordinary variables to hold constants instead of using enumeration constants or const
-qualified objects can result in a value intended to be constant being changed at runtime.
Recommendation |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
DCL00-A |
1 (low) |
1 (unlikely) |
2 (medium) |
P2 |
L3 |
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
\[[Summit 05|AA. C References#Summit 05]\] [Question 10.5b|http://c-faq.com/cpp/constvsdefine.html] \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.3.2.1, "Lvalues, arrays, and function designators," Section 6.7.2.2, "Enumeration specifiers," and Section 6.10.3, "Macro replacement" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "CSJ Passing parameters and return values" |
02. Declarations and Initialization (DCL) 02. Declarations and Initialization (DCL) DCL01-A. Do not reuse variable names in subscopes