From b3045d1adfee180910606213963096e487c590f9 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 10 Jun 2021 21:33:24 +0100 Subject: [PATCH] Fix pattern matching with invalid prefix. --- lib/util_topic.c | 3 +++ test/unit/util_topic_test.c | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/lib/util_topic.c b/lib/util_topic.c index 2454f0cb..8b4df859 100644 --- a/lib/util_topic.c +++ b/lib/util_topic.c @@ -193,6 +193,7 @@ static int topic_matches_sub(const char *sub, const char *topic, const char *cli { size_t spos; const char *pattern_check; + const char *lastchar = NULL; if(!result) return MOSQ_ERR_INVAL; *result = false; @@ -214,6 +215,7 @@ static int topic_matches_sub(const char *sub, const char *topic, const char *cli return MOSQ_ERR_INVAL; } if(match_patterns && + (lastchar == NULL || lastchar[0] == '/') && sub[0] == '%' && (sub[1] == 'c' || sub[1] == 'u') && (sub[2] == '/' || sub[2] == '\0') @@ -337,6 +339,7 @@ static int topic_matches_sub(const char *sub, const char *topic, const char *cli return MOSQ_ERR_SUCCESS; } } + lastchar = sub-1; } if((topic[0] != 0 || sub[0] != 0)){ *result = false; diff --git a/test/unit/util_topic_test.c b/test/unit/util_topic_test.c index a104af87..a5f349db 100644 --- a/test/unit/util_topic_test.c +++ b/test/unit/util_topic_test.c @@ -411,6 +411,11 @@ static void TEST_pattern_both(void) rc = mosquitto_topic_matches_sub_with_pattern("test/%username/%client", "test/username/clientid", "clientid", "username", &match); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); CU_ASSERT_EQUAL(match, false); + + /* Not a pattern */ + rc = mosquitto_topic_matches_sub_with_pattern("test/a%u/a%c", "test/ausername/aclientid", "clientid", "username", &match); + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_EQUAL(match, false); } static void TEST_pattern_wildcard(void)