From a2a60f253c090b6cbc3547d9c05e4ee2199da333 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 9 Jul 2015 14:19:47 -0700 Subject: [PATCH] Fail with packet size in constructor --- libraries/networking/src/NLPacket.cpp | 10 ---------- libraries/networking/src/Packet.cpp | 18 +++++++----------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/libraries/networking/src/NLPacket.cpp b/libraries/networking/src/NLPacket.cpp index 1b1ac50eda..c3b09439ba 100644 --- a/libraries/networking/src/NLPacket.cpp +++ b/libraries/networking/src/NLPacket.cpp @@ -30,16 +30,6 @@ qint64 NLPacket::localHeaderSize() const { } std::unique_ptr NLPacket::create(PacketType::Value type, qint64 size) { - auto maxPayload = maxPayloadSize(type); - if (size == -1) { - // default size of -1, means biggest packet possible - size = maxPayload; - } - - // Fail with invalid size - Q_ASSERT(size >= 0 || size < maxPayload); - - // allocate memory return std::unique_ptr(new NLPacket(type, size)); } diff --git a/libraries/networking/src/Packet.cpp b/libraries/networking/src/Packet.cpp index 876cd91962..a5ab9d1716 100644 --- a/libraries/networking/src/Packet.cpp +++ b/libraries/networking/src/Packet.cpp @@ -24,16 +24,6 @@ qint64 Packet::maxPayloadSize(PacketType::Value type) { } std::unique_ptr Packet::create(PacketType::Value type, qint64 size) { - auto maxPayload = maxPayloadSize(type); - if (size == -1) { - // default size of -1, means biggest packet possible - size = maxPayload; - } - - // Fail with invalid size - Q_ASSERT(size >= 0 || size < maxPayload); - - // allocate memory return std::unique_ptr(new Packet(type, size)); } @@ -55,8 +45,14 @@ Packet::Packet(PacketType::Value type, qint64 size) : _packet(new char(_packetSize)), _payloadStart(_packet.get() + localHeaderSize(_type)), _capacity(size) { + auto maxPayload = maxPayloadSize(type); + if (size == -1) { + // default size of -1, means biggest packet possible + size = maxPayload; + } + // Sanity check - Q_ASSERT(size <= maxPayloadSize(type)); + Q_ASSERT(size >= 0 || size < maxPayload); // copy packet type and version in header writePacketTypeAndVersion(type);