Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 1eb27ba

Browse files
committed
fix(coverity): Fix errors reported by Coverity
`bundle_array_copy()` has been implemented now, so `utarray_push_back()` can copy the whole `bundle` object to the last element of the bundle_array object. Therefore, we can use `bundle_transaction_free()` instead of using `free()` on `bundle_transaction_t` object. Memory leaks and the statement that execution can't reach are solved, too.
1 parent c63558b commit 1eb27ba

File tree

10 files changed

+57
-28
lines changed

10 files changed

+57
-28
lines changed

accelerator/core/core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ status_t ta_get_bundles_by_addr(const iota_client_service_t* const service, tryt
525525

526526
bundle_array_add(bundle_array, bundle);
527527
hash243_set_add(&bundle_hash_set, transaction_bundle(curr_tx));
528-
free(bundle);
528+
bundle_transactions_free(&bundle);
529529
}
530530
}
531531
hash243_set_free(&bundle_hash_set);
@@ -661,8 +661,8 @@ status_t broadcast_buffered_txn(const ta_core_t* const core) {
661661
* Delete UUID-sent_transaction pair from key-value storage
662662
*/
663663

664-
get_trytes_req_t* req;
665-
get_trytes_res_t* res;
664+
get_trytes_req_t* req = NULL;
665+
get_trytes_res_t* res = NULL;
666666
do {
667667
char uuid[UUID_STR_LEN];
668668
flex_trit_t req_txn_flex_trits[NUM_FLEX_TRITS_SERIALIZED_TRANSACTION + 1];

accelerator/core/mam_core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,15 @@ static status_t create_channel_fetch_all_transactions(const iota_client_service_
235235
status_t ret = SC_OK;
236236
find_transactions_req_t *txn_req = find_transactions_req_new();
237237
transaction_array_t *obj_res = transaction_array_new();
238+
if (!txn_req || !obj_res) {
239+
ret = SC_MAM_NULL;
240+
ta_log_error("%s\n", ta_error_to_string(ret));
241+
goto done;
242+
}
238243

239244
if (mam_api_channel_create(api, channel_depth, chid) != RC_OK) {
240245
ret = SC_MAM_FAILED_CREATE_OR_GET_ID;
241-
ta_log_error("%s\n", "SC_MAM_FAILED_CREATE_OR_GET_ID");
246+
ta_log_error("%s\n", ta_error_to_string(ret));
242247
goto done;
243248
}
244249

accelerator/core/serializer/serializer.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ static status_t send_mam_message_mam_v1_req_deserialize(cJSON const* const json_
790790
json_value = cJSON_GetObjectItemCaseSensitive(json_key, "message");
791791
if (json_value != NULL) {
792792
size_t payload_size = strlen(json_value->valuestring);
793-
char* payload = (char*)malloc((payload_size + 1) * sizeof(char));
793+
data->message = (char*)malloc((payload_size + 1) * sizeof(char));
794794

795795
// In case the payload is unicode, the character whose ASCII code is beyond 128 result to an
796796
// error status_t code
@@ -801,9 +801,7 @@ static status_t send_mam_message_mam_v1_req_deserialize(cJSON const* const json_
801801
goto done;
802802
}
803803
}
804-
805-
snprintf(payload, payload_size + 1, "%s", json_value->valuestring);
806-
data->message = payload;
804+
memcpy(data->message, json_value->valuestring, payload_size + 1);
807805
} else {
808806
ta_log_error("%s\n", ta_error_to_string(SC_SERIALIZER_NULL));
809807
ret = SC_SERIALIZER_NULL;
@@ -933,49 +931,55 @@ static status_t recv_mam_message_mam_v1_req_deserialize(cJSON const* const json_
933931
if (cJSON_HasObjectItem(json_key, "bundle_hash")) {
934932
json_value = cJSON_GetObjectItemCaseSensitive(json_key, "bundle_hash");
935933
if (json_value == NULL) {
936-
ta_log_error("%s\n", ta_error_to_string(SC_CCLIENT_JSON_KEY));
937-
return SC_CCLIENT_JSON_KEY;
934+
ret = SC_CCLIENT_JSON_KEY;
935+
ta_log_error("%s\n", ta_error_to_string(ret));
936+
return ret;
938937
}
939938
if (cJSON_IsString(json_value) && (json_value->valuestring != NULL) &&
940939
(strlen(json_value->valuestring) == NUM_TRYTES_HASH)) {
941940
bundle_hash = (char*)malloc(sizeof(char) * (NUM_TRYTES_HASH + 1));
942941
strncpy(bundle_hash, json_value->valuestring, sizeof(char) * (NUM_TRYTES_HASH + 1));
943942
} else {
944-
ta_log_error("%s\n", ta_error_to_string(SC_CCLIENT_JSON_PARSE));
945-
return SC_CCLIENT_JSON_PARSE;
943+
ret = SC_CCLIENT_JSON_PARSE;
944+
ta_log_error("%s\n", ta_error_to_string(ret));
945+
return ret;
946946
}
947947
goto set_data_id;
948948
}
949949

950950
if (cJSON_HasObjectItem(json_key, "chid")) {
951951
json_value = cJSON_GetObjectItemCaseSensitive(json_key, "chid");
952952
if (json_value == NULL) {
953-
ta_log_error("%s\n", ta_error_to_string(SC_CCLIENT_JSON_KEY));
954-
return SC_CCLIENT_JSON_KEY;
953+
ret = SC_CCLIENT_JSON_KEY;
954+
ta_log_error("%s\n", ta_error_to_string(ret));
955+
return ret;
955956
}
956957
if (cJSON_IsString(json_value) &&
957958
(json_value->valuestring != NULL && (strlen(json_value->valuestring) == NUM_TRYTES_HASH))) {
958959
chid = (char*)malloc(sizeof(char) * (NUM_TRYTES_HASH + 1));
959960
strncpy(chid, json_value->valuestring, sizeof(char) * (NUM_TRYTES_HASH + 1));
960961
} else {
961-
ta_log_error("%s\n", ta_error_to_string(SC_CCLIENT_JSON_PARSE));
962-
return SC_CCLIENT_JSON_PARSE;
962+
ret = SC_CCLIENT_JSON_PARSE;
963+
ta_log_error("%s\n", ta_error_to_string(ret));
964+
return ret;
963965
}
964966
}
965967

966968
if (cJSON_HasObjectItem(json_key, "msg_id")) {
967969
json_value = cJSON_GetObjectItemCaseSensitive(json_key, "msg_id");
968970
if (json_value == NULL) {
969-
ta_log_error("%s\n", ta_error_to_string(SC_CCLIENT_JSON_KEY));
970-
return SC_CCLIENT_JSON_KEY;
971+
ret = SC_CCLIENT_JSON_KEY;
972+
ta_log_error("%s\n", ta_error_to_string(ret));
973+
return ret;
971974
}
972975
if (cJSON_IsString(json_value) && (json_value->valuestring != NULL) &&
973976
(strlen(json_value->valuestring) == NUM_TRYTES_MAM_MSG_ID)) {
974977
msg_id = (char*)malloc(sizeof(char) * (NUM_TRYTES_MAM_MSG_ID + 1));
975978
strncpy(msg_id, json_value->valuestring, sizeof(char) * (NUM_TRYTES_MAM_MSG_ID + 1));
976979
} else {
977-
ta_log_error("%s\n", ta_error_to_string(SC_CCLIENT_JSON_PARSE));
978-
return SC_CCLIENT_JSON_PARSE;
980+
ret = SC_CCLIENT_JSON_PARSE;
981+
ta_log_error("%s\n", ta_error_to_string(ret));
982+
return ret;
979983
}
980984
}
981985

accelerator/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ int main(int argc, char* argv[]) {
9696
if (quiet_mode) {
9797
// Destroy logger when quiet mode is on
9898
logger_helper_release(logger_id);
99-
logger_helper_destroy();
99+
if (logger_helper_destroy() != RC_OK) {
100+
ta_log_error("Destroying logger failed %s.\n", MAIN_LOGGER);
101+
return EXIT_FAILURE;
102+
}
100103
} else {
101104
http_logger_init();
102105
apis_logger_init();

accelerator/mqtt_main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ int main(int argc, char *argv[]) {
4646
if (quiet_mode) {
4747
// Destroy logger when quiet mode is on
4848
logger_helper_release(logger_id);
49-
logger_helper_destroy();
49+
if (logger_helper_destroy() != RC_OK) {
50+
ta_log_error("Destroying logger failed %s.\n", CONN_MQTT_LOGGER);
51+
return EXIT_FAILURE;
52+
}
5053
} else {
5154
mqtt_utils_logger_init();
5255
mqtt_common_logger_init();

connectivity/mqtt/duplex_callback.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static status_t mqtt_request_handler(mosq_config_t *cfg, char *subscribe_topic,
5353

5454
status_t ret = SC_OK;
5555
char *json_result = NULL;
56+
char *res_topic = NULL;
5657
char device_id[ID_LEN] = {0};
5758

5859
// get the Device ID.
@@ -110,7 +111,7 @@ static status_t mqtt_request_handler(mosq_config_t *cfg, char *subscribe_topic,
110111

111112
// Set response publishing topic with the topic we got message and the Device ID (client ID) we got in the message
112113
int res_topic_len = strlen(subscribe_topic) + 1 + ID_LEN + 1;
113-
char *res_topic = (char *)malloc(res_topic_len);
114+
res_topic = (char *)malloc(res_topic_len);
114115
snprintf(res_topic, res_topic_len, "%s/%s", subscribe_topic, device_id);
115116
ret = gossip_channel_set(cfg, NULL, NULL, res_topic);
116117
if (ret != SC_OK) {
@@ -126,6 +127,7 @@ static status_t mqtt_request_handler(mosq_config_t *cfg, char *subscribe_topic,
126127

127128
done:
128129
free(json_result);
130+
free(res_topic);
129131
return ret;
130132
}
131133

connectivity/mqtt/mqtt_common.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ status_t cfg_add_topic(mosq_config_t *cfg, client_type_t client_type, char *topi
198198

199199
if (client_type == client_pub || client_type == client_duplex) {
200200
cfg->pub_config->topic = strdup(topic);
201-
} else if (client_type == client_duplex) {
202-
cfg->pub_config->response_topic = strdup(topic);
201+
if (client_type == client_duplex) {
202+
cfg->pub_config->response_topic = strdup(topic);
203+
}
203204
} else {
204205
cfg->sub_config->topic_count++;
205206
cfg->sub_config->topics = realloc(cfg->sub_config->topics, cfg->sub_config->topic_count * sizeof(char *));

storage/scylladb_identity.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ status_t db_set_identity_hash(db_identity_t* obj, const cass_byte_t* hash, size_
135135
}
136136
if (hash == NULL) {
137137
ta_log_error("NULL pointer to hash to insert into identity table\n");
138+
return SC_TA_NULL;
138139
}
139140
if (length != DB_NUM_TRYTES_HASH) {
140-
ta_log_error("SC_STORAGE_INVALID_INPUT\n");
141+
ta_log_error("%s\n", ta_error_to_string(SC_STORAGE_INVALID_INPUT));
141142
return SC_STORAGE_INVALID_INPUT;
142143
}
143144
memcpy(obj->hash, hash, DB_NUM_TRYTES_HASH);

storage/scylladb_identity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ db_identity_array_t* db_identity_array_new();
5151
static inline void db_identity_array_free(db_identity_array_t** const db_identity_array) {
5252
if (db_identity_array && *db_identity_array) {
5353
utarray_free(*db_identity_array);
54+
*db_identity_array = NULL;
5455
}
55-
*db_identity_array = NULL;
5656
}
5757

5858
/**

utils/bundle_array.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@ extern "C" {
2424
*/
2525

2626
typedef UT_array bundle_array_t;
27+
// We should synchronize this implementation as to the implementation in the IOTA library
28+
static UT_icd bundle_transactions_icd = {sizeof(iota_transaction_t), 0, 0, 0};
29+
30+
static inline void bundle_array_copy(void *_dst, const void *_src) {
31+
bundle_transactions_t *bdl_dst = (bundle_transactions_t *)_dst;
32+
bundle_transactions_t *bdl_src = (bundle_transactions_t *)_src;
33+
iota_transaction_t *txn = NULL;
34+
utarray_init(bdl_dst, &bundle_transactions_icd);
35+
BUNDLE_FOREACH(bdl_src, txn) { bundle_transactions_add(bdl_dst, txn); }
36+
}
2737

28-
static UT_icd bundle_array_icd = {sizeof(bundle_transactions_t), 0, 0, 0};
38+
static UT_icd bundle_array_icd = {sizeof(bundle_transactions_t), 0, bundle_array_copy, 0};
2939

3040
/**
3141
* Allocate memory of bundle_array_t

0 commit comments

Comments
 (0)