You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[clang] Extend lifetime analysis to support assignments for pointer-like objects. (#99032)
Summary:
This is a follow-up patch to #96475 to detect dangling assignments for
C++ pointer-like objects (classes annotated with the
`[[gsl::Pointer]]`). Fixes#63310.
Similar to the behavior for built-in pointer types, if a temporary owner
(`[[gsl::Owner]]`) object is assigned to a pointer-like class object,
and this temporary object is destroyed at the end of the full assignment
expression, the assignee pointer is considered dangling. In such cases,
clang will emit a warning:
```
/tmp/t.cpp:7:20: warning: object backing the pointer my_string_view will be destroyed at the end of the full-expression [-Wdangling-assignment-gsl]
7 | my_string_view = CreateString();
| ^~~~~~~~~~~~~~
1 warning generated.
```
This new warning is `-Wdangling-assignment-gsl`. It is initially
disabled, but I intend to enable it by default in clang 20.
I have initially tested this patch on our internal codebase, and it has
identified many use-after-free bugs, primarily related to `string_view`.
Test Plan:
Reviewers:
Subscribers:
Tasks:
Tags:
Differential Revision: https://phabricator.intern.facebook.com/D60251757
Copy file name to clipboardExpand all lines: clang/docs/ReleaseNotes.rst
+3
Original file line number
Diff line number
Diff line change
@@ -727,6 +727,9 @@ Improvements to Clang's diagnostics
727
727
728
728
- Clang now diagnoses integer constant expressions that are folded to a constant value as an extension in more circumstances. Fixes #GH59863
729
729
730
+
- Clang now diagnoses dangling assignments for pointer-like objects (annotated with `[[gsl::Pointer]]`) under `-Wdangling-assignment-gsl` (off by default)
MyIntPointer p = MyIntOwner{}; // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
123
-
p = MyIntOwner{}; // TODO ?
124
-
global = MyIntOwner{}; // TODO ?
123
+
MyIntPointer pp = p = MyIntOwner{}; // expected-warning {{object backing the pointer p will be}}
124
+
p = MyIntOwner{}; // expected-warning {{object backing the pointer p }}
125
+
pp = p; // no warning
126
+
global = MyIntOwner{}; // expected-warning {{object backing the pointer global }}
125
127
MyLongPointerFromConversion p2 = MyLongOwnerWithConversion{}; // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
126
-
p2 = MyLongOwnerWithConversion{}; //TODO ?
127
-
global2 = MyLongOwnerWithConversion{}; //TODO ?
128
+
p2 = MyLongOwnerWithConversion{}; //expected-warning {{object backing the pointer p2 }}
129
+
global2 = MyLongOwnerWithConversion{}; //expected-warning {{object backing the pointer global2 }}
std::basic_string_view<char> v = cond ? def : ""; // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
0 commit comments