Commit 5d84ff29413ce4a65bbf7bdc9f70a09f0589dbf3

Authored by Wiebe Cazemier
1 parent 862f0933

Simple protection against growing write buffer

Showing 1 changed file with 5 additions and 1 deletions
client.cpp
... ... @@ -84,8 +84,12 @@ void Client::writeMqttPacket(const MqttPacket &packet)
84 84 {
85 85 std::lock_guard<std::mutex> locker(writeBufMutex);
86 86  
  87 + // We have to allow big packets, yet don't allow a slow loris subscriber to grow huge write buffers. This
  88 + // could be enhanced a lot, but it's a start.
  89 + const uint32_t growBufMaxTo = std::min<int>(packet.getSizeIncludingNonPresentHeader() * 1000, MAX_PACKET_SIZE);
  90 +
87 91 // Grow as far as we can. We have to make room for one MQTT packet.
88   - while (packet.getSizeIncludingNonPresentHeader() > writebuf.freeSpace() && writebuf.getSize() < MAX_PACKET_SIZE)
  92 + while (packet.getSizeIncludingNonPresentHeader() > writebuf.freeSpace() && writebuf.getSize() < growBufMaxTo)
89 93 {
90 94 writebuf.doubleSize();
91 95 }
... ...