Game Development Community

[BUG] StringBuffer does not clear empty strings correctly

by James Urquhart · in iTorque 2D · 02/23/2012 (5:09 am) · 0 replies

Looking at StringBuffer::set(const UTF8 *in):

void StringBuffer::set(const UTF8 *in)
{
incRequestCount8();
// Convert and store. Note that a UTF16 version of the string cannot be longer.
FrameTemp<UTF16> tmpBuff(dStrlen(in)+1);
if(!in || in[0] == 0 || !convertUTF8toUTF16(in, tmpBuff, dStrlen(in)+1))
{
// Easy out, it's a blank string, or a bad string.
mBuffer.clear();
mBuffer.push_back(0);
AssertFatal(mBuffer.last() == 0, "StringBuffer::set UTF8 - not a null terminated string!");
return;
}

// Otherwise, we've a copy to do. (This might not be strictly necessary.)
mBuffer.setSize(dStrlen(tmpBuff)+1);
dMemcpy(mBuffer.address(), tmpBuff, sizeof(UTF16) * mBuffer.size());
mBuffer.compact();
AssertFatal(mBuffer.last() == 0, "StringBuffer::set UTF8 - not a null terminated string!");
mDirty8 = true;
}

The problem

If a string is empty, we quit the function early. However we don't set "mDirty8 = true;", so the UTF8 buffer is never cleared. This can result in the following strange turn of events:


StringBuffer buffer;
const char *string;
buffer.set("Kork");
string = buffer.getPtr8(); // returns "Kork"
buffer.set("");
string = buffer.getPtr8(); // returns "Kork"

The solution is of course to add "mDirty8 = true;" before the early return. StringBuffer::setNoConvert also suffers from this problem.