forked from qt-creator/qt-creator
Utils: Reserve in smallstring was flaky
We have to copy the content of SmallString before we instantiate a new constructor in the same memory. So the content to which data() is pointing can be already invalid. Change-Id: I3a0ab4f9ac0c1219c2bd75fc4412eaf56209ca64 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -102,12 +102,7 @@ public:
|
||||
} else {
|
||||
m_data.allocated.data.pointer = Memory::allocate(capacity + 1);
|
||||
std::memcpy(m_data.allocated.data.pointer, string, size);
|
||||
m_data.allocated.data.pointer[size] = 0;
|
||||
m_data.allocated.data.size = size;
|
||||
m_data.allocated.data.capacity = capacity;
|
||||
m_data.allocated.shortStringSize = 0;
|
||||
m_data.allocated.isReference = true;
|
||||
m_data.allocated.isReadOnlyReference = false;
|
||||
initializeLongString(size, capacity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,8 +240,12 @@ public:
|
||||
m_data.allocated.data.capacity = newCapacity;
|
||||
} else {
|
||||
const size_type oldSize = size();
|
||||
const char *oldData = data();
|
||||
|
||||
new (this) BasicSmallString(data(), oldSize, newCapacity);
|
||||
char *newData = Memory::allocate(newCapacity + 1);
|
||||
std::memcpy(newData, oldData, oldSize);
|
||||
m_data.allocated.data.pointer = newData;
|
||||
initializeLongString(oldSize, newCapacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -660,6 +659,16 @@ private:
|
||||
{
|
||||
}
|
||||
|
||||
void initializeLongString(size_type size, size_type capacity)
|
||||
{
|
||||
m_data.allocated.data.pointer[size] = 0;
|
||||
m_data.allocated.data.size = size;
|
||||
m_data.allocated.data.capacity = capacity;
|
||||
m_data.allocated.shortStringSize = 0;
|
||||
m_data.allocated.isReference = true;
|
||||
m_data.allocated.isReadOnlyReference = false;
|
||||
}
|
||||
|
||||
char &at(size_type index)
|
||||
{
|
||||
return *(data() + index);
|
||||
|
||||
Reference in New Issue
Block a user