[llvm] 9090430 - Add clang::lifetimebound annotation to StringRef constructors. (#113878)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 28 06:28:12 PDT 2024


Author: Haojian Wu
Date: 2024-10-28T14:28:07+01:00
New Revision: 9090430d4176fa260b8da46b7b983b3760d452be

URL: https://github.com/llvm/llvm-project/commit/9090430d4176fa260b8da46b7b983b3760d452be
DIFF: https://github.com/llvm/llvm-project/commit/9090430d4176fa260b8da46b7b983b3760d452be.diff

LOG: Add clang::lifetimebound annotation to StringRef constructors. (#113878)

Adding the lifetimebound annotation to the ArrayRef's array constructor
can enable us to detect the following use-after-free issues:

```
llvm::StringRef TestZoneName() {
   char test[] = "foo"; // oops, missing static
   return test; // use-after-free.
}
```

See #113533

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringRef.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index f879bbf7164fd6..0dcd4d90086eff 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -81,7 +81,7 @@ namespace llvm {
     StringRef(std::nullptr_t) = delete;
 
     /// Construct a string ref from a cstring.
-    /*implicit*/ constexpr StringRef(const char *Str)
+    /*implicit*/ constexpr StringRef(const char *Str LLVM_LIFETIME_BOUND)
         : View(Str, Str ?
     // GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen.
 #if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
@@ -93,7 +93,8 @@ namespace llvm {
     }
 
     /// Construct a string ref from a pointer and length.
-    /*implicit*/ constexpr StringRef(const char *data, size_t length)
+    /*implicit*/ constexpr StringRef(const char *data LLVM_LIFETIME_BOUND,
+                                     size_t length)
         : View(data, length) {}
 
     /// Construct a string ref from an std::string.


        


More information about the llvm-commits mailing list