diff --git a/include/mosquitto_broker.h b/include/mosquitto_broker.h index 75ca52dc..c1528094 100644 --- a/include/mosquitto_broker.h +++ b/include/mosquitto_broker.h @@ -255,7 +255,6 @@ struct mosquitto_evt_persist_subscription { * it may change in a future minor release. */ struct mosquitto_evt_persist_client_msg { const char *client_id; - char *plugin_client_id; uint64_t cmsg_id; uint64_t store_id; uint32_t subscription_identifier; @@ -282,8 +281,6 @@ struct mosquitto_evt_persist_base_msg { const char *source_username; char *plugin_topic; void *plugin_payload; - char *plugin_source_id; - char *plugin_source_username; const mosquitto_property *properties; mosquitto_property *plugin_properties; uint32_t payloadlen; diff --git a/plugins/persist-sqlite/restore.c b/plugins/persist-sqlite/restore.c index 5f0a0f97..b895a23e 100644 --- a/plugins/persist-sqlite/restore.c +++ b/plugins/persist-sqlite/restore.c @@ -216,25 +216,8 @@ static int msg_restore(struct mosquitto_sqlite *ms) continue; } } - str = (const char *)sqlite3_column_text(stmt, 4); - if(str){ - msg.plugin_source_id = strdup(str); - if(!msg.plugin_source_id){ - free(msg.plugin_topic); - failed++; - continue; - } - } - str = (const char *)sqlite3_column_text(stmt, 5); - if(str){ - msg.plugin_source_username = strdup(str); - if(!msg.plugin_source_username){ - free(msg.plugin_topic); - free(msg.plugin_source_id); - failed++; - continue; - } - } + msg.source_id = (const char *)sqlite3_column_text(stmt, 4); + msg.source_username = (const char *)sqlite3_column_text(stmt, 5); payload = (const void *)sqlite3_column_blob(stmt, 3); msg.payloadlen = (uint32_t)sqlite3_column_int(stmt, 6); if(payload && msg.payloadlen){ @@ -242,8 +225,6 @@ static int msg_restore(struct mosquitto_sqlite *ms) if(!msg.plugin_payload){ free(msg.plugin_topic); free(msg.plugin_topic); - free(msg.plugin_source_id); - free(msg.plugin_source_username); failed++; continue; } @@ -277,7 +258,6 @@ static int client_msg_restore(struct mosquitto_sqlite *ms) struct mosquitto_evt_persist_client_msg msg; int rc; long count = 0, failed = 0; - const char *str; rc = sqlite3_prepare_v2(ms->db, "SELECT client_id, store_id, dup, direction, mid, qos, retain, state " @@ -291,10 +271,7 @@ static int client_msg_restore(struct mosquitto_sqlite *ms) memset(&msg, 0, sizeof(msg)); while(sqlite3_step(stmt) == SQLITE_ROW){ - str = (const char *)sqlite3_column_text(stmt, 0); - if(str){ - msg.plugin_client_id = strdup(str); - } + msg.client_id = (const char *)sqlite3_column_text(stmt, 0); msg.store_id = (uint64_t)sqlite3_column_int64(stmt, 1); msg.dup = sqlite3_column_int(stmt, 2); msg.direction = (uint8_t)sqlite3_column_int(stmt, 3); diff --git a/src/plugin_public.c b/src/plugin_public.c index b55f150e..25f4f4ba 100644 --- a/src/plugin_public.c +++ b/src/plugin_public.c @@ -614,13 +614,11 @@ int mosquitto_persist_client_msg_add(struct mosquitto_evt_persist_client_msg *cl struct mosquitto *context; struct mosquitto_base_msg *base_msg; - if(client_msg == NULL || client_msg->plugin_client_id == NULL){ - free(client_msg->plugin_client_id); + if(client_msg == NULL || client_msg->client_id == NULL){ return MOSQ_ERR_INVAL; } - HASH_FIND(hh_id, db.contexts_by_id, client_msg->plugin_client_id, strlen(client_msg->plugin_client_id), context); - free(client_msg->plugin_client_id); + HASH_FIND(hh_id, db.contexts_by_id, client_msg->client_id, strlen(client_msg->client_id), context); if(context == NULL){ return MOSQ_ERR_NOT_FOUND; } @@ -646,9 +644,9 @@ int mosquitto_persist_client_msg_delete(struct mosquitto_evt_persist_client_msg { struct mosquitto *context; - if(client_msg == NULL || client_msg->plugin_client_id == NULL) return MOSQ_ERR_INVAL; + if(client_msg == NULL || client_msg->client_id == NULL) return MOSQ_ERR_INVAL; - HASH_FIND(hh_id, db.contexts_by_id, client_msg->plugin_client_id, strlen(client_msg->plugin_client_id), context); + HASH_FIND(hh_id, db.contexts_by_id, client_msg->client_id, strlen(client_msg->client_id), context); if(context == NULL){ return MOSQ_ERR_NOT_FOUND; } @@ -666,9 +664,9 @@ int mosquitto_persist_client_msg_update(struct mosquitto_evt_persist_client_msg { struct mosquitto *context; - if(client_msg == NULL || client_msg->plugin_client_id == NULL) return MOSQ_ERR_INVAL; + if(client_msg == NULL || client_msg->client_id == NULL) return MOSQ_ERR_INVAL; - HASH_FIND(hh_id, db.contexts_by_id, client_msg->plugin_client_id, strlen(client_msg->plugin_client_id), context); + HASH_FIND(hh_id, db.contexts_by_id, client_msg->client_id, strlen(client_msg->client_id), context); if(context == NULL){ return MOSQ_ERR_NOT_FOUND; } @@ -730,8 +728,10 @@ int mosquitto_persist_base_msg_add(struct mosquitto_evt_persist_base_msg *msg) memset(&context, 0, sizeof(context)); - context.id = msg->plugin_source_id; - context.username = msg->plugin_source_username; + /* db__message_store only takes a copy of .id and .username, so it is reasonably safe + * to cast the const char * to char * */ + context.id = (char *)msg->source_id; + context.username = (char *)msg->source_username; if(msg->expiry_time == 0){ message_expiry_interval = 0; @@ -771,8 +771,6 @@ int mosquitto_persist_base_msg_add(struct mosquitto_evt_persist_base_msg *msg) } } rc = db__message_store(&context, base_msg, message_expiry_interval, msg->store_id, mosq_mo_broker); - free(context.id); - free(context.username); return rc; error: