diff options
Diffstat (limited to 'src/server/shared/Packets/ByteBuffer.cpp')
-rw-r--r-- | src/server/shared/Packets/ByteBuffer.cpp | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/server/shared/Packets/ByteBuffer.cpp b/src/server/shared/Packets/ByteBuffer.cpp index bd323c4bc2e..2553e71f857 100644 --- a/src/server/shared/Packets/ByteBuffer.cpp +++ b/src/server/shared/Packets/ByteBuffer.cpp @@ -21,6 +21,7 @@ #include "Common.h" #include "Log.h" #include "Util.h" +#include <utf8.h> #include <sstream> #include <ctime> @@ -52,11 +53,16 @@ ByteBufferSourceException::ByteBufferSourceException(size_t pos, size_t size, message().assign(ss.str()); } +ByteBufferInvalidValueException::ByteBufferInvalidValueException(char const* type, size_t pos) +{ + message().assign(Trinity::StringFormat("Invalid %s value found in ByteBuffer at pos " SZFMTD)); +} + ByteBuffer& ByteBuffer::operator>>(float& value) { value = read<float>(); if (!std::isfinite(value)) - throw ByteBufferException(); + throw ByteBufferInvalidValueException("float", _rpos - sizeof(float)); return *this; } @@ -64,10 +70,25 @@ ByteBuffer& ByteBuffer::operator>>(double& value) { value = read<double>(); if (!std::isfinite(value)) - throw ByteBufferException(); + throw ByteBufferInvalidValueException("double", _rpos - sizeof(double)); return *this; } +std::string ByteBuffer::ReadCString(bool requireValidUtf8 /*= true*/) +{ + std::string value; + while (rpos() < size()) // prevent crash at wrong string format in packet + { + char c = read<char>(); + if (c == 0) + break; + value += c; + } + if (requireValidUtf8 && !utf8::is_valid(value.begin(), value.end())) + throw ByteBufferInvalidValueException("string", _rpos - value.length() - 1); + return value; +} + uint32 ByteBuffer::ReadPackedTime() { uint32 packedDate = read<uint32>(); |