From ae666b07ce38d3dea536b12bcfc001fd4250d86b Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 21 Dec 2017 08:36:58 +0000 Subject: [PATCH] [654] Initialise "result" in mosquitto_topic_matches_sub. Thanks to markhermelinggt. Bug: https://github.com/eclipse/mosquitto/issues/654 --- ChangeLog.txt | 2 ++ lib/util_mosq.c | 15 ++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index d356c85e..d77a54fc 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -12,6 +12,8 @@ Broker: Client library: - Fix incorrect PSK key being used if it had leading zeroes. +- Initialise "result" variable as soon as possible in + mosquitto_topic_matches_sub. Closes #654. Build: - Don't run TLS-PSK tests if TLS-PSK disabled at compile time. Closes #636. diff --git a/lib/util_mosq.c b/lib/util_mosq.c index 55e65e9e..6469dfad 100644 --- a/lib/util_mosq.c +++ b/lib/util_mosq.c @@ -228,13 +228,17 @@ int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result int spos, tpos; bool multilevel_wildcard = false; - if(!sub || !topic || !result) return MOSQ_ERR_INVAL; + if(!result) return MOSQ_ERR_INVAL; + *result = false; + + if(!sub || !topic){ + return MOSQ_ERR_INVAL; + } slen = strlen(sub); tlen = strlen(topic); if(!slen || !tlen){ - *result = false; return MOSQ_ERR_INVAL; } @@ -242,7 +246,6 @@ int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result if((sub[0] == '$' && topic[0] != '$') || (topic[0] == '$' && sub[0] != '$')){ - *result = false; return MOSQ_ERR_SUCCESS; } } @@ -269,7 +272,6 @@ int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result return MOSQ_ERR_SUCCESS; }else if(tpos == tlen && spos == slen-1 && sub[spos] == '+'){ if(spos > 0 && sub[spos-1] != '/'){ - *result = false; return MOSQ_ERR_INVAL; } spos++; @@ -280,12 +282,10 @@ int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result if(sub[spos] == '+'){ /* Check for bad "+foo" or "a/+foo" subscription */ if(spos > 0 && sub[spos-1] != '/'){ - *result = false; return MOSQ_ERR_INVAL; } /* Check for bad "foo+" or "foo+/a" subscription */ if(spos < slen-1 && sub[spos+1] != '/'){ - *result = false; return MOSQ_ERR_INVAL; } spos++; @@ -298,19 +298,16 @@ int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result } }else if(sub[spos] == '#'){ if(spos > 0 && sub[spos-1] != '/'){ - *result = false; return MOSQ_ERR_INVAL; } multilevel_wildcard = true; if(spos+1 != slen){ - *result = false; return MOSQ_ERR_INVAL; }else{ *result = true; return MOSQ_ERR_SUCCESS; } }else{ - *result = false; return MOSQ_ERR_SUCCESS; } }