Optimize reading string from packet.

Avoids possibility or re-allocation of the internal buffer of the string when its long.
This commit is contained in:
ratkosrb
2023-03-07 00:26:43 +02:00
parent df93de46ae
commit c1abfdc74e
+14 -6
View File
@@ -259,14 +259,22 @@ class ByteBuffer
ByteBuffer& operator>>(std::string& value)
{
value.clear();
while (rpos() < size()) // prevent crash at wrong string format in packet
// prevent crash at wrong string format in packet
if (_rpos < size())
{
char c = read<char>();
if (c == 0)
break;
value += c;
size_t startPos = _rpos;
while (_storage[_rpos] != '\0')
{
_rpos++;
if (_rpos + sizeof(char) > size())
throw ByteBufferException(false, _rpos, sizeof(char), size());
}
value.assign((char*)(&_storage[startPos]), _rpos - startPos);
_rpos++;
}
return *this;
}