Skip to content

Attribute clang::lifetimebound produces no warning when assign temporary's address to pointer declared before. #54492

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

Closed
Kidsunbo opened this issue Mar 22, 2022 · 0 comments · Fixed by #96475
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@Kidsunbo
Copy link

Kidsunbo commented Mar 22, 2022

[[clang::lifetimebound]] could be used to provide some lifetime annotation to functions. In the statement of a pointer initialization, everything works fine. But in the statement of a pointer assignment, the desired warning can not be produced.

Here is the code sample:

#include <string>

const char* get(const std::string& s [[clang::lifetimebound]]){
    return s.data();
}

int main(){
 // can be detected
 const char* s1 = get("hello"s);

 // cannot be detected
 const char* s2 = nullptr;
 s2 = get("hello"s);

 return 0;
}

Here is more code to illustrate this: https://godbolt.org/z/fhTPYTa1d

Is this intentional or a flaw of lifetime check?

@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed new issue labels Mar 22, 2022
hokein added a commit that referenced this issue Jul 1, 2024
… 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
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this issue Jul 3, 2024
… 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
kbluck pushed a commit to kbluck/llvm-project that referenced this issue Jul 6, 2024
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants