
Do not cast away a const
qualification on an object of pointer type. Casting away the const
qualification allows a program to modify the object referred to by the pointer, which may result in undefined behavior. See undefined behavior 61 of in Appendix J of the C Standard.
As an illustration, C99 the C Standard [ISO/IEC 9899:2011] provides a footnote (subclause 6.7.3, paragraph 4):
The implementation may place a
const
object that is not volatile in a read-only region of storage. Moreover, the implementation need not allocate storage for such an object if its address is never used.
...
The remove_spaces()
function in this noncompliant code example accepts a pointer to a string str
and a string length slen
and removes the space character from the string by shifting the remaining characters toward the front of the string. The function remove_spaces()
is passed a const
char
pointer as an argument. The const
qualification is cast away, and then the contents of the string are modified.
Code Block | ||||
---|---|---|---|---|
| ||||
void remove_spaces(const char *str, size_t slen) {
char *p = (char *)str;
size_t i;
for (i = 0; i < slen && str[i]; i++) {
if (str[i] != ' ') *p++ = str[i];
}
*p = '\0';
}
|
...
In this compliant solution, the function remove_spaces()
is passed a non-const
char
pointer. The calling function must ensure that the null-terminated byte string passed to the function is not const
by making a copy of the string or by other means.
Code Block | ||||
---|---|---|---|---|
| ||||
void remove_spaces(char *str, size_t slen) {
char *p = str;
size_t i;
for (i = 0; i < slen && str[i]; i++) {
if (str[i] != ' ') *p++ = str[i];
}
*p = '\0';
}
|
...
In this noncompliant code example, the contents of the const
int
array vals
are cleared by the call to memset()
.:
Code Block | ||||
---|---|---|---|---|
| ||||
const int vals[3] = {3, 4, 5};
memset(vals, 0, sizeof(vals));
|
...
Implementation Details
The GCC compiler GCC issues a warning when an implicit cast is performed.
...
If the intention is to allow the array values to be modified, do not declare the array as const
.:
Code Block | ||||
---|---|---|---|---|
| ||||
int vals[3] = {3, 4, 5};
memset(vals, 0, sizeof(vals));
|
Otherwise, do not attempt to modify the contents of the array.
Exceptions
EXP05-C-EX1: An exception to this recommendation is allowed when it is necessary to cast away const
when invoking a legacy API that does not accept a const
argument, provided the function does not attempt to modify the referenced variable. For example, the following code casts away the const
qualification of INVFNAME
in the call to the audit_log()
function.
Code Block |
---|
/* Legacy function defined elsewhereelsewhere—cannot - cannot be modified */ void audit_log(char *errstr) { fprintf(stderr, "Error: %s.\n", errstr); } /* ... */ const char INVFNAME[] = "Invalid file name."; audit_log((char *)INVFNAME); /* EXP05-EX1 */ /* ... */ |
EXP05-C-EX2: A number of C99 C standard library functions are specified to return non-const
pointers that refer to their const
-qualified arguments. When the actual arguments to such functions reference const
objects, attempting to use the returned non-const
pointers to modify the const
objects would be a violation of guideline EXP40-C. Do not modify constant valuesobjects and would lead to undefined behavior. These functions are the following:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For instance, in following example, the function strchr
returns an unqualified char*
that points to the terminating null character of the constant character array s
(which could be stored in ROM). Even though the pointer is not const
, attempting to modify the character it points to would lead to undefined behavior.
Code Block |
---|
extern const char s[]; char* where; where = strchr(s, '\0'); /* modifyingModifying *s is undefined */ |
Similarly, in the following example below, the function strtol
sets the unqualified char*
pointer referenced by end
to point just past the last successfully parsed character of the constant character array s
(which could be stored in ROM). Even though the pointer is not const
, attempting to modify the character it points to would lead to undefined behavior.
Code Block |
---|
extern const char s[]; long x; char* end; x = strtol(s, &end, 0); /* modifyingModifying **end is undefined */ |
EXP05-C-EX3: Because " const
means " means read-only," and not "constant," it is sometimes useful to declare struct
members as (pointer to) const
objects to obtain diagnostics when the user tries to change them in some way other than via the functions that are specifically designed to maintain that data type. Within those functions, however, it may be necessary to strip off the const
qualification to update those members.
...
If the object is constant, the compiler may allocate storage in ROM or write-protected memory. Attempting to modify such an object may lead to a program crash or denial-of-service attack.
Recommendation | Severity | Likelihood | Detectable |
---|
Repairable | Priority | Level |
---|---|---|
EXP05-C |
Medium | Probable |
No |
No |
P4 |
L3 |
Automated Detection
Tool | Version | Checker | Description |
---|
Section |
---|
Astrée |
| pointer-qualifier-cast-const pointer-qualifier-cast-const-implicit | Fully checked | ||||||
Axivion Bauhaus Suite |
| CertC-EXP05 | Fully implemented | ||||||
CodeSonar |
| LANG.CAST.PC.CRCQ | Cast removes const qualifier | ||||||
Compass/ROSE | |||||||||
| CC2.EXP05 | Fully implemented |
GCC |
|
|
|
Can detect violations of this recommendation when the | |||||||||
Helix QAC |
| C0311, C0431 | |||||||
LDRA tool suite |
| 203 S | Fully implemented | ||||||
Parasoft C/C++test |
| CERT_C-EXP05-a | A cast shall not remove any 'const' or 'volatile' qualification from the type of a pointer or reference | ||||||
PC-lint Plus |
| 9005 | Partially supported | ||||||
Polyspace Bug Finder |
|
Section |
---|
Compass/ROSE |
Checks for cast to pointer that removes const qualification (rec. fully supported) | |||||||||
RuleChecker |
| pointer-qualifier-cast-const pointer-qualifier-cast-const-implicit | Fully checked |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
...
...
...
...
access a cv-qualified object through a cv-unqualified type | |
ISO/IEC TR 24772:2013 | Pointer Casting and Pointer Type Changes [HFC] Type System [IHN] |
MISRA C:2012 | Rule 11.8 (required) |
MITRE CWE | CWE-704, Incorrect type conversion or cast |
Bibliography
[ISO/IEC 9899:2011] | Subclause 6.7.3, "Type Qualifiers" |
...
Bibliography
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 6.7.3, "Type qualifiers"
\[[ISO/IEC PDTR 24772|AA. Bibliography#ISO/IEC PDTR 24772]\] "HFC Pointer casting and pointer type changes" and "IHN Type system"
\[[MISRA 2004|AA. Bibliography#MISRA 04]\] Rule 11.5
\[[MITRE 2007|AA. Bibliography#MITRE 07]\] [CWE ID 704|http://cwe.mitre.org/data/definitions/704.html], "Incorrect Type Conversion or Cast" |
EXP04-C. Do not perform byte-by-byte comparisons involving a structure 03. Expressions (EXP) EXP06-C. Operands to the sizeof operator should not contain side effects