Core/Misc: Add more extensive ByteBuffer exceptions to trace down seemingly random crashes.

This commit is contained in:
Machiavelli
2012-02-28 14:51:50 +01:00
parent 3ea9fe4022
commit 218ed813d0
+51 -17
View File
@@ -27,22 +27,49 @@
class ByteBufferException class ByteBufferException
{ {
public: public:
ByteBufferException(bool _add, size_t _pos, size_t _esize, size_t _size) ByteBufferException(size_t pos, size_t size, size_t valueSize)
: add(_add), pos(_pos), esize(_esize), size(_size) : Pos(pos), Size(size), ValueSize(valueSize)
{ {
PrintPosError(); PrintError();
} }
void PrintPosError() const protected:
virtual void PrintError() const = 0;
size_t Pos;
size_t Size;
size_t ValueSize;
};
class ByteBufferPositionException : public ByteBufferException
{
public:
ByteBufferPositionException(bool add, size_t pos, size_t size, size_t valueSize)
: ByteBufferException(pos, size, valueSize), _add(add) {}
protected:
void PrintError() const override
{ {
sLog->outError("Attempted to %s in ByteBuffer (pos: " SIZEFMTD " size: "SIZEFMTD") value with size: " SIZEFMTD, sLog->outError("Attempted to %s value with size: "SIZEFMTD" in ByteBuffer (pos: " SIZEFMTD " size: "SIZEFMTD") " ,
(add ? "put" : "get"), pos, size, esize); ValueSize, (_add ? "put" : "get"), Pos, Size);
} }
private: private:
bool add; bool _add;
size_t pos; };
size_t esize;
size_t size; class ByteBufferSourceException : public ByteBufferException
{
public:
ByteBufferSourceException(size_t pos, size_t size, size_t valueSize)
: ByteBufferException(pos, size, valueSize) {}
protected:
void PrintError() const override
{
sLog->outError("Attempted to put a %s in ByteBuffer (pos: "SIZEFMTD" size: "SIZEFMTD")",
(ValueSize > 0 ? "NULL-pointer" : "zero-sized value"), Pos, Size);
}
}; };
class ByteBuffer class ByteBuffer
@@ -271,7 +298,7 @@ class ByteBuffer
void read_skip(size_t skip) void read_skip(size_t skip)
{ {
if (_rpos + skip > size()) if (_rpos + skip > size())
throw ByteBufferException(false, _rpos, skip, size()); throw ByteBufferPositionException(false, _rpos, skip, size());
_rpos += skip; _rpos += skip;
} }
@@ -285,7 +312,7 @@ class ByteBuffer
template <typename T> T read(size_t pos) const template <typename T> T read(size_t pos) const
{ {
if (pos + sizeof(T) > size()) if (pos + sizeof(T) > size())
throw ByteBufferException(false, pos, sizeof(T), size()); throw ByteBufferPositionException(false, pos, sizeof(T), size());
T val = *((T const*)&_storage[pos]); T val = *((T const*)&_storage[pos]);
EndianConvert(val); EndianConvert(val);
return val; return val;
@@ -294,7 +321,7 @@ class ByteBuffer
void read(uint8 *dest, size_t len) void read(uint8 *dest, size_t len)
{ {
if (_rpos + len > size()) if (_rpos + len > size())
throw ByteBufferException(false, _rpos, len, size()); throw ByteBufferPositionException(false, _rpos, len, size());
memcpy(dest, &_storage[_rpos], len); memcpy(dest, &_storage[_rpos], len);
_rpos += len; _rpos += len;
} }
@@ -302,7 +329,7 @@ class ByteBuffer
void readPackGUID(uint64& guid) void readPackGUID(uint64& guid)
{ {
if (rpos() + 1 > size()) if (rpos() + 1 > size())
throw ByteBufferException(false, _rpos, 1, size()); throw ByteBufferPositionException(false, _rpos, 1, size());
guid = 0; guid = 0;
@@ -314,7 +341,7 @@ class ByteBuffer
if (guidmark & (uint8(1) << i)) if (guidmark & (uint8(1) << i))
{ {
if (rpos() + 1 > size()) if (rpos() + 1 > size())
throw ByteBufferException(false, _rpos, 1, size()); throw ByteBufferPositionException(false, _rpos, 1, size());
uint8 bit; uint8 bit;
(*this) >> bit; (*this) >> bit;
@@ -359,7 +386,10 @@ class ByteBuffer
void append(const uint8 *src, size_t cnt) void append(const uint8 *src, size_t cnt)
{ {
if (!cnt) if (!cnt)
return; throw ByteBufferSourceException(_wpos, size(), cnt);
if (!src)
throw ByteBufferSourceException(_wpos, size(), cnt);
ASSERT(size() < 10000000); ASSERT(size() < 10000000);
@@ -407,7 +437,11 @@ class ByteBuffer
void put(size_t pos, const uint8 *src, size_t cnt) void put(size_t pos, const uint8 *src, size_t cnt)
{ {
if (pos + cnt > size()) if (pos + cnt > size())
throw ByteBufferException(true, pos, cnt, size()); throw ByteBufferPositionException(true, pos, cnt, size());
if (!src)
throw ByteBufferSourceException(_wpos, size(), cnt);
memcpy(&_storage[pos], src, cnt); memcpy(&_storage[pos], src, cnt);
} }