Skip to content

Diagnose dangling assignments for std::initializer_list. #110103

Open
@hokein

Description

@hokein

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;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:memory-safetyIssue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions