[libcxx-commits] [libcxx] Add [[clang::lifetimebound]] to numerous functions in libc++ include headers (PR #112751)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 1 10:58:16 PDT 2024


================
@@ -10,6 +10,8 @@
 #ifndef SUPPORT_TEST_MACROS_HPP
 #define SUPPORT_TEST_MACROS_HPP
 
+#pragma clang diagnostic warning "-Wdangling"
----------------
higher-performance wrote:

There's no UB.

`s5.size()` is OK, but `s5[0]` is not. But lifetimebound isn't granular enough to say "this method is OK on a dangling object but that method is not", so we have to make it all-or-nothing. In this case, we say that the entire view (i.e. span) is bad even if the size is OK to query, because it's almost always bad usage in practice.

Note that this isn't any different from raw pointers, because this is OK:
```
char* p = std::string(...).data();
```
and this is not:
```
char* p = std::string(...).data();
p[0];
```
again, lifetimebound doesn't provide a way to distinguish these, so we just say both of them are bad, because it's a code smell (and fragile code) to have a dangling pointer to potentially-deallocated memory regardless.

https://github.com/llvm/llvm-project/pull/112751


More information about the libcxx-commits mailing list