diff --git a/src/persist_write.c b/src/persist_write.c index aac3554c..7147b6e5 100644 --- a/src/persist_write.c +++ b/src/persist_write.c @@ -94,12 +94,12 @@ static int persist__message_store_save(struct mosquitto_db *db, FILE *db_fptr) stored = db->msg_store; while(stored){ - if(stored->ref_count < 1){ + if(stored->ref_count < 1 || stored->topic == NULL){ stored = stored->next; continue; } - if(stored->topic && !strncmp(stored->topic, "$SYS", 4)){ + if(!strncmp(stored->topic, "$SYS", 4)){ if(stored->ref_count <= 1 && stored->dest_id_count == 0){ /* $SYS messages that are only retained shouldn't be persisted. */ stored = stored->next; @@ -132,13 +132,10 @@ static int persist__message_store_save(struct mosquitto_db *db, FILE *db_fptr) chunk.F.source_username_len = 0; chunk.source.username = NULL; } - if(stored->topic){ - chunk.F.topic_len = strlen(stored->topic); - chunk.topic = stored->topic; - }else{ - chunk.F.topic_len = 0; - chunk.topic = NULL; - } + + chunk.F.topic_len = strlen(stored->topic); + chunk.topic = stored->topic; + if(stored->source_listener){ chunk.F.source_port = stored->source_listener->port; }else{ diff --git a/test/unit/persist_read_test.c b/test/unit/persist_read_test.c index 5b0ad2b6..d0c9f7ae 100644 --- a/test/unit/persist_read_test.c +++ b/test/unit/persist_read_test.c @@ -210,7 +210,10 @@ static void TEST_v3_message_store(void) CU_ASSERT_EQUAL(db.msg_store->mid, 0); CU_ASSERT_EQUAL(db.msg_store->qos, 2); CU_ASSERT_EQUAL(db.msg_store->retain, 1); - CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic"); + CU_ASSERT_PTR_NOT_NULL(db.msg_store->topic); + if(db.msg_store->topic){ + CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic"); + } CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7); if(db.msg_store->payloadlen == 7){ CU_ASSERT_NSTRING_EQUAL(UHPA_ACCESS_PAYLOAD(db.msg_store), "payload", 7); @@ -276,7 +279,10 @@ static void TEST_v3_client_message(void) CU_ASSERT_EQUAL(context->inflight_msgs->store->mid, 0); CU_ASSERT_EQUAL(context->inflight_msgs->store->qos, 2); CU_ASSERT_EQUAL(context->inflight_msgs->store->retain, 1); - CU_ASSERT_STRING_EQUAL(context->inflight_msgs->store->topic, "topic"); + CU_ASSERT_PTR_NOT_NULL(context->inflight_msgs->store->topic); + if(context->inflight_msgs->store->topic){ + CU_ASSERT_STRING_EQUAL(context->inflight_msgs->store->topic, "topic"); + } CU_ASSERT_EQUAL(context->inflight_msgs->store->payloadlen, 7); if(context->inflight_msgs->store->payloadlen == 7){ CU_ASSERT_NSTRING_EQUAL(UHPA_ACCESS_PAYLOAD(context->inflight_msgs->store), "payload", 7); @@ -320,7 +326,10 @@ static void TEST_v3_retain(void) CU_ASSERT_EQUAL(db.msg_store->mid, 0); CU_ASSERT_EQUAL(db.msg_store->qos, 2); CU_ASSERT_EQUAL(db.msg_store->retain, 1); - CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic"); + CU_ASSERT_PTR_NOT_NULL(db.msg_store->topic); + if(db.msg_store->topic){ + CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic"); + } CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7); if(db.msg_store->payloadlen == 7){ CU_ASSERT_NSTRING_EQUAL(UHPA_ACCESS_PAYLOAD(db.msg_store), "payload", 7); @@ -387,7 +396,10 @@ static void TEST_v4_message_store(void) CU_ASSERT_EQUAL(db.msg_store->mid, 0); CU_ASSERT_EQUAL(db.msg_store->qos, 1); CU_ASSERT_EQUAL(db.msg_store->retain, 0); - CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic"); + CU_ASSERT_PTR_NOT_NULL(db.msg_store->topic); + if(db.msg_store->topic){ + CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic"); + } CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7); if(db.msg_store->payloadlen == 7){ CU_ASSERT_NSTRING_EQUAL(UHPA_ACCESS_PAYLOAD(db.msg_store), "payload", 7);