
This adds alpha.core.StoreToImmutable, a new alpha checker that detects writes to immutable memory regions, implementing part of SEI CERT Rule ENV30-C. The original proposal only handled global const variables, but this implementation extends it to also detect writes to: - Local const variables - String literals - Const parameters and struct members - Const arrays and pointers to const data This checker is the continuation of the work started by zukatsinadze. Discussion: https://reviews.llvm.org/D124244
73 lines
2.3 KiB
C++
73 lines
2.3 KiB
C++
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable -std=c++17 -verify %s
|
|
|
|
void test_write_to_const_ref_param(const int ¶m) {
|
|
*(int*)¶m = 100; // expected-warning {{Trying to write to immutable memory}}
|
|
}
|
|
|
|
// FIXME: This should warn in C mode too.
|
|
void test_write_to_string_literal() {
|
|
char *str = (char*)"hello";
|
|
str[0] = 'H'; // expected-warning {{Trying to write to immutable memory}}
|
|
}
|
|
|
|
struct ParamStruct {
|
|
const int z; // expected-note {{Memory region is declared as immutable here}}
|
|
int w;
|
|
};
|
|
|
|
void test_write_to_const_struct_ref_param(const ParamStruct &s) {
|
|
*(int*)&s.z = 100; // expected-warning {{Trying to write to immutable memory}}
|
|
}
|
|
|
|
void test_const_ref_to_nonconst_data() {
|
|
int data = 42;
|
|
const int &ref = data;
|
|
*(int*)&ref = 100; // No warning expected
|
|
}
|
|
|
|
void test_const_ref_to_const_data() {
|
|
const int data = 42; // expected-note {{Memory region is declared as immutable here}}
|
|
const int &ref = data;
|
|
*(int*)&ref = 100; // expected-warning {{Trying to write to immutable memory}}
|
|
}
|
|
|
|
void test_ref_to_nonconst_data() {
|
|
int data = 42;
|
|
int &ref = data;
|
|
ref = 100; // No warning expected
|
|
}
|
|
|
|
void test_ref_to_const_data() {
|
|
const int data = 42; // expected-note {{Memory region is declared as immutable here}}
|
|
int &ref = *(int*)&data;
|
|
ref = 100; // expected-warning {{Trying to write to immutable memory}}
|
|
}
|
|
|
|
struct MultipleLayerStruct {
|
|
MultipleLayerStruct();
|
|
const int data; // expected-note {{Memory region is declared as immutable here}}
|
|
const int buf[10]; // expected-note {{Enclosing memory region is declared as immutable here}}
|
|
};
|
|
|
|
MultipleLayerStruct MLS[10];
|
|
|
|
void test_multiple_layer_struct_array_member() {
|
|
int *p = (int*)&MLS[2].data;
|
|
*p = 4; // expected-warning {{Trying to write to immutable memory}}
|
|
}
|
|
|
|
void test_multiple_layer_struct_array_array_member() {
|
|
int *p = (int*)&MLS[2].buf[3];
|
|
*p = 4; // expected-warning {{Trying to write to immutable memory}}
|
|
}
|
|
|
|
struct StructWithNonConstMember {
|
|
int x;
|
|
};
|
|
|
|
const StructWithNonConstMember SWNCM{0}; // expected-note {{Enclosing memory region is declared as immutable here}}
|
|
|
|
void test_write_to_non_const_member_of_const_struct() {
|
|
*(int*)&SWNCM.x = 100; // expected-warning {{Trying to write to immutable memory in global read-only storage}}
|
|
}
|