Refactor to remove duplicate code.

pull/2345/merge
Roger A. Light 3 years ago
parent b49631df23
commit f3777c0662

@ -265,9 +265,7 @@ static int persist__base_msg_chunk_restore(FILE *db_fptr, uint32_t length)
}else{ }else{
rc = persist__chunk_base_msg_read_v234(db_fptr, &chunk, db_version); rc = persist__chunk_base_msg_read_v234(db_fptr, &chunk, db_version);
} }
if(rc){ if(rc) return rc;
return rc;
}
if(chunk.F.source_port){ if(chunk.F.source_port){
for(i=0; i<db.config->listener_count; i++){ for(i=0; i<db.config->listener_count; i++){
@ -282,11 +280,8 @@ static int persist__base_msg_chunk_restore(FILE *db_fptr, uint32_t length)
message_expiry_interval64 = chunk.F.expiry_time - time(NULL); message_expiry_interval64 = chunk.F.expiry_time - time(NULL);
if(message_expiry_interval64 < 0 || message_expiry_interval64 > UINT32_MAX){ if(message_expiry_interval64 < 0 || message_expiry_interval64 > UINT32_MAX){
/* Expired message */ /* Expired message */
mosquitto__FREE(chunk.source.id); rc = MOSQ_ERR_SUCCESS;
mosquitto__FREE(chunk.source.username); goto cleanup;
mosquitto__FREE(chunk.topic);
mosquitto__FREE(chunk.payload);
return MOSQ_ERR_SUCCESS;
}else{ }else{
message_expiry_interval = (uint32_t)message_expiry_interval64; message_expiry_interval = (uint32_t)message_expiry_interval64;
} }
@ -296,12 +291,9 @@ static int persist__base_msg_chunk_restore(FILE *db_fptr, uint32_t length)
base_msg = mosquitto__calloc(1, sizeof(struct mosquitto_base_msg)); base_msg = mosquitto__calloc(1, sizeof(struct mosquitto_base_msg));
if(base_msg == NULL){ if(base_msg == NULL){
mosquitto__FREE(chunk.source.id);
mosquitto__FREE(chunk.source.username);
mosquitto__FREE(chunk.topic);
mosquitto__FREE(chunk.payload);
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM; rc = MOSQ_ERR_NOMEM;
goto cleanup;
} }
base_msg->source_mid = chunk.F.source_mid; base_msg->source_mid = chunk.F.source_mid;
@ -325,6 +317,12 @@ static int persist__base_msg_chunk_restore(FILE *db_fptr, uint32_t length)
}else{ }else{
return rc; return rc;
} }
cleanup:
mosquitto__FREE(chunk.source.id);
mosquitto__FREE(chunk.source.username);
mosquitto__FREE(chunk.topic);
mosquitto__FREE(chunk.payload);
return rc;
} }
static int persist__retain_chunk_restore(FILE *db_fptr) static int persist__retain_chunk_restore(FILE *db_fptr)

@ -99,7 +99,6 @@ int persist__chunk_client_msg_read_v234(FILE *db_fptr, struct P_client_msg *chun
{ {
uint16_t i16temp; uint16_t i16temp;
int rc; int rc;
char *err;
uint8_t retain, dup; uint8_t retain, dup;
rc = persist__read_string(db_fptr, &chunk->client_id); rc = persist__read_string(db_fptr, &chunk->client_id);
@ -122,8 +121,7 @@ int persist__chunk_client_msg_read_v234(FILE *db_fptr, struct P_client_msg *chun
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
error: error:
err = strerror(errno); log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
mosquitto__FREE(chunk->client_id); mosquitto__FREE(chunk->client_id);
return 1; return 1;
} }
@ -134,20 +132,15 @@ int persist__chunk_base_msg_read_v234(FILE *db_fptr, struct P_base_msg *chunk, u
uint32_t i32temp; uint32_t i32temp;
uint16_t i16temp; uint16_t i16temp;
int rc = 0; int rc = 0;
char *err;
read_e(db_fptr, &chunk->F.store_id, sizeof(dbid_t)); read_e(db_fptr, &chunk->F.store_id, sizeof(dbid_t));
rc = persist__read_string(db_fptr, &chunk->source.id); rc = persist__read_string(db_fptr, &chunk->source.id);
if(rc){ if(rc) return rc;
return rc;
}
if(db_version == 4){ if(db_version == 4){
rc = persist__read_string(db_fptr, &chunk->source.username); rc = persist__read_string(db_fptr, &chunk->source.username);
if(rc){ if(rc) goto error;
mosquitto__FREE(chunk->source.id);
return rc;
}
read_e(db_fptr, &i16temp, sizeof(uint16_t)); read_e(db_fptr, &i16temp, sizeof(uint16_t));
chunk->F.source_port = ntohs(i16temp); chunk->F.source_port = ntohs(i16temp);
} }
@ -159,11 +152,7 @@ int persist__chunk_base_msg_read_v234(FILE *db_fptr, struct P_base_msg *chunk, u
read_e(db_fptr, &i16temp, sizeof(uint16_t)); read_e(db_fptr, &i16temp, sizeof(uint16_t));
rc = persist__read_string(db_fptr, &chunk->topic); rc = persist__read_string(db_fptr, &chunk->topic);
if(rc){ if(rc) goto error;
mosquitto__FREE(chunk->source.id);
mosquitto__FREE(chunk->source.username);
return rc;
}
read_e(db_fptr, &chunk->F.qos, sizeof(uint8_t)); read_e(db_fptr, &chunk->F.qos, sizeof(uint8_t));
read_e(db_fptr, &chunk->F.retain, sizeof(uint8_t)); read_e(db_fptr, &chunk->F.retain, sizeof(uint8_t));
@ -174,11 +163,8 @@ int persist__chunk_base_msg_read_v234(FILE *db_fptr, struct P_base_msg *chunk, u
if(chunk->F.payloadlen){ if(chunk->F.payloadlen){
chunk->payload = mosquitto_malloc(chunk->F.payloadlen+1); chunk->payload = mosquitto_malloc(chunk->F.payloadlen+1);
if(chunk->payload == NULL){ if(chunk->payload == NULL){
mosquitto__FREE(chunk->source.id); rc = MOSQ_ERR_NOMEM;
mosquitto__FREE(chunk->source.username); goto error;
mosquitto__FREE(chunk->topic);
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
} }
read_e(db_fptr, chunk->payload, chunk->F.payloadlen); read_e(db_fptr, chunk->payload, chunk->F.payloadlen);
/* Ensure zero terminated regardless of contents */ /* Ensure zero terminated regardless of contents */
@ -187,22 +173,19 @@ int persist__chunk_base_msg_read_v234(FILE *db_fptr, struct P_base_msg *chunk, u
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
error: error:
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
mosquitto__FREE(chunk->source.id); mosquitto__FREE(chunk->source.id);
mosquitto__FREE(chunk->source.username); mosquitto__FREE(chunk->source.username);
return 1; mosquitto__FREE(chunk->topic);
return rc;
} }
int persist__chunk_retain_read_v234(FILE *db_fptr, struct P_retain *chunk) int persist__chunk_retain_read_v234(FILE *db_fptr, struct P_retain *chunk)
{ {
dbid_t i64temp; dbid_t i64temp;
char *err;
if(fread(&i64temp, sizeof(dbid_t), 1, db_fptr) != 1){ if(fread(&i64temp, sizeof(dbid_t), 1, db_fptr) != 1){
err = strerror(errno); log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
return 1; return 1;
} }
chunk->F.store_id = i64temp; chunk->F.store_id = i64temp;
@ -214,28 +197,20 @@ int persist__chunk_retain_read_v234(FILE *db_fptr, struct P_retain *chunk)
int persist__chunk_sub_read_v234(FILE *db_fptr, struct P_sub *chunk) int persist__chunk_sub_read_v234(FILE *db_fptr, struct P_sub *chunk)
{ {
int rc; int rc;
char *err;
rc = persist__read_string(db_fptr, &chunk->client_id); rc = persist__read_string(db_fptr, &chunk->client_id);
if(rc){ if(rc) goto error;
return rc;
}
rc = persist__read_string(db_fptr, &chunk->topic); rc = persist__read_string(db_fptr, &chunk->topic);
if(rc){ if(rc) goto error;
mosquitto__FREE(chunk->client_id);
return rc;
}
read_e(db_fptr, &chunk->F.qos, sizeof(uint8_t)); read_e(db_fptr, &chunk->F.qos, sizeof(uint8_t));
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
error: error:
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
mosquitto__FREE(chunk->client_id); mosquitto__FREE(chunk->client_id);
mosquitto__FREE(chunk->topic); mosquitto__FREE(chunk->topic);
return 1; return rc;
} }
#endif #endif

@ -120,23 +120,19 @@ int persist__chunk_client_msg_read_v56(FILE *db_fptr, struct P_client_msg *chunk
length -= (uint32_t)(sizeof(struct PF_client_msg) + chunk->F.id_len); length -= (uint32_t)(sizeof(struct PF_client_msg) + chunk->F.id_len);
rc = persist__read_string_len(db_fptr, &chunk->client_id, chunk->F.id_len); rc = persist__read_string_len(db_fptr, &chunk->client_id, chunk->F.id_len);
if(rc){ if(rc) return rc;
return rc;
}
if(length > 0){ if(length > 0){
memset(&prop_packet, 0, sizeof(struct mosquitto__packet)); memset(&prop_packet, 0, sizeof(struct mosquitto__packet));
prop_packet.remaining_length = length; prop_packet.remaining_length = length;
prop_packet.payload = mosquitto__malloc(length); prop_packet.payload = mosquitto__malloc(length);
if(!prop_packet.payload){ if(!prop_packet.payload) return MOSQ_ERR_NOMEM;
return MOSQ_ERR_NOMEM;
}
read_e(db_fptr, prop_packet.payload, length); read_e(db_fptr, prop_packet.payload, length);
rc = property__read_all(CMD_PUBLISH, &prop_packet, &properties); rc = property__read_all(CMD_PUBLISH, &prop_packet, &properties);
mosquitto__FREE(prop_packet.payload); mosquitto__FREE(prop_packet.payload);
if(rc){ if(rc) return rc;
return rc;
}
if(properties){ if(properties){
p = properties; p = properties;
while(p){ while(p){
@ -179,32 +175,20 @@ int persist__chunk_base_msg_read_v56(FILE *db_fptr, struct P_base_msg *chunk, ui
if(chunk->F.source_id_len){ if(chunk->F.source_id_len){
rc = persist__read_string_len(db_fptr, &chunk->source.id, chunk->F.source_id_len); rc = persist__read_string_len(db_fptr, &chunk->source.id, chunk->F.source_id_len);
if(rc){ if(rc) goto error;
return rc;
}
} }
if(chunk->F.source_username_len){ if(chunk->F.source_username_len){
rc = persist__read_string_len(db_fptr, &chunk->source.username, chunk->F.source_username_len); rc = persist__read_string_len(db_fptr, &chunk->source.username, chunk->F.source_username_len);
if(rc){ if(rc) goto error;
mosquitto__FREE(chunk->source.id);
return rc;
}
} }
rc = persist__read_string_len(db_fptr, &chunk->topic, chunk->F.topic_len); rc = persist__read_string_len(db_fptr, &chunk->topic, chunk->F.topic_len);
if(rc){ if(rc) goto error;
mosquitto__FREE(chunk->source.id);
mosquitto__FREE(chunk->source.username);
return rc;
}
if(chunk->F.payloadlen > 0){ if(chunk->F.payloadlen > 0){
chunk->payload = mosquitto__malloc(chunk->F.payloadlen+1); chunk->payload = mosquitto__malloc(chunk->F.payloadlen+1);
if(chunk->payload == NULL){ if(chunk->payload == NULL){
mosquitto__FREE(chunk->source.id); rc = MOSQ_ERR_NOMEM;
mosquitto__FREE(chunk->source.username); goto error;
mosquitto__FREE(chunk->topic);
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
} }
read_e(db_fptr, chunk->payload, chunk->F.payloadlen); read_e(db_fptr, chunk->payload, chunk->F.payloadlen);
/* Ensure zero terminated regardless of contents */ /* Ensure zero terminated regardless of contents */
@ -215,31 +199,26 @@ int persist__chunk_base_msg_read_v56(FILE *db_fptr, struct P_base_msg *chunk, ui
prop_packet.remaining_length = length; prop_packet.remaining_length = length;
prop_packet.payload = mosquitto__malloc(length); prop_packet.payload = mosquitto__malloc(length);
if(!prop_packet.payload){ if(!prop_packet.payload){
mosquitto__FREE(chunk->source.id); rc = MOSQ_ERR_NOMEM;
mosquitto__FREE(chunk->source.username); goto error;
mosquitto__FREE(chunk->topic);
return MOSQ_ERR_NOMEM;
} }
read_e(db_fptr, prop_packet.payload, length); read_e(db_fptr, prop_packet.payload, length);
rc = property__read_all(CMD_PUBLISH, &prop_packet, &properties); rc = property__read_all(CMD_PUBLISH, &prop_packet, &properties);
mosquitto__FREE(prop_packet.payload); mosquitto__FREE(prop_packet.payload);
if(rc){ if(rc){
mosquitto__FREE(chunk->source.id); rc = MOSQ_ERR_NOMEM;
mosquitto__FREE(chunk->source.username); goto error;
mosquitto__FREE(chunk->topic);
return rc;
} }
} }
chunk->properties = properties; chunk->properties = properties;
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
error: error:
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
mosquitto__FREE(chunk->source.id); mosquitto__FREE(chunk->source.id);
mosquitto__FREE(chunk->source.username); mosquitto__FREE(chunk->source.username);
mosquitto__FREE(chunk->topic); mosquitto__FREE(chunk->topic);
mosquitto__FREE(prop_packet.payload); mosquitto__FREE(prop_packet.payload);
return 1; return rc;
} }
@ -255,7 +234,7 @@ int persist__chunk_retain_read_v56(FILE *db_fptr, struct P_retain *chunk)
int persist__chunk_sub_read_v56(FILE *db_fptr, struct P_sub *chunk) int persist__chunk_sub_read_v56(FILE *db_fptr, struct P_sub *chunk)
{ {
int rc; int rc = MOSQ_ERR_SUCCESS;
read_e(db_fptr, &chunk->F, sizeof(struct PF_sub)); read_e(db_fptr, &chunk->F, sizeof(struct PF_sub));
chunk->F.identifier = ntohl(chunk->F.identifier); chunk->F.identifier = ntohl(chunk->F.identifier);
@ -263,19 +242,15 @@ int persist__chunk_sub_read_v56(FILE *db_fptr, struct P_sub *chunk)
chunk->F.topic_len = ntohs(chunk->F.topic_len); chunk->F.topic_len = ntohs(chunk->F.topic_len);
rc = persist__read_string_len(db_fptr, &chunk->client_id, chunk->F.id_len); rc = persist__read_string_len(db_fptr, &chunk->client_id, chunk->F.id_len);
if(rc){ if(rc) goto error;
return rc;
}
rc = persist__read_string_len(db_fptr, &chunk->topic, chunk->F.topic_len); rc = persist__read_string_len(db_fptr, &chunk->topic, chunk->F.topic_len);
if(rc){ if(rc) goto error;
mosquitto__FREE(chunk->client_id);
return rc;
}
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
error: error:
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno)); mosquitto__FREE(chunk->client_id);
return 1; return rc;
} }
#endif #endif

Loading…
Cancel
Save