From 73e60d029c0ff0406d4e185f48183c30b83daa49 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Fri, 3 Dec 2010 05:42:25 +0000 Subject: Support/ADT/Twine: Make toNullTerminatedStringRef not rely on UB :(. llvm-svn: 120791 --- llvm/lib/Support/Twine.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'llvm/lib/Support/Twine.cpp') 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 &Out) const { } StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl &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(LHS)); + case StdStringKind: { + const std::string *str = static_cast(LHS); + return StringRef(str->c_str(), str->size()); + } + default: + break; + } } toVector(Out); Out.push_back(0); -- cgit v1.2.3