Skip to content

Commit d7ea768

Browse files
hokeinNoumanAmir657
authored andcommitted
Add clang::lifetimebound annotation to StringRef constructors. (llvm#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 llvm#113533
1 parent e5419a8 commit d7ea768

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

llvm/include/llvm/ADT/StringRef.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace llvm {
8181
StringRef(std::nullptr_t) = delete;
8282

8383
/// Construct a string ref from a cstring.
84-
/*implicit*/ constexpr StringRef(const char *Str)
84+
/*implicit*/ constexpr StringRef(const char *Str LLVM_LIFETIME_BOUND)
8585
: View(Str, Str ?
8686
// GCC 7 doesn't have constexpr char_traits. Fall back to __builtin_strlen.
8787
#if defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE < 8
@@ -93,7 +93,8 @@ namespace llvm {
9393
}
9494

9595
/// Construct a string ref from a pointer and length.
96-
/*implicit*/ constexpr StringRef(const char *data, size_t length)
96+
/*implicit*/ constexpr StringRef(const char *data LLVM_LIFETIME_BOUND,
97+
size_t length)
9798
: View(data, length) {}
9899

99100
/// Construct a string ref from an std::string.

0 commit comments

Comments
 (0)