[llvm-commits] [llvm] r120600 - in /llvm/trunk: include/llvm/ADT/Twine.h lib/Support/Twine.cpp unittests/ADT/TwineTest.cpp

Michael Spencer bigcheesegs at gmail.com
Thu Dec 2 09:56:53 PST 2010


On Wed, Dec 1, 2010 at 3:52 PM, Frits van Bommel <fvbommel at gmail.com> wrote:
> On Wed, Dec 1, 2010 at 9:37 PM, Michael J. Spencer
> <bigcheesegs at gmail.com> wrote:
>> Log:
>> Support/ADT/Twine: Add toNullTerminatedStringRef.
>
>> +StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
>> +  if (isSingleStringRef()) {
>> +    StringRef sr = getSingleStringRef();
>> +    if (*(sr.begin() + sr.size()) == 0)
>
> This is undefined behavior if the StringRef is at the end of whatever
> memory allocation it's in.
> For instance, it might for instance have been constructed from a
> std::string, which doesn't need to null-terminate its data unless
> c_str() is called, or it might have been constructed with the pointer
> + length constructor.

This is true, however, the only other alternative is to _always_ do a
copy and add a null terminator. Even in the common case where one
already exists.

- Michael Spencer

>> +      return sr;
>> +  }
>> +  toVector(Out);
>> +  Out.push_back(0);
>> +  Out.pop_back();
>> +  return StringRef(Out.data(), Out.size());
>> +}
>




More information about the llvm-commits mailing list