| 1 |
const int global_const = 42; |
| 2 |
|
| 3 |
struct TestStruct { |
| 4 |
const int x; |
| 5 |
int y; |
| 6 |
}; |
| 7 |
|
| 8 |
void immutable_violation_examples() { |
| 9 |
*(int *)&global_const = 100; // warn: Trying to write to immutable memory |
| 10 |
|
| 11 |
const int local_const = 42; |
| 12 |
*(int *)&local_const = 43; // warn: Trying to write to immutable memory |
| 13 |
|
| 14 |
// NOTE: The following is reported in C++, but not in C, as the analyzer |
| 15 |
// treats string literals as non-const char arrays in C mode. |
| 16 |
char *ptr_to_str_literal = (char *)"hello"; |
| 17 |
ptr_to_str_literal[0] = 'H'; // warn: Trying to write to immutable memory |
| 18 |
|
| 19 |
TestStruct s = {1, 2}; |
| 20 |
*(int *)&s.x = 10; // warn: Trying to write to immutable memory |
| 21 |
} |