Support/ADT/Twine: Make toNullTerminatedStringRef not rely on UB :(.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120791 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Support/Twine.cpp b/lib/Support/Twine.cpp
index 4f6f479..75cea29 100644
--- a/lib/Support/Twine.cpp
+++ b/lib/Support/Twine.cpp
@@ -31,10 +31,18 @@
}
StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
- if (isSingleStringRef()) {
- StringRef sr = getSingleStringRef();
- if (*(sr.begin() + sr.size()) == 0)
- return sr;
+ if (isUnary()) {
+ switch (getLHSKind()) {
+ case CStringKind:
+ // Already null terminated, yay!
+ return StringRef(static_cast<const char*>(LHS));
+ case StdStringKind: {
+ const std::string *str = static_cast<const std::string*>(LHS);
+ return StringRef(str->c_str(), str->size());
+ }
+ default:
+ break;
+ }
}
toVector(Out);
Out.push_back(0);