From 5d84ff29413ce4a65bbf7bdc9f70a09f0589dbf3 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Tue, 5 Jan 2021 21:37:46 +0100 Subject: [PATCH] Simple protection against growing write buffer --- client.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client.cpp b/client.cpp index 73baba5..30c11ab 100644 --- a/client.cpp +++ b/client.cpp @@ -84,8 +84,12 @@ void Client::writeMqttPacket(const MqttPacket &packet) { std::lock_guard locker(writeBufMutex); + // We have to allow big packets, yet don't allow a slow loris subscriber to grow huge write buffers. This + // could be enhanced a lot, but it's a start. + const uint32_t growBufMaxTo = std::min(packet.getSizeIncludingNonPresentHeader() * 1000, MAX_PACKET_SIZE); + // Grow as far as we can. We have to make room for one MQTT packet. - while (packet.getSizeIncludingNonPresentHeader() > writebuf.freeSpace() && writebuf.getSize() < MAX_PACKET_SIZE) + while (packet.getSizeIncludingNonPresentHeader() > writebuf.freeSpace() && writebuf.getSize() < growBufMaxTo) { writebuf.doubleSize(); } -- libgit2 0.21.4