llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Add details for -Wlifetime-safety-invalidation diags

Open usx95 opened this issue 6 months ago • 5 comments

https://godbolt.org/z/jTb1zEcfP

#include <vector>
void f(std::vector<int> a, std::vector<int> b, bool flag) {
    int* pa = &a[0];
    int* pb = &b[0];
    std::vector<int>* pv = (flag ? &a : &b);
    pv->push_back(42);
    (void)pa;
    (void)pb;
}

Current:

<source>:3:16: warning: object whose reference is captured is later invalidated [-Wlifetime-safety-invalidation]
    3 |     int* pa = &a[0];
      |                ^
<source>:6:9: note: invalidated here
    6 |     pv->push_back(42);
      |     ~~~~^~~~~~~~~~~~~
<source>:7:11: note: later used here
    7 |     (void)pa;
      |           ^~
<source>:4:16: warning: object whose reference is captured is later invalidated [-Wlifetime-safety-invalidation]
    4 |     int* pb = &b[0];
      |                ^
<source>:6:9: note: invalidated here
    6 |     pv->push_back(42);
      |     ~~~~^~~~~~~~~~~~~
<source>:8:11: note: later used here
    8 |     (void)pb;
      |           ^~

Expected:

  • local variable 'a' is later invalidated
  • local variable 'b' is later invalidated

usx95 avatar May 28 '26 18:05 usx95

@llvm/issue-subscribers-clang-temporal-safety

Author: Utkarsh Saxena (usx95)

https://godbolt.org/z/jTb1zEcfP
#include &lt;vector&gt;
void f(std::vector&lt;int&gt; a, std::vector&lt;int&gt; b, bool flag) {
    int* pa = &amp;a[0];
    int* pb = &amp;b[0];
    std::vector&lt;int&gt;* pv = (flag ? &amp;a : &amp;b);
    pv-&gt;push_back(42);
    (void)pa;
    (void)pb;
}

Current:

&lt;source&gt;:3:16: warning: object whose reference is captured is later invalidated [-Wlifetime-safety-invalidation]
    3 |     int* pa = &amp;a[0];
      |                ^
&lt;source&gt;:6:9: note: invalidated here
    6 |     pv-&gt;push_back(42);
      |     ~~~~^~~~~~~~~~~~~
&lt;source&gt;:7:11: note: later used here
    7 |     (void)pa;
      |           ^~
&lt;source&gt;:4:16: warning: object whose reference is captured is later invalidated [-Wlifetime-safety-invalidation]
    4 |     int* pb = &amp;b[0];
      |                ^
&lt;source&gt;:6:9: note: invalidated here
    6 |     pv-&gt;push_back(42);
      |     ~~~~^~~~~~~~~~~~~
&lt;source&gt;:8:11: note: later used here
    8 |     (void)pb;
      |           ^~

Expected:

  • local variable 'a' is later invalidated
  • local variable 'b' is later invalidated

Similarly for params, field and globals. https://godbolt.org/z/dcc3E6Wzn

#include <vector>
void f(std::vector<int> &a) {
    int* pa = &a[0];
    a.push_back(42);
    (void)pa;
}

<source>:2:8: warning: parameter is later invalidated [-Wlifetime-safety-invalidation]
    2 | void f(std::vector<int> &a) {
      |        ^~~~~~~~~~~~~~~~~~~
<source>:4:7: note: invalidated here
    4 |     a.push_back(42);
      |     ~~^~~~~~~~~~~~~
<source>:5:11: note: later used here
    5 |     (void)pa;
      |           ^~

expected:

  • parameter 'a' is later invalidated.

usx95 avatar May 28 '26 18:05 usx95

Hey @usx95 , do you think this is already tackled in PR #204900 ?

iitianpushkar avatar Jun 22 '26 12:06 iitianpushkar

Yes, I think we close this issue with your PR. We already added object description to the warnings, and now you've added it to notes too.

NeKon69 avatar Jun 22 '26 17:06 NeKon69

While looking at the lifetime safety project board on GitHub to see where I can start learning more about LSA I have found this issue and based on the comments and the PR listed solving this issue I think we can close this issue as completed. @NeKon69 @usx95

benedekaibas avatar Nov 29 '26 16:11 benedekaibas