From 07d712d5aecbf7b8628f83d803fafeda25eb929a Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 24 Sep 2019 20:38:01 +0200 Subject: [PATCH] Fix a crash in topic matching. Subscribing a client to a/b/c and publishing to a/b leads to a crash, turns out it is a off-by-one calculation in topic matching code. --- libnymea-mqtt/mqttserver.cpp | 3 +++ tests/operation/test_operation.cpp | 1 + 2 files changed, 4 insertions(+) diff --git a/libnymea-mqtt/mqttserver.cpp b/libnymea-mqtt/mqttserver.cpp index b0a37cb..b7e86e4 100644 --- a/libnymea-mqtt/mqttserver.cpp +++ b/libnymea-mqtt/mqttserver.cpp @@ -628,6 +628,9 @@ bool MqttServerPrivate::matchTopic(const QString &topicFilter, const QString &to if (filterParts.at(i) == QStringLiteral("#")) { continue; } + if (topicParts.length() <= i) { + return false; + } if (topicParts.at(i) == filterParts.at(i)) { continue; } diff --git a/tests/operation/test_operation.cpp b/tests/operation/test_operation.cpp index b5ee4a9..d5afe57 100644 --- a/tests/operation/test_operation.cpp +++ b/tests/operation/test_operation.cpp @@ -489,6 +489,7 @@ void OperationTests::testSubscriptionTopicMatching_data() rows.append({ "a//+/", "a///", "1" }); rows.append({ "a//+/#", "a//b/c", "1" }); rows.append({ "a//+/#", "a/b/c/d", "0" }); + rows.append({ "a/b/c", "a/b", "0"}); rows.append({ "$SYS/", "$SYS/", "0" }); rows.append({ "#", "$SYS/", "0" });