<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/71411>71411</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang 15 is not triggering a dangling reference indicating stack use after return with -fsanitize=address and optimization
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mohamed-ai-selim
      </td>
    </tr>
</table>

<pre>
    In scenario A:
The following code doesn't trigger warning -Wreturn-stack-address as expected, indicating a dangling reference to stack after return. Yet upon running the sanitizer, it triggers the **runtime error stack-use-after-return**. So it might has something to do with dangling reference from outer types e.g. std::reference_wrapper

Scenario B:
Clang triggers the warning as expected, as well as the sanitizer

N.B. in gcc the warning is triggered as expected.
```

#include <iostream>
#include <functional>

// scenario A
const int& foo()
{
    int x = 234;
    std::reference_wrapper<int> s{x};
   
    return s.get();
}

// scenario B
/*
const int& foo()
{
    int s = 234;   
    return s;
}*/

// clang: clang++-15 -o test main.cpp -std=c++14 -Wdangling-field -Wdangling-initializer-list -Wdangling -Wdangling-gsl -Wreturn-stack-address -fsanitize=address
// gcc: g++-10 -o test main.cpp -std=c++14 -Wframe-address -Wreturn-local-addr -O3
int main()
{
    const auto& f_res = foo();
    std::cout << "result: " << f_res << "\n";
    
    return 0;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVU2P4zYM_TXKhbBhy87XwYd87AC9tIctsOhpoUi0rVaWDIluZvfXF7LjxOlmFkWBwViQyMdH8oUUIejGIlZsfWTr80oM1Dpfda4VHapE6CSg0d3q4tS36hcLQaIVXjs4sOLAsjPLDr-3CLUzxl21bUA6haAcBsv4loC8bhr0cBXexufki0cavE0CCflXIpTyGAKIAPjeoyRUjJ9AW6WloOggQAnbmHj0WKNHKxHIwegPoib0MEGm8AcSDL2z4Ac7RqMWIQirSX9H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