| Wiki Markup |
|---|
Do not cast away a const qualification on a variable type. Casting away the const qualification will allow violation of rule \[[EXP31-C|EXP31-C. Do not modify constant values ]\] prohibiting the modification of constant values. |
...
Otherwise, do not attempt to modify the contents of the array.
Exception
It is acceptable 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 log() function.
| Code Block |
|---|
void log(char *errstr) { fprintf(stderr, "Error: %s.\n", errstr); } ... const char INVFNAME[] = "Invalid file name."; log((char *)INVFNAME); ... |
Priority: P6 Level: L2
If the object really is constant, the compiler may have put it in ROM or write-protected memory. Trying to modify such an object may lead to a program crash. This could allow an attacker to mount a denial of service attack.
...