[clang] [LifetimeSafety] Enable C support by default under -Wlifetime-safety (PR #224028)
Benedek Kaibas via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 16 09:01:59 PDT 2026
================
@@ -57,6 +57,24 @@ The analysis flags the assignment `v = s` as defective because `s` is
destroyed while `v` is still alive and points to `s`, and adds a note
to where `v` is used after `s` has been destroyed.
+```c
+#include <stdio.h>
+void simple_dangle() {
+ int *ptr = NULL;
+ {
+ int i = 5;
+ ptr = &i; // warning: local variable 'i' does not live long enough
+ } // note: local variable 'i' is destroyed here
+ *ptr = 6; // note: later used here
+}
+```
+
+This example demonstrates a simples use-after-scope bug in C. The `ptr` pointer
+is set to `NULL` in the outer scope. In the inner scope ptr points to `i`, but
+its lifetime ends at the end of the inner block which causes `ptr` to dangle
+when it is set to 6.
+
+
----------------
benedekaibas wrote:
I think for consistency the example and the description for C should be kept. I agree that the example is self explanatory, but it would be inconsistent in the documentation if C++ has its own code example with a description and C does not.
https://github.com/llvm/llvm-project/pull/224028
More information about the cfe-commits
mailing list