diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp index 2319b8adb1..d2fe0b4cde 100644 --- a/src/network/core/packet.cpp +++ b/src/network/core/packet.cpp @@ -223,7 +223,7 @@ bool Packet::CanReadFromPacket(size_t bytes_to_read, bool close_connection) */ bool Packet::HasPacketSizeData() const { - return this->pos >= sizeof(PacketSize); + return this->pos >= EncodedLengthOfPacketSize(); } /** @@ -250,10 +250,10 @@ bool Packet::ParsePacketSize() /* If the size of the packet is less than the bytes required for the size and type of * the packet, or more than the allowed limit, then something is wrong with the packet. * In those cases the packet can generally be regarded as containing garbage data. */ - if (size < sizeof(PacketSize) + sizeof(PacketType) || size > this->limit) return false; + if (size < EncodedLengthOfPacketSize() + EncodedLengthOfPacketType() || size > this->limit) return false; this->buffer.resize(size); - this->pos = sizeof(PacketSize); + this->pos = static_cast(EncodedLengthOfPacketSize()); return true; } @@ -263,7 +263,7 @@ bool Packet::ParsePacketSize() void Packet::PrepareToRead() { /* Put the position on the right place */ - this->pos = sizeof(PacketSize); + this->pos = static_cast(EncodedLengthOfPacketSize()); } /** @@ -272,8 +272,8 @@ void Packet::PrepareToRead() */ PacketType Packet::GetPacketType() const { - assert(this->Size() >= sizeof(PacketSize) + sizeof(PacketType)); - return static_cast(buffer[sizeof(PacketSize)]); + assert(this->Size() >= EncodedLengthOfPacketSize() + EncodedLengthOfPacketType()); + return static_cast(buffer[EncodedLengthOfPacketSize()]); } /** diff --git a/src/network/core/packet.h b/src/network/core/packet.h index beff95431d..2631173cce 100644 --- a/src/network/core/packet.h +++ b/src/network/core/packet.h @@ -40,6 +40,8 @@ typedef uint8_t PacketType; ///< Identifier for the packet * (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0)) */ struct Packet { + static constexpr size_t EncodedLengthOfPacketSize() { return sizeof(PacketSize); } + static constexpr size_t EncodedLengthOfPacketType() { return sizeof(PacketType); } private: /** The current read/write position in the packet */ PacketSize pos; @@ -52,7 +54,7 @@ private: NetworkSocketHandler *cs; public: - Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size = sizeof(PacketSize)); + Packet(NetworkSocketHandler *cs, size_t limit, size_t initial_read_size = EncodedLengthOfPacketSize()); Packet(NetworkSocketHandler *cs, PacketType type, size_t limit = COMPAT_MTU); /* Sending/writing of packets */