Commit aa87dfb69e1b839f75132330b53cbc3600c491f7

Authored by Wiebe Cazemier
1 parent 5856733c

Comply with protocol spec to check for non-0 packet ID

[MQTT-2.3.1-1]
Showing 1 changed file with 15 additions and 0 deletions
mqttpacket.cpp
@@ -425,6 +425,11 @@ void MqttPacket::handleSubscribe() @@ -425,6 +425,11 @@ void MqttPacket::handleSubscribe()
425 425
426 uint16_t packet_id = readTwoBytesToUInt16(); 426 uint16_t packet_id = readTwoBytesToUInt16();
427 427
  428 + if (packet_id == 0)
  429 + {
  430 + throw ProtocolError("Packet ID 0 when subscribing is invalid."); // [MQTT-2.3.1-1]
  431 + }
  432 +
428 Authentication &authentication = *ThreadAuth::getAuth(); 433 Authentication &authentication = *ThreadAuth::getAuth();
429 434
430 std::list<char> subs_reponse_codes; 435 std::list<char> subs_reponse_codes;
@@ -478,6 +483,11 @@ void MqttPacket::handleUnsubscribe() @@ -478,6 +483,11 @@ void MqttPacket::handleUnsubscribe()
478 483
479 uint16_t packet_id = readTwoBytesToUInt16(); 484 uint16_t packet_id = readTwoBytesToUInt16();
480 485
  486 + if (packet_id == 0)
  487 + {
  488 + throw ProtocolError("Packet ID 0 when unsubscribing is invalid."); // [MQTT-2.3.1-1]
  489 + }
  490 +
481 while (remainingAfterPos() > 0) 491 while (remainingAfterPos() > 0)
482 { 492 {
483 uint16_t topicLength = readTwoBytesToUInt16(); 493 uint16_t topicLength = readTwoBytesToUInt16();
@@ -534,6 +544,11 @@ void MqttPacket::handlePublish() @@ -534,6 +544,11 @@ void MqttPacket::handlePublish()
534 packet_id_pos = pos; 544 packet_id_pos = pos;
535 packet_id = readTwoBytesToUInt16(); 545 packet_id = readTwoBytesToUInt16();
536 546
  547 + if (packet_id == 0)
  548 + {
  549 + throw ProtocolError("Packet ID 0 when publishing is invalid."); // [MQTT-2.3.1-1]
  550 + }
  551 +
537 if (qos == 1) 552 if (qos == 1)
538 { 553 {
539 PubAck pubAck(packet_id); 554 PubAck pubAck(packet_id);