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] Diagnose dangling issues for the "Container<GSLPointer>" case. (llvm#107213)
This pull request enhances the GSL lifetime analysis to detect
situations where a dangling `Container<GSLPointer>` object is
constructed:
```cpp
std::vector<std::string_view> bad = {std::string()}; // dangling
```
The assignment case is not yet supported, but they will be addressed in
a follow-up.
Fixesllvm#100526 (excluding the `push_back` case).
Copy file name to clipboardExpand all lines: clang/docs/ReleaseNotes.rst
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -298,6 +298,8 @@ Improvements to Clang's diagnostics
298
298
299
299
- Clang now warns for u8 character literals used in C23 with ``-Wpre-c23-compat`` instead of ``-Wpre-c++17-compat``.
300
300
301
+
- Clang now diagnoses cases where a dangling ``GSLOwner<GSLPointer>`` object is constructed, e.g. ``std::vector<string_view> v = {std::string()};`` (#GH100526).
std::string_view svjkk1 = ReturnStringView(StrCat("bar", "x")); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}
554
577
}
555
578
} // namespace GH100549
579
+
580
+
namespaceGH100526 {
581
+
voidtest() {
582
+
std::vector<std::string_view> v1({std::string()}); // expected-warning {{object backing the pointer will be destroyed at the end}}
583
+
std::vector<std::string_view> v2({
584
+
std::string(), // expected-warning {{object backing the pointer will be destroyed at the end}}
585
+
std::string_view()
586
+
});
587
+
std::vector<std::string_view> v3({
588
+
std::string_view(),
589
+
std::string() // expected-warning {{object backing the pointer will be destroyed at the end}}
590
+
});
591
+
592
+
std::optional<std::string_view> o1 = std::string(); // expected-warning {{object backing the pointer}}
593
+
594
+
std::string s;
595
+
// This is a tricky use-after-free case, what it does:
596
+
// 1. make_optional creates a temporary "optional<string>"" object
597
+
// 2. the temporary object owns the underlying string which is copied from s.
598
+
// 3. the t3 object holds the view to the underlying string of the temporary object.
599
+
std::optional<std::string_view> o2 = std::make_optional(s); // expected-warning {{object backing the pointer}}
600
+
std::optional<std::string_view> o3 = std::optional<std::string>(s); // expected-warning {{object backing the pointer}}
0 commit comments