================
@@ -0,0 +1,166 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable 
-verify %s
+
+// Test basic functionality of StoreToImmutable checker
+// This tests direct writes to immutable regions without function modeling
+
+// Direct write to a const global variable
+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}}
+  // expected-note@-1 {{Writing to immutable memory is undefined behavior. 
This memory region is marked as immutable and should not be modified}}
----------------
NagyDonat wrote:

I'm also very surprised to see that `note` diagnostics are visible without 
`-analyzer-output=text` but I don't have a strong opinion about whether this is 
a problem or not.

> It may have something to do with the raw use of the addNote API, which is not 
> usually done. We usually use BugReport visitors for putting down notes and 
> probably their behavior is different. [...]
>
> I tried to grep for one case when we emit notes from a static analyzer 
> checker and could not find one.

Just a quick drive-by remark: `CStringChecker::emitUninitializedReadBug` also 
adds a note with a direct `addNote` call instead of using a bug reporter 
visitor.

https://github.com/llvm/llvm-project/pull/150417
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to