The C standardStandard, Section 6.7.3, para. 6 [ISO/IEC 9899:2011], states:
If an attempt is made to modify an object defined with a
const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.
(See also undefined behavior 64 of in Appendix J of the C Standard.)
There are existing compiler implementations that allow const-qualified values to be modified without generating a warning message.
...
The following well-formed but noncompliant code example borrowed from Section 6.5.16.1 of C11 the C Standard allows a constant value to be modified.
...
If cpp, cp, and c are declared with static storage duration, this program terminates abnormally for both MS Visual Studio and GCC Version 3.2.2.
Compliant Solution
The compliant solution depends on the intention of the programmer. If the intention is that the value of c is modifiable, then it should not be declared as a constant. If the intention is that the value of c is not meant to change, then do not write noncompliant code that attempts to modify it.
...
Modifying constant objects through non-constant nonconstant references results in undefined behavior.
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
Bibliography
...
| ] | Section 6.7.3, "Type qualifiers," and Section 6.5.16.1, "Simple |
|---|
...
| Assignment" |
|---|
...
...