aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/network/networkpacket.cpp67
-rw-r--r--src/network/networkpacket.h2
2 files changed, 31 insertions, 38 deletions
diff --git a/src/network/networkpacket.cpp b/src/network/networkpacket.cpp
index d7487af40..95605d410 100644
--- a/src/network/networkpacket.cpp
+++ b/src/network/networkpacket.cpp
@@ -39,6 +39,16 @@ NetworkPacket::~NetworkPacket()
m_data.clear();
}
+void NetworkPacket::checkReadOffset(u32 from_offset)
+{
+ if (from_offset >= m_datasize) {
+ std::stringstream ss;
+ ss << "Reading outside packet (offset: " <<
+ from_offset << ", packet size: " << getSize() << ")";
+ throw SerializationError(ss.str());
+ }
+}
+
void NetworkPacket::putRawPacket(u8 *data, u32 datasize, u16 peer_id)
{
// If a m_command is already set, we are rewriting on same packet
@@ -55,8 +65,7 @@ void NetworkPacket::putRawPacket(u8 *data, u32 datasize, u16 peer_id)
char* NetworkPacket::getString(u32 from_offset)
{
- if (from_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(from_offset);
return (char*)&m_data[from_offset];
}
@@ -187,8 +196,7 @@ std::string NetworkPacket::readLongString()
NetworkPacket& NetworkPacket::operator>>(char& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU8(&m_data[m_read_offset]);
@@ -198,8 +206,7 @@ NetworkPacket& NetworkPacket::operator>>(char& dst)
char NetworkPacket::getChar(u32 offset)
{
- if (offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(offset);
return readU8(&m_data[offset]);
}
@@ -276,8 +283,7 @@ NetworkPacket& NetworkPacket::operator<<(float src)
NetworkPacket& NetworkPacket::operator>>(bool& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU8(&m_data[m_read_offset]);
@@ -287,8 +293,7 @@ NetworkPacket& NetworkPacket::operator>>(bool& dst)
NetworkPacket& NetworkPacket::operator>>(u8& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU8(&m_data[m_read_offset]);
@@ -298,8 +303,7 @@ NetworkPacket& NetworkPacket::operator>>(u8& dst)
u8 NetworkPacket::getU8(u32 offset)
{
- if (offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(offset);
return readU8(&m_data[offset]);
}
@@ -310,16 +314,14 @@ u8* NetworkPacket::getU8Ptr(u32 from_offset)
return NULL;
}
- if (from_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(from_offset);
return (u8*)&m_data[from_offset];
}
NetworkPacket& NetworkPacket::operator>>(u16& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU16(&m_data[m_read_offset]);
@@ -329,16 +331,14 @@ NetworkPacket& NetworkPacket::operator>>(u16& dst)
u16 NetworkPacket::getU16(u32 from_offset)
{
- if (from_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(from_offset);
return readU16(&m_data[from_offset]);
}
NetworkPacket& NetworkPacket::operator>>(u32& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU32(&m_data[m_read_offset]);
@@ -348,8 +348,7 @@ NetworkPacket& NetworkPacket::operator>>(u32& dst)
NetworkPacket& NetworkPacket::operator>>(u64& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readU64(&m_data[m_read_offset]);
@@ -359,8 +358,7 @@ NetworkPacket& NetworkPacket::operator>>(u64& dst)
NetworkPacket& NetworkPacket::operator>>(float& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readF1000(&m_data[m_read_offset]);
@@ -370,8 +368,7 @@ NetworkPacket& NetworkPacket::operator>>(float& dst)
NetworkPacket& NetworkPacket::operator>>(v2f& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readV2F1000(&m_data[m_read_offset]);
@@ -381,8 +378,7 @@ NetworkPacket& NetworkPacket::operator>>(v2f& dst)
NetworkPacket& NetworkPacket::operator>>(v3f& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readV3F1000(&m_data[m_read_offset]);
@@ -392,8 +388,7 @@ NetworkPacket& NetworkPacket::operator>>(v3f& dst)
NetworkPacket& NetworkPacket::operator>>(s16& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readS16(&m_data[m_read_offset]);
@@ -409,8 +404,7 @@ NetworkPacket& NetworkPacket::operator<<(s16 src)
NetworkPacket& NetworkPacket::operator>>(s32& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readS32(&m_data[m_read_offset]);
@@ -426,8 +420,7 @@ NetworkPacket& NetworkPacket::operator<<(s32 src)
NetworkPacket& NetworkPacket::operator>>(v3s16& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readV3S16(&m_data[m_read_offset]);
@@ -445,8 +438,7 @@ NetworkPacket& NetworkPacket::operator>>(v2s32& dst)
NetworkPacket& NetworkPacket::operator>>(v3s32& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readV3S32(&m_data[m_read_offset]);
@@ -494,8 +486,7 @@ NetworkPacket& NetworkPacket::operator<<(v3s32 src)
NetworkPacket& NetworkPacket::operator>>(video::SColor& dst)
{
- if (m_read_offset >= m_datasize)
- throw SerializationError("Malformed packet read");
+ checkReadOffset(m_read_offset);
dst = readARGB8(&m_data[m_read_offset]);
diff --git a/src/network/networkpacket.h b/src/network/networkpacket.h
index 0afb1e7e3..9ad169888 100644
--- a/src/network/networkpacket.h
+++ b/src/network/networkpacket.h
@@ -109,6 +109,8 @@ public:
// Temp, we remove SharedBuffer when migration finished
Buffer<u8> oldForgePacket();
private:
+ void checkReadOffset(u32 from_offset);
+
template<typename T> void checkDataSize()
{
if (m_read_offset + sizeof(T) > m_datasize) {