Commit b8885205 authored by Marco Bubke's avatar Marco Bubke

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's avatarTobias Hunger <tobias.hunger@qt.io>
parent 91bce7ab
...@@ -102,12 +102,7 @@ public: ...@@ -102,12 +102,7 @@ public:
} else { } else {
m_data.allocated.data.pointer = Memory::allocate(capacity + 1); m_data.allocated.data.pointer = Memory::allocate(capacity + 1);
std::memcpy(m_data.allocated.data.pointer, string, size); std::memcpy(m_data.allocated.data.pointer, string, size);
m_data.allocated.data.pointer[size] = 0; initializeLongString(size, capacity);
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;
} }
} }
...@@ -245,8 +240,12 @@ public: ...@@ -245,8 +240,12 @@ public:
m_data.allocated.data.capacity = newCapacity; m_data.allocated.data.capacity = newCapacity;
} else { } else {
const size_type oldSize = size(); 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: ...@@ -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) char &at(size_type index)
{ {
return *(data() + index); return *(data() + index);
......
...@@ -546,6 +546,15 @@ TEST(SmallString, AppendShortSmallString) ...@@ -546,6 +546,15 @@ TEST(SmallString, AppendShortSmallString)
ASSERT_THAT(text, SmallString("some text")); ASSERT_THAT(text, SmallString("some text"));
} }
TEST(SmallString, AppendLongSmallStringToShortSmallString)
{
SmallString text("some ");
text.append(SmallString("very very very very very long string"));
ASSERT_THAT(text, SmallString("some very very very very very long string"));
}
TEST(SmallString, AppendLongSmallString) TEST(SmallString, AppendLongSmallString)
{ {
SmallString longText("some very very very very very very very very very very very long string"); SmallString longText("some very very very very very very very very very very very long string");
...@@ -833,6 +842,24 @@ TEST(SmallString, ReserveMuchBiggerThanReference) ...@@ -833,6 +842,24 @@ TEST(SmallString, ReserveMuchBiggerThanReference)
ASSERT_THAT(text.capacity(), 100); ASSERT_THAT(text.capacity(), 100);
} }
TEST(SmallString, TextIsCopiedAfterReserveFromShortToLongString)
{
SmallString text("text");
text.reserve(100);
ASSERT_THAT(text, "text");
}
TEST(SmallString, TextIsCopiedAfterReserveReferenceToLongString)
{
SmallString text("some very very very very very very very very very very very long string");
text.reserve(100);
ASSERT_THAT(text, "some very very very very very very very very very very very long string");
}
TEST(SmallString, ReserveSmallerThanShortSmallString) TEST(SmallString, ReserveSmallerThanShortSmallString)
{ {
SmallString text = SmallString::fromUtf8("text"); SmallString text = SmallString::fromUtf8("text");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment