Remove one instance of recursion.

pull/1203/head
Roger A. Light 7 years ago
parent 48253bc12c
commit c95ae6a6fd

@ -453,32 +453,31 @@ static int sub__add_normal(struct mosquitto_db *db, struct mosquitto *context, i
} }
static int sub__add_recurse(struct mosquitto_db *db, struct mosquitto *context, int qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier, struct sub__token *tokens, char *sharename) static int sub__add_context(struct mosquitto_db *db, struct mosquitto *context, int qos, uint32_t identifier, int options, struct mosquitto__subhier *subhier, struct sub__token *tokens, char *sharename)
/* FIXME - this function has the potential to leak subhier, audit calling functions. */
{ {
struct mosquitto__subhier *branch; struct mosquitto__subhier *branch;
if(!tokens){ /* Find leaf node */
if(context && context->id){ while(tokens){
if(sharename){ HASH_FIND(hh, subhier->children, tokens->topic, tokens->topic_len, branch);
return sub__add_shared(db, context, qos, identifier, options, subhier, tokens, sharename); if(!branch){
}else{ /* Not found */
return sub__add_normal(db, context, qos, identifier, options, subhier, tokens); branch = sub__add_hier_entry(subhier, &subhier->children, tokens->topic, tokens->topic_len);
} if(!branch) return MOSQ_ERR_NOMEM;
}else{
return MOSQ_ERR_SUCCESS;
} }
subhier = branch;
tokens = tokens ->next;
} }
HASH_FIND(hh, subhier->children, tokens->topic, tokens->topic_len, branch); /* Add add our context */
if(branch){ if(context && context->id){
return sub__add_recurse(db, context, qos, identifier, options, branch, tokens->next, sharename); if(sharename){
return sub__add_shared(db, context, qos, identifier, options, subhier, tokens, sharename);
}else{
return sub__add_normal(db, context, qos, identifier, options, subhier, tokens);
}
}else{ }else{
/* Not found */ return MOSQ_ERR_SUCCESS;
branch = sub__add_hier_entry(subhier, &subhier->children, tokens->topic, tokens->topic_len);
if(!branch) return MOSQ_ERR_NOMEM;
return sub__add_recurse(db, context, qos, identifier, options, branch, tokens->next, sharename);
} }
} }
@ -740,7 +739,7 @@ int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub
} }
} }
rc = sub__add_recurse(db, context, qos, identifier, options, subhier, tokens, sharename); rc = sub__add_context(db, context, qos, identifier, options, subhier, tokens, sharename);
sub__topic_tokens_free(tokens); sub__topic_tokens_free(tokens);
@ -814,7 +813,7 @@ int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const ch
/* We have a message that needs to be retained, so ensure that the subscription /* We have a message that needs to be retained, so ensure that the subscription
* tree for its topic exists. * tree for its topic exists.
*/ */
sub__add_recurse(db, NULL, 0, 0, 0, subhier, tokens, NULL); sub__add_context(db, NULL, 0, 0, 0, subhier, tokens, NULL);
} }
rc = sub__search(db, subhier, tokens, source_id, topic, qos, retain, *stored, true); rc = sub__search(db, subhier, tokens, source_id, topic, qos, retain, *stored, true);
} }

Loading…
Cancel
Save