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

Commit 1ae60d4

Browse files
committed
fix(mam): Move definition to avoid SEGV fault
Define `mam_encrypt_key_t` object in the middle of the function would cause SEGV when the `mam_encrypt_key_t` object being freed. And using `ta_error_to_string` to generate the string which is going to be printed in logger. For #706
1 parent 3dfc37a commit 1ae60d4

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

accelerator/core/mam_core.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,19 @@ static status_t ta_mam_init(mam_api_t *const api, const iota_config_t *const ico
258258

259259
if (seed) {
260260
if (mam_api_init(api, seed) != RC_OK) {
261-
ta_log_error("%s\n", "SC_MAM_FAILED_INIT");
261+
ta_log_error("%s\n", ta_error_to_string(SC_MAM_FAILED_INIT));
262262
return SC_MAM_FAILED_INIT;
263263
}
264264
} else {
265265
// Use local MAM file on the current machine
266266
retcode_t rc = mam_api_load(iconf->mam_file_path, api, NULL, 0);
267267
if (rc == RC_UTILS_FAILED_TO_OPEN_FILE) {
268268
if (mam_api_init(api, (tryte_t *)iconf->seed) != RC_OK) {
269-
ta_log_error("%s\n", "SC_MAM_FAILED_INIT");
269+
ta_log_error("%s\n", ta_error_to_string(SC_MAM_FAILED_INIT));
270270
return SC_MAM_FAILED_INIT;
271271
}
272272
} else if (rc != RC_OK) {
273-
ta_log_error("%s\n", "SC_MAM_FAILED_INIT");
273+
ta_log_error("%s\n", ta_error_to_string(SC_MAM_FAILED_INIT));
274274
return SC_MAM_FAILED_INIT;
275275
}
276276
}
@@ -295,7 +295,7 @@ static status_t create_channel_fetch_all_transactions(const iota_client_service_
295295
if (!first_iter) {
296296
if (mam_api_channel_create(api, channel_depth, chid) != RC_OK) {
297297
ret = SC_MAM_FAILED_CREATE_OR_GET_ID;
298-
ta_log_error("%s\n", "SC_MAM_FAILED_CREATE_OR_GET_ID");
298+
ta_log_error("%s\n", ta_error_to_string(ret));
299299
goto done;
300300
}
301301
}
@@ -306,8 +306,8 @@ static status_t create_channel_fetch_all_transactions(const iota_client_service_
306306
// TODO use `ta_find_transaction_objects(service, obj_req, obj_res)` instead of the original 'iota.c' function
307307
retcode_t ret_rc = iota_client_find_transaction_objects(service, txn_req, obj_res);
308308
if (ret_rc && ret_rc != RC_NULL_PARAM) {
309-
ret = SC_MAM_FAILED_DESTROYED;
310-
ta_log_error("%s\n", "SC_MAM_FAILED_DESTROYED");
309+
ret = SC_MAM_FAILED_RESPONSE;
310+
ta_log_error("%s\n", ta_error_to_string(ret));
311311
goto done;
312312
}
313313

@@ -482,7 +482,7 @@ static status_t ta_mam_written_msg_to_bundle(const iota_client_service_t *const
482482
// Writing packet to bundle
483483
if (ta_mam_write_packet(api, payload, msg_id_trits, *bundle)) {
484484
ret = SC_MAM_FAILED_WRITE;
485-
ta_log_error("%s\n", "SC_MAM_FAILED_WRITE");
485+
ta_log_error("%s\n", ta_error_to_string(ret));
486486
goto done;
487487
}
488488

@@ -516,13 +516,13 @@ static status_t ta_mam_write_announce_to_bundle(mam_api_t *const api, const size
516516
trit_t msg_id[MAM_MSG_ID_SIZE];
517517
if (mam_api_channel_create(api, channel_depth, chid1) != RC_OK) {
518518
ret = SC_MAM_FAILED_CREATE_OR_GET_ID;
519-
ta_log_error("%s\n", "SC_MAM_FAILED_CREATE_OR_GET_ID");
519+
ta_log_error("%s\n", ta_error_to_string(ret));
520520
goto done;
521521
}
522522

523523
if (mam_api_bundle_announce_channel(api, chid, chid1, mam_key.psks, mam_key.ntru_pks, *bundle, msg_id) != RC_OK) {
524524
ret = SC_MAM_FAILED_WRITE_HEADER;
525-
ta_log_error("%s\n", "SC_MAM_FAILED_WRITE_HEADER");
525+
ta_log_error("%s\n", ta_error_to_string(ret));
526526
goto done;
527527
}
528528

@@ -680,6 +680,7 @@ status_t ta_recv_mam_message(const iota_config_t *const iconf, const iota_client
680680
bundle_array_t *bundle_array = NULL;
681681
bundle_array_new(&bundle_array);
682682
mam_pk_t_set_t init_trusted_ch = NULL;
683+
mam_encrypt_key_t mam_key = {.psks = NULL, .ntru_pks = NULL, .ntru_sks = NULL};
683684
recv_mam_data_id_mam_v1_t *data_id = (recv_mam_data_id_mam_v1_t *)req->data_id;
684685
recv_mam_key_mam_v1_t *key = (recv_mam_key_mam_v1_t *)req->key;
685686
if (mam_api_init(&mam, (tryte_t *)iconf->seed) != RC_OK) {
@@ -716,7 +717,6 @@ status_t ta_recv_mam_message(const iota_config_t *const iconf, const iota_client
716717
}
717718

718719
// Add decryption keys
719-
mam_encrypt_key_t mam_key = {.psks = NULL, .ntru_pks = NULL, .ntru_sks = NULL};
720720
ret = ta_set_mam_key(&mam_key, key->psk_array, NULL, key->ntru_array);
721721
if (ret != SC_OK) {
722722
ta_log_error("%s\n", ta_error_to_string(ret));

0 commit comments

Comments
 (0)