[LifetimeSafety] Support pointer arithmetic on gsl::Pointer types
Currently this does not warn:
#include <vector>
auto f() {
std::vector v{1};
auto val = v.begin(); // does not diagnose
val = val + 10;
return val;
}
Expected behavior would be to emit a warning similar to "address of stack memory is returned later".
https://godbolt.org/z/jGqMv6enE
@llvm/issue-subscribers-clang-temporal-safety
Author: NeKon69
#include <vector>
auto f() {
std::vector v{1};
auto val = v.begin(); // does not diagnose
val = val + 10;
return val;
}
Expected behavior would be to emit a warning similar to "address of stack memory is returned later".
https://godbolt.org/z/jGqMv6enE
This turned out to be a bit harder than I initially anticipated. I’m still working on it, though
I don't think I can fix that reliably without introducing a lot of false-positives, so I am probably going to leave it for someone more experienced.
I don't think I can fix that reliably without introducing a lot of false-positives, so I am probably going to leave it for someone more experienced.
Do you have examples where you saw false positives for future reference.
Note that we have to do this only for gsl::pointer types, not all types.
Do you have examples where you saw false positives, for future reference?
What I tried to do was to make some kind of a guess as to whether the produced result depends on this. Some of the checks were whether the return type is the same as this, whether the type was marked with gsl::Pointer, and whether it is not a static function. After that, I also tried adding an isInStlNamespace condition, but that would not really support all user-defined types, so I abandoned that idea.
For example, imagine this scenario:
struct [[gsl::Pointer]] View {
View operator+(int x) { return View{}; }
};
A user could easily write something like this. Even if it does not make much sense to do something like that, I do not think we can afford to create false-positives here
Hmmmm.. Now that I know a bit more about LifetimeSafety, I feel like I can try this issue one more time.
Is it OK if I only support iterators? I still don't think I can support all of the gsl::Pointer types because their semantics are ambiguous
iterators and pointers should be a good enough.