diff --git a/ChangeLog.txt b/ChangeLog.txt index 82683352..74967b7b 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -17,6 +17,8 @@ Broker: Client library: - Set minimum keepalive argument to `mosquitto_connect*()` to be 5 seconds. Closes #1550. +- Fix `mosquitto_topic_matches_sub()` not returning MOSQ_ERR_INVAL if the + topic contains a wildcard. Closes #1589. Clients: - Fix `--remove-retained` not obeying the `-T` option for filtering out diff --git a/lib/util_topic.c b/lib/util_topic.c index 673cc6ca..ae279467 100644 --- a/lib/util_topic.c +++ b/lib/util_topic.c @@ -219,6 +219,9 @@ int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *top spos++; sub++; while(topic[0] != 0 && topic[0] != '/'){ + if(topic[0] == '+' || topic[0] == '#'){ + return MOSQ_ERR_INVAL; + } topic++; } if(topic[0] == 0 && sub[0] == 0){ @@ -234,6 +237,12 @@ int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *top if(sub[1] != 0){ return MOSQ_ERR_INVAL; }else{ + while(topic[0] != 0){ + if(topic[0] == '+' || topic[0] == '#'){ + return MOSQ_ERR_INVAL; + } + topic++; + } *result = true; return MOSQ_ERR_SUCCESS; } @@ -292,6 +301,12 @@ int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *top if((topic[0] != 0 || sub[0] != 0)){ *result = false; } + while(topic[0] != 0){ + if(topic[0] == '+' || topic[0] == '#'){ + return MOSQ_ERR_INVAL; + } + topic++; + } return MOSQ_ERR_SUCCESS; } diff --git a/test/unit/util_topic_test.c b/test/unit/util_topic_test.c index 88f668a2..b669fcc9 100644 --- a/test/unit/util_topic_test.c +++ b/test/unit/util_topic_test.c @@ -20,6 +20,9 @@ static void no_match_helper(int rc_expected, const char *sub, const char *topic) rc = mosquitto_topic_matches_sub(sub, topic, &match); CU_ASSERT_EQUAL(rc, rc_expected); + if(rc != rc_expected){ + printf("%d:%d %s:%s\n", rc, rc_expected, sub, topic); + } CU_ASSERT_EQUAL(match, false); } @@ -135,6 +138,16 @@ static void TEST_invalid_but_matching(void) no_match_helper(MOSQ_ERR_INVAL, "foo/bar#", "foo/bar#"); no_match_helper(MOSQ_ERR_INVAL, "foo+", "fooa"); + + no_match_helper(MOSQ_ERR_INVAL, "foo/+", "foo/+"); + no_match_helper(MOSQ_ERR_INVAL, "foo/#", "foo/+"); + no_match_helper(MOSQ_ERR_INVAL, "foo/+", "foo/bar/+"); + no_match_helper(MOSQ_ERR_INVAL, "foo/#", "foo/bar/+"); + + no_match_helper(MOSQ_ERR_INVAL, "foo/+", "foo/#"); + no_match_helper(MOSQ_ERR_INVAL, "foo/#", "foo/#"); + no_match_helper(MOSQ_ERR_INVAL, "foo/+", "foo/bar/#"); + no_match_helper(MOSQ_ERR_INVAL, "foo/#", "foo/bar/#"); }