-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Extend -Wdangling to handle assignments, not just initializations #63310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
CC @Xazax-hun |
+1, 100% agreed. In fact, MSVC's |
Do you think you could give pointers to what changes would be needed to support this? |
Sure! The current implementation is in Unfortunately, the code is somewhat convoluted in the name of performance. The check is done while the AST is being built, so lifetime extension, handling of I am not convinced that the potential performance benefits would worth the cost of complexity here, it might be better to factor the As for where to put the check for the assignments, one could take a look at |
Duplicate of #54492? |
#46984 also seems to be a duplicate of this. |
… built-in pointer type (#96475) The lifetime bound warning in Clang currently only considers initializations. This patch extends the warning to include assignments. - **Support for assignments of built-in pointer types**: this is done is by reusing the existing statement-local implementation. Clang now warns if the pointer is assigned to a temporary object that being destoryed at the end of the full assignment expression. With this patch, we will detect more cases under the on-by-default diagnostic `-Wdangling`. I have added a new category for this specific diagnostic so that people can temporarily disable it if their codebase is not yet clean. This is the first step to address #63310, focusing only on pointer types. Support for C++ assignment operators will come in a follow-up patch. Fixes #54492
…signments between pointer-like objects. This patch extend the lifetimebound analysis to cover assignments between pointer-like objects (`gsl::Pointer`). Specifically, it tracks the RHS of assignment operators to determine if it points to a temporary object whose lifetime ends at the end of the full expression. If such a situation is detected, a diagnostic warning is emitted. Note that this detection currently applies only to the assignment expression **within** the initializer: ``` /tmp/t.cpp:3:30: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl] 3 | std::string_view b = abc = std::string(); ^~~~~~~~~~~~~ ``` Part of llvm#63310
… built-in pointer type (llvm#96475) The lifetime bound warning in Clang currently only considers initializations. This patch extends the warning to include assignments. - **Support for assignments of built-in pointer types**: this is done is by reusing the existing statement-local implementation. Clang now warns if the pointer is assigned to a temporary object that being destoryed at the end of the full assignment expression. With this patch, we will detect more cases under the on-by-default diagnostic `-Wdangling`. I have added a new category for this specific diagnostic so that people can temporarily disable it if their codebase is not yet clean. This is the first step to address llvm#63310, focusing only on pointer types. Support for C++ assignment operators will come in a follow-up patch. Fixes llvm#54492
Dumping my thoughts on this. @Xazax-hun @usx95 @ilya-biryukov @kinu The main case we aim to detect is when a pointer object is assigned to an owner object whose lifetime will end at the end of the full expression (still a statement-local analysis). The compiler needs to know whether a class behaves like a pointer or an owner, as the dangling warning only makes sense for pointer-like objects. The existing In C++, the assignment operator is a member function, and it can be defined explicitly by users or implicitly by the compiler. Based on the type of
Supporting 1) is sufficient to detect the common std::string foo() { return ""; }
int main() {
std::string_view v;
// On the RHS of the assignment, we have:
// - a temporary object `std::string` (the result of function `foo()` call)
// - a temporary object `std::string_view` constructing from the above temporary `std::string`
v = foo(); // invokes v.operator=(const std::string_view&)
} Case 2) is probably less common but would be nice to support. On the other hand, we have the Another tiny bit is how to handle compound assignment operators, such as |
Yeah, There are a few things we could do to try to generalize:
|
… built-in pointer type (llvm#96475) The lifetime bound warning in Clang currently only considers initializations. This patch extends the warning to include assignments. - **Support for assignments of built-in pointer types**: this is done is by reusing the existing statement-local implementation. Clang now warns if the pointer is assigned to a temporary object that being destoryed at the end of the full assignment expression. With this patch, we will detect more cases under the on-by-default diagnostic `-Wdangling`. I have added a new category for this specific diagnostic so that people can temporarily disable it if their codebase is not yet clean. This is the first step to address llvm#63310, focusing only on pointer types. Support for C++ assignment operators will come in a follow-up patch. Fixes llvm#54492
…ike 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
Currently,
-Wdangling
/-Wdangling-gsl
detect the following:However, they fail to detect this case:
It seems the compiler currently only handles initializations, not regular assignments, despite them posing the same issue. So it would be great to handle the second case as well.
More generally, if a
lifetimebound
object is assigned to a non-temporary expression, then I believe it would be correct to issue the diagnostic.The text was updated successfully, but these errors were encountered: