From aa89eb35759cea74daaff5ca9c42cedfca314127 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 3 Feb 2025 09:30:57 +0000 Subject: [PATCH] Minor sub tree refactor --- src/database.c | 12 +----------- src/mosquitto_broker_internal.h | 2 +- src/subs.c | 21 +++++++++++++++++++-- test/unit/broker/persist_read_stubs.c | 4 ++++ 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/database.c b/src/database.c index ba7c0e85..05c3939a 100644 --- a/src/database.c +++ b/src/database.c @@ -180,8 +180,6 @@ static void db__msg_remove_from_queued_stats(struct mosquitto_msg_data *msg_data int db__open(struct mosquitto__config *config) { - struct mosquitto__subhier *subhier; - if(!config) return MOSQ_ERR_INVAL; db.contexts_by_id = NULL; @@ -198,15 +196,7 @@ int db__open(struct mosquitto__config *config) db.normal_subs = NULL; db.shared_subs = NULL; - subhier = sub__add_hier_entry(NULL, &db.shared_subs, "", 0); - if(!subhier) return MOSQ_ERR_NOMEM; - - subhier = sub__add_hier_entry(NULL, &db.normal_subs, "", 0); - if(!subhier) return MOSQ_ERR_NOMEM; - - subhier = sub__add_hier_entry(NULL, &db.normal_subs, "$SYS", (uint16_t)strlen("$SYS")); - if(!subhier) return MOSQ_ERR_NOMEM; - + sub__init(); retain__init(); db.config->security_options.unpwd = NULL; diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index d87ae0a8..bdcbe72b 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -743,8 +743,8 @@ void db__check_acl_of_all_messages(struct mosquitto *context); /* ============================================================ * Subscription functions * ============================================================ */ +int sub__init(void); int sub__add(struct mosquitto *context, const struct mosquitto_subscription *sub); -struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len); int sub__remove(struct mosquitto *context, const char *sub, uint8_t *reason); void sub__tree_print(struct mosquitto__subhier *root, int level); int sub__clean_session(struct mosquitto *context); diff --git a/src/subs.c b/src/subs.c index 6045f808..31f384e9 100644 --- a/src/subs.c +++ b/src/subs.c @@ -59,6 +59,8 @@ Contributors: #include "utlist.h" +static struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len); + static int subs__send(struct mosquitto__subleaf *leaf, const char *topic, uint8_t qos, int retain, struct mosquitto__base_msg *stored) { bool client_retain; @@ -523,7 +525,7 @@ static int sub__search(struct mosquitto__subhier *subhier, char **split_topics, } -struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len) +static struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len) { struct mosquitto__subhier *child; @@ -536,7 +538,9 @@ struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent } child->parent = parent; child->topic_len = len; - strncpy(child->topic, topic, len); + if(len > 0){ + strncpy(child->topic, topic, len); + } HASH_ADD(hh, *sibling, topic, child->topic_len, child); @@ -794,3 +798,16 @@ void sub__tree_print(struct mosquitto__subhier *root, int level) sub__tree_print(branch->children, level+1); } } + +int sub__init(void) +{ + if(sub__add_hier_entry(NULL, &db.shared_subs, "", 0) == NULL + || sub__add_hier_entry(NULL, &db.normal_subs, "", 0) == NULL + || sub__add_hier_entry(NULL, &db.normal_subs, "$SYS", (uint16_t)strlen("$SYS")) == NULL + ){ + + return MOSQ_ERR_NOMEM; + }else{ + return MOSQ_ERR_SUCCESS; + } +} diff --git a/test/unit/broker/persist_read_stubs.c b/test/unit/broker/persist_read_stubs.c index 52b338d1..b2708efc 100644 --- a/test/unit/broker/persist_read_stubs.c +++ b/test/unit/broker/persist_read_stubs.c @@ -251,3 +251,7 @@ int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property return MOSQ_ERR_SUCCESS; } +int sub__init(void) +{ + return MOSQ_ERR_SUCCESS; +}