122 lines · c
1// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable -verify %s2 3// Test basic functionality of StoreToImmutable checker for the C programming language.4 5const int tentative_global_const; // expected-note {{Memory region is declared as immutable here}}6 7void test_direct_write_to_tentative_const_global() {8 *(int*)&tentative_global_const = 100; // expected-warning {{Trying to write to immutable memory in global read-only storage}}9}10 11const int global_const = 42; // expected-note {{Memory region is declared as immutable here}}12 13void test_direct_write_to_const_global() {14 // This should trigger a warning about writing to immutable memory15 *(int*)&global_const = 100; // expected-warning {{Trying to write to immutable memory in global read-only storage}}16}17 18void test_write_through_const_pointer() {19 const int local_const = 10; // expected-note {{Memory region is declared as immutable here}}20 int *ptr = (int*)&local_const;21 *ptr = 20; // expected-warning {{Trying to write to immutable memory}}22}23 24void test_write_to_const_array() {25 const int arr[5] = {1, 2, 3, 4, 5}; // expected-note {{Enclosing memory region is declared as immutable here}}26 int *ptr = (int*)arr;27 ptr[0] = 10; // expected-warning {{Trying to write to immutable memory}}28}29 30struct TestStruct {31 const int x; // expected-note 2 {{Memory region is declared as immutable here}}32 int y;33};34 35void test_write_to_const_struct_member() {36 struct TestStruct s = {1, 2};37 int *ptr = (int*)&s.x;38 *ptr = 10; // expected-warning {{Trying to write to immutable memory}}39}40 41const int global_array[3] = {1, 2, 3}; // expected-note {{Enclosing memory region is declared as immutable here}}42 43void test_write_to_const_global_array() {44 int *ptr = (int*)global_array;45 ptr[0] = 10; // expected-warning {{Trying to write to immutable memory in global read-only storage}}46}47 48const struct TestStruct global_struct = {1, 2};49 50void test_write_to_const_global_struct() {51 int *ptr = (int*)&global_struct.x;52 *ptr = 10; // expected-warning {{Trying to write to immutable memory in global read-only storage}}53}54 55 56void test_write_to_const_param(const int param) { // expected-note {{Memory region is declared as immutable here}}57 *(int*)¶m = 100; // expected-warning {{Trying to write to immutable memory}}58}59 60void test_write_to_const_ptr_param(const int *param) {61 *(int*)param = 100; // expected-warning {{Trying to write to immutable memory}}62}63 64void test_write_to_const_array_param(const int arr[5]) {65 *(int*)arr = 100; // expected-warning {{Trying to write to immutable memory}}66}67 68struct ParamStruct {69 const int z; // expected-note 2 {{Memory region is declared as immutable here}}70 int w;71};72 73void test_write_to_const_struct_param(const struct ParamStruct s) {74 *(int*)&s.z = 100; // expected-warning {{Trying to write to immutable memory}}75}76 77void test_write_to_const_struct_ptr_param(const struct ParamStruct *s) {78 *(int*)&s->z = 100; // expected-warning {{Trying to write to immutable memory}}79}80 81void test_write_to_nonconst() {82 int non_const = 42;83 *(int*)&non_const = 100; // No warning expected84}85 86int global_non_const = 42;87 88void test_write_to_nonconst_global() {89 *(int*)&global_non_const = 100; // No warning expected90}91 92struct NonConstStruct {93 int x;94 int y;95};96 97void test_write_to_nonconst_struct_member() {98 struct NonConstStruct s = {1, 2};99 *(int*)&s.x = 100; // No warning expected100}101 102void test_write_to_nonconst_param(int param) {103 *(int*)¶m = 100; // No warning expected104}105 106void test_normal_assignment() {107 int x = 42;108 x = 100; // No warning expected109}110 111void test_const_ptr_to_nonconst_data() {112 int data = 42;113 const int *ptr = &data;114 *(int*)ptr = 100; // No warning expected115}116 117void test_const_ptr_to_const_data() {118 const int data = 42; // expected-note {{Memory region is declared as immutable here}}119 const int *ptr = &data;120 *(int*)ptr = 100; // expected-warning {{Trying to write to immutable memory}}121} 122