[UB] Fix the two ways that we would try to memcpy from a null buffer in

the nested name specifier code.

First, skip the entire thing when the input is empty.

Next, handle the case where we started off with a null buffer and a zero
capacity to skip copying and freeing.

This was found with UBSan.

llvm-svn: 243946
This commit is contained in:
Chandler Carruth 2015-08-04 03:52:56 +00:00
parent d96e877788
commit b6708d8ebf
1 changed files with 9 additions and 7 deletions

View File

@ -435,17 +435,19 @@ TypeLoc NestedNameSpecifierLoc::getTypeLoc() const {
namespace {
void Append(char *Start, char *End, char *&Buffer, unsigned &BufferSize,
unsigned &BufferCapacity) {
if (Start == End)
return;
if (BufferSize + (End - Start) > BufferCapacity) {
// Reallocate the buffer.
unsigned NewCapacity
= std::max((unsigned)(BufferCapacity? BufferCapacity * 2
: sizeof(void*) * 2),
(unsigned)(BufferSize + (End - Start)));
unsigned NewCapacity = std::max(
(unsigned)(BufferCapacity ? BufferCapacity * 2 : sizeof(void *) * 2),
(unsigned)(BufferSize + (End - Start)));
char *NewBuffer = static_cast<char *>(malloc(NewCapacity));
memcpy(NewBuffer, Buffer, BufferSize);
if (BufferCapacity)
if (BufferCapacity) {
memcpy(NewBuffer, Buffer, BufferSize);
free(Buffer);
}
Buffer = NewBuffer;
BufferCapacity = NewCapacity;
}