================
@@ -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
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits