Commit 86ae3f7633b5f209f6a49be270f367708d6da353

Authored by Wiebe Cazemier
1 parent c4df7557

Require presence of topics in UNSUBSCRIBE

Showing 1 changed file with 10 additions and 0 deletions
mqttpacket.cpp
@@ -494,8 +494,12 @@ void MqttPacket::handleUnsubscribe() @@ -494,8 +494,12 @@ void MqttPacket::handleUnsubscribe()
494 throw ProtocolError("Packet ID 0 when unsubscribing is invalid."); // [MQTT-2.3.1-1] 494 throw ProtocolError("Packet ID 0 when unsubscribing is invalid."); // [MQTT-2.3.1-1]
495 } 495 }
496 496
  497 + int numberOfUnsubs = 0;
  498 +
497 while (remainingAfterPos() > 0) 499 while (remainingAfterPos() > 0)
498 { 500 {
  501 + numberOfUnsubs++;
  502 +
499 uint16_t topicLength = readTwoBytesToUInt16(); 503 uint16_t topicLength = readTwoBytesToUInt16();
500 std::string topic(readBytes(topicLength), topicLength); 504 std::string topic(readBytes(topicLength), topicLength);
501 505
@@ -506,6 +510,12 @@ void MqttPacket::handleUnsubscribe() @@ -506,6 +510,12 @@ void MqttPacket::handleUnsubscribe()
506 logger->logf(LOG_UNSUBSCRIBE, "Client '%s' unsubscribed from '%s'", sender->repr().c_str(), topic.c_str()); 510 logger->logf(LOG_UNSUBSCRIBE, "Client '%s' unsubscribed from '%s'", sender->repr().c_str(), topic.c_str());
507 } 511 }
508 512
  513 + // MQTT-3.10.3-2
  514 + if (numberOfUnsubs == 0)
  515 + {
  516 + throw ProtocolError("No topics specified to unsubscribe to.");
  517 + }
  518 +
509 UnsubAck unsubAck(packet_id); 519 UnsubAck unsubAck(packet_id);
510 MqttPacket response(unsubAck); 520 MqttPacket response(unsubAck);
511 sender->writeMqttPacket(response); 521 sender->writeMqttPacket(response);