Skip to content
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

Diagnose dangling assignments for std::initializer_list. #110103

Open
hokein opened this issue Sep 26, 2024 · 2 comments
Open

Diagnose dangling assignments for std::initializer_list. #110103

hokein opened this issue Sep 26, 2024 · 2 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer clang:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)

Comments

@hokein
Copy link
Collaborator

hokein commented Sep 26, 2024

See the example below, it would be nice for clang to diagnose this case.

https://godbolt.org/z/W3dhEzY6M

#include <vector>
using namespace std;

int main() 
{
  // Good: Initializing an initializer_list object from the array extends the
  // lifetime of the array exactly like binding a reference to a temporary.
  initializer_list<int> a = {1, 2, 3};

  // Dangerous! the lifetime of the backing array is not extended for assignments.
  // 
  // A holds dangling pointers into backing array which are destroyed at the end of full expression.
  a = {2, 3, 4};
  for (int i : a) {
    i;
  }
}
@hokein hokein added clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr) labels Sep 26, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 26, 2024

@llvm/issue-subscribers-clang-frontend

Author: Haojian Wu (hokein)

See the example below, it would be nice for clang to diagnose this case.

https://godbolt.org/z/W3dhEzY6M

#include &lt;vector&gt;
using namespace std;

int main() 
{
  // Good: Initializing an initializer_list object from the array extends the
  // lifetime of the array exactly like binding a reference to a temporary.
  initializer_list&lt;int&gt; a = {1, 2, 3};

  // Dangerous! the lifetime of the backing array is not extended for assignments.
  // 
  // A holds dangling pointers into backing array which are destroyed at the end of full expression.
  a = {2, 3, 4};
  for (int i : a) {
    i;
  }
}

@shafik shafik added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 26, 2024
@hokein
Copy link
Collaborator Author

hokein commented Nov 21, 2024

gcc implements this warning -Winit-list-lifetime: https://godbolt.org/z/M4bxvjG46

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 clang:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)
Projects
None yet
Development

No branches or pull requests

3 participants