From 60ac1cde91c67f666fe78b670abc3a6fea6f44f9 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Mon, 12 Jun 2023 21:34:50 +0200 Subject: [PATCH] [Fix] Bugfix in hasWildcard --- src/mqttclient.cpp | 17 ++++++++++++++++- src/mqttutil.cpp | 7 ++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index a4d2564..42eafb8 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -215,9 +215,17 @@ void MqttClient::disconnect() Token MqttClient::publish(const MqttMessage& message, int qos) { - if (hasWildcard(message.topic()) || !isValidTopic(message.topic())) { + if (hasWildcard(message.topic())) + { + LogDebug("[MqttClient::publish]","Topic has wildcard : " + message.topic()); return Token(m_clientId, -1); } + else if(!isValidTopic(message.topic())) + { + LogDebug("[MqttClient::publish]","Topic is invalid : " + message.topic()); + return Token(m_clientId, -1); + } + OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); IMqttClientImpl* client(nullptr); { @@ -239,6 +247,13 @@ Token MqttClient::publish(const MqttMessage& message, int qos) } client = m_principalClient.get(); } + + if(!client) + { + LogDebug("[MqttClient::publish]", "Invalid pointer to IMqttClient retrieved."); + return Token(m_clientId, -1); + } + return Token(client->clientId(), client->publish(message, qos)); } diff --git a/src/mqttutil.cpp b/src/mqttutil.cpp index 4f22c15..a7b3376 100644 --- a/src/mqttutil.cpp +++ b/src/mqttutil.cpp @@ -55,7 +55,12 @@ bool isValidTopic( const std::string &topic ) bool hasWildcard( const std::string &topic ) { - return ( topic.size() > 0 && (topic.find( '+' ) != std::string::npos || topic.size() - 1 == '#' ) ); + return ( topic.size() > 0 && + ( + topic.find( '+' ) != std::string::npos || + topic.back() == '#' + ) + ); } bool testForOverlap( const std::string &existingTopic, const std::string &newTopic ) -- libgit2 0.21.4