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

[LifetimeSafety] False-negative for gsl::Pointer constructed from lifetimebound arg

Open usx95 opened this issue 10 months ago • 6 comments

https://godbolt.org/z/P4ca5foxM

struct Bar {
  ~Bar();
};

struct [[gsl::Pointer]] Pointer {
  Pointer(const Bar & bar [[clang::lifetimebound]]);
};

Pointer test3(Bar bar) {
  return bar; // Should be an error for returning stack addr.
}

usx95 avatar Jan 14 '26 07:01 usx95

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

Author: Utkarsh Saxena (usx95)

https://godbolt.org/z/P4ca5foxM ```cpp struct Bar { ~Bar(); };

struct [[gsl::Pointer]] Pointer { Pointer(const Bar & bar [[clang::lifetimebound]]); };

Pointer test3(Bar bar) { return bar; // Should be an error for returning stack addr. }

</details>

llvmbot avatar Jan 14 '26 07:01 llvmbot

One more example from the wild where this seems important:

#include <vector>

#include "absl/types/span.h"

void TemporarySpan() {
    // Error. Ok.
    absl::Span<const int> span = std::vector<int>{1, 2, 3};
    (void)span[0];

    // No error. Ok.
    span = {};
    (void)span[0];

    // FIXME: Detect this.
    span = absl::Span<const int>({1, 2, 3});
    (void)span[0];
}

void LocalSpan() {
    // Error. Ok.
    absl::Span<const int> span;
    {
        std::vector<int> v = {1, 2, 3};
        span = v;
    }
    (void)span[0];
}

struct SpanField {
    // Dangling field error. Ok.
    SpanField(std::vector<int> v) : span_(v) {}
    // This is apparently ok ? We have no dtor for the initialiser list
    SpanField() : span_({}) {}
    // FIXME. Detect this.
    SpanField(bool) : span_({1, 2, 3}) {}

   private:
    absl::Span<const int> span_;
};

https://godbolt.org/z/eoxfaj436 Both FIXME above are related to lifetimebound arg in a constructor of absl::Span.

Target constructor is:

  template <typename LazyT = T,
            typename = EnableIfValueIsConst<LazyT>>
  Span(std::initializer_list<value_type> v
           ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept  // NOLINT(runtime/explicit)
      : Span(v.begin(), v.size()) {}

usx95 avatar Mar 03 '26 09:03 usx95

cc: @aeft

usx95 avatar Mar 25 '26 15:03 usx95

Please assign this to me.

aeft avatar Apr 01 '26 06:04 aeft

@aeft Oops.. sorry, 2 days ago @usx95 reached out to me and sent a few issues I could work on (including this one), I started working on it yesterday, but completely forgot to leave a comment here. I'm really sorry

NeKon69 avatar Apr 01 '26 08:04 NeKon69

This is a good ramp up issue for lifetime safety. I would let Nekon work on it

usx95 avatar Apr 01 '26 08:04 usx95