Fix mosquitto_topic_matches_sub() rc with sub=="topic/#abc".

This now returns MOSQ_ERR_INVAL as expected.
This commit is contained in:
Roger A. Light
2019-02-12 11:34:45 +00:00
parent b6fb2c5824
commit bb914b985c
2 changed files with 18 additions and 3 deletions
+13 -2
View File
@@ -179,7 +179,7 @@ int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *top
if(topic[tpos] == '+' || topic[tpos] == '#'){
return MOSQ_ERR_INVAL;
}
if(sub[spos] != topic[tpos]){
if(tpos == topiclen || sub[spos] != topic[tpos]){ /* Check for wildcard matches */
if(sub[spos] == '+'){
/* Check for bad "+foo" or "a/+foo" subscription */
if(spos > 0 && sub[spos-1] != '/'){
@@ -198,10 +198,11 @@ int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *top
return MOSQ_ERR_SUCCESS;
}
}else if(sub[spos] == '#'){
/* Check for bad "foo#" subscription */
if(spos > 0 && sub[spos-1] != '/'){
return MOSQ_ERR_INVAL;
}
multilevel_wildcard = true;
/* Check for # not the final character of the sub, e.g. "#foo" */
if(spos+1 != sublen){
return MOSQ_ERR_INVAL;
}else{
@@ -221,9 +222,19 @@ int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *top
multilevel_wildcard = true;
return MOSQ_ERR_SUCCESS;
}
/* There is no match at this point, but is the sub invalid? */
for(int i=spos; i<sublen; i++){
if(sub[i] == '#' && i+1 != sublen){
return MOSQ_ERR_INVAL;
}
}
/* Valid input, but no match */
return MOSQ_ERR_SUCCESS;
}
}else{
/* sub[spos] == topic[tpos] */
if(tpos == topiclen-1){
/* Check for e.g. foo matching foo/# */
if(spos == sublen-3
+5 -1
View File
@@ -149,7 +149,6 @@ static void TEST_valid_no_matching(void)
no_match_helper(MOSQ_ERR_SUCCESS, "foo/+/#", "fo2/bar/baz");
no_match_helper(MOSQ_ERR_SUCCESS, "/#", "foo/bar");
no_match_helper(MOSQ_ERR_SUCCESS, "/#a", "foo/bar");
no_match_helper(MOSQ_ERR_SUCCESS, "#", "$SYS/bar");
no_match_helper(MOSQ_ERR_SUCCESS, "$BOB/bar", "$SYS/bar");
@@ -162,6 +161,11 @@ static void TEST_invalid(void)
no_match_helper(MOSQ_ERR_INVAL, "fo#o/", "foo");
no_match_helper(MOSQ_ERR_INVAL, "foo#", "fooa");
no_match_helper(MOSQ_ERR_INVAL, "foo+", "foo");
no_match_helper(MOSQ_ERR_INVAL, "foo/#a", "foo");
no_match_helper(MOSQ_ERR_INVAL, "#a", "foo");
no_match_helper(MOSQ_ERR_INVAL, "foo/#abc", "foo");
no_match_helper(MOSQ_ERR_INVAL, "#abc", "foo");
no_match_helper(MOSQ_ERR_INVAL, "/#a", "foo/bar");
}
/* ========================================================================