diff options
author | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-12-03 05:42:25 +0000 |
---|---|---|
committer | Michael J. Spencer <bigcheesegs@gmail.com> | 2010-12-03 05:42:25 +0000 |
commit | 73e60d029c0ff0406d4e185f48183c30b83daa49 (patch) | |
tree | ddba5d81fb3db259f6344029cc9752da8f03933a /llvm/lib/Support/Twine.cpp | |
parent | 31e310cda0e1660e3bada29096e29796aabe059a (diff) | |
download | bcm5719-llvm-73e60d029c0ff0406d4e185f48183c30b83daa49.tar.gz bcm5719-llvm-73e60d029c0ff0406d4e185f48183c30b83daa49.zip |
Support/ADT/Twine: Make toNullTerminatedStringRef not rely on UB :(.
llvm-svn: 120791
Diffstat (limited to 'llvm/lib/Support/Twine.cpp')
-rw-r--r-- | llvm/lib/Support/Twine.cpp | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/llvm/lib/Support/Twine.cpp b/llvm/lib/Support/Twine.cpp index 4f6f479a7ea..75cea2961a9 100644 --- a/llvm/lib/Support/Twine.cpp +++ b/llvm/lib/Support/Twine.cpp @@ -31,10 +31,18 @@ StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const { } 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); |