From 6099ee028fbb9b98c439f82d8592aea53a37fbb2 Mon Sep 17 00:00:00 2001 From: Andrei Balasescu Date: Sun, 12 Apr 2020 01:20:08 +0300 Subject: [PATCH] Added check to prevent subscribe/unsubscribe to empty topics --- src/PubSubClient.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/PubSubClient.cpp b/src/PubSubClient.cpp index 70c3b27..a89ff8b 100755 --- a/src/PubSubClient.cpp +++ b/src/PubSubClient.cpp @@ -548,10 +548,14 @@ boolean PubSubClient::subscribe(const char* topic) { } boolean PubSubClient::subscribe(const char* topic, uint8_t qos) { + size_t topicLength = strlen(topic); + if (topic == 0) { + return false; + } if (qos > 1) { return false; } - if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) { + if (MQTT_MAX_PACKET_SIZE < 9 + topicLength) { // Too long return false; } @@ -572,7 +576,11 @@ boolean PubSubClient::subscribe(const char* topic, uint8_t qos) { } boolean PubSubClient::unsubscribe(const char* topic) { - if (MQTT_MAX_PACKET_SIZE < 9 + strlen(topic)) { + size_t topicLength = strlen(topic); + if (topic == 0) { + return false; + } + if (MQTT_MAX_PACKET_SIZE < 9 + topicLength) { // Too long return false; }