...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <iostream>
void f(char c) {
char &const p = c;
p = 'p';
std::cout << c << std::endl;
} |
Implementation Details
With Microsoft Visual Studio 2013, this code compiles successfully with a warning diagnostic (warning C4227: anachronism used : qualifiers on reference are ignored) and outputs:
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <iostream>
void f(char c) {
const char &p = c;
p = 'p'; // error, read-only variable is not assignable
std::cout << c << std::endl;
}
|
Risk Assessment
const and volatile reference types may result in undefined behavior instead of a fatal diagnostic, causing unexpected values to be stored and leading to possible data integrity violations.
...
Tool | Version | Checker | Description | ||||||
| 14 | ||||||||
| Clang |
|
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
| [ISO/IEC 14882-2014] | 8.3.2, "References" |
| [Dewhurst 02] | Gotcha #5, "Misunderstanding References" |