[clang] [clang][analyzer] Add StoreToImmutable checker (PR #150417)

Endre Fülöp via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 30 06:07:41 PDT 2025


================
@@ -0,0 +1,120 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable -verify %s
+
+// Test basic functionality of StoreToImmutable checker for the C programming language.
+
+const int tentative_global_const; // expected-note {{Memory region is in immutable space}}
+
+void test_direct_write_to_tentative_const_global() {
+  *(int*)&tentative_global_const = 100; // expected-warning {{Writing to immutable memory is undefined behavior. This memory region is marked as immutable and should not be modified}}
+}
+
+const int global_const = 42; // expected-note {{Memory region is in immutable space}}
+
+void test_direct_write_to_const_global() {
+  // This should trigger a warning about writing to immutable memory
+  *(int*)&global_const = 100; // expected-warning {{Writing to immutable memory is undefined behavior. This memory region is marked as immutable and should not be modified}}
+}
+
+void test_write_through_const_pointer() {
+  const int local_const = 10; // expected-note {{Memory region is in immutable space}}
+  int *ptr = (int*)&local_const;
+  *ptr = 20; // expected-warning {{Writing to immutable memory is undefined behavior. This memory region is marked as immutable and should not be modified}}
+}
+
+void test_write_to_const_array() {
+  const int arr[5] = {1, 2, 3, 4, 5};
----------------
gamesh411 wrote:

Could give a note to the enclosing array declaration.

https://github.com/llvm/llvm-project/pull/150417


More information about the cfe-commits mailing list