From 7c82007b4e5d5c66612f349f8a810c420ef4de75 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sun, 7 Feb 2021 18:14:14 +0100 Subject: [PATCH] Improved packet allocation --- src/protocol/Packet.cpp | 6 ++++-- src/protocol/Packet.h | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/protocol/Packet.cpp b/src/protocol/Packet.cpp index 8647f19..dfcaa97 100644 --- a/src/protocol/Packet.cpp +++ b/src/protocol/Packet.cpp @@ -160,7 +160,8 @@ namespace ts { protocol::OutgoingServerPacket* protocol::allocate_outgoing_server_packet(size_t payload_size) { auto base_size = sizeof(protocol::OutgoingServerPacket) - 1; - auto full_size = base_size + payload_size; + /* Allocate at least one payload byte since we're our payload array of length 1 */ + auto full_size = base_size + std::max(payload_size, (size_t) 1); auto result = (protocol::OutgoingServerPacket*) malloc(full_size); construct_osp(result); @@ -178,7 +179,8 @@ namespace ts { protocol::OutgoingClientPacket* protocol::allocate_outgoing_client_packet(size_t payload_size) { auto base_size = sizeof(protocol::OutgoingClientPacket) - 1; - auto full_size = base_size + payload_size; + /* Allocate at least one payload byte since we're our payload array of length 1 */ + auto full_size = base_size + std::max(payload_size, (size_t) 1); auto result = (protocol::OutgoingClientPacket*) malloc(full_size); construct_ocp(result); diff --git a/src/protocol/Packet.h b/src/protocol/Packet.h index e113c59..fd517c0 100644 --- a/src/protocol/Packet.h +++ b/src/protocol/Packet.h @@ -244,7 +244,7 @@ namespace ts::protocol { struct OutgoingClientPacket : public OutgoingPacket { public: OutgoingClientPacket() = default; - virtual ~OutgoingClientPacket() = default; + ~OutgoingClientPacket() override = default; OutgoingClientPacket* next; /* used within the write/process queue */