From fc8deae5dfa14df550a585af28db788f6ea6a47f Mon Sep 17 00:00:00 2001 From: Jihoon Kim Date: Tue, 31 May 2022 20:11:45 +0900 Subject: [PATCH] Prevent crash when failing to get request_id in bundle Change-Id: I5f3c13ef97a84a807a38c16a5f239f3059f1a708 Signed-off-by: Jihoon Kim --- client/nlp_client.c | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/client/nlp_client.c b/client/nlp_client.c index 3c69bea..c22ee72 100644 --- a/client/nlp_client.c +++ b/client/nlp_client.c @@ -42,7 +42,7 @@ typedef struct { const char *tag; } pos_tag_s; -static char *get_request_id() +static char *generate_request_id() { _request_id++; char buffer [sizeof(unsigned int)*8+1]; @@ -51,11 +51,22 @@ static char *get_request_id() return strdup(buffer); } -static void process_pos_tag_result(nlp_h nh, bundle *msg) +unsigned int get_request_id(bundle *msg) { + unsigned int request_id_num = 0; char *request_id = NULL; - bundle_get_str(msg, "request_id", &request_id); + LOGD("id : %s", request_id); + + if (request_id) + request_id_num = (unsigned int)atoi(request_id); + + return request_id_num; +} + +static void process_pos_tag_result(nlp_h nh, bundle *msg) +{ + unsigned int request_id_num = get_request_id(msg); const char **tag_array = NULL; const char **token_array = NULL; @@ -85,7 +96,7 @@ static void process_pos_tag_result(nlp_h nh, bundle *msg) pos_tag_result_h = glist; if (nh->pos_tag_result_cb) - nh->pos_tag_result_cb((unsigned int)atoi(request_id), pos_tag_result_h, nh->pos_tag_result_data); + nh->pos_tag_result_cb(request_id_num, pos_tag_result_h, nh->pos_tag_result_data); GList* iter = NULL; @@ -108,10 +119,7 @@ static void process_pos_tag_result(nlp_h nh, bundle *msg) static void process_word_tokenize_result(nlp_h nh, bundle *msg) { - char *request_id = NULL; - - bundle_get_str(msg, "request_id", &request_id); - LOGD("id : %s", request_id); + unsigned int request_id_num = get_request_id(msg); const char **word_array = NULL; int len_word_array = 0; @@ -130,7 +138,7 @@ static void process_word_tokenize_result(nlp_h nh, bundle *msg) word_tokenize_result_h = glist; if (nh->word_tokenize_result_cb) - nh->word_tokenize_result_cb((unsigned int)atoi(request_id), word_tokenize_result_h, nh->word_tokenize_result_data); + nh->word_tokenize_result_cb(request_id_num, word_tokenize_result_h, nh->word_tokenize_result_data); if (g_list_length(glist) > 0) { g_list_free(glist); @@ -140,10 +148,7 @@ static void process_word_tokenize_result(nlp_h nh, bundle *msg) static void process_language_detect_result(nlp_h nh, bundle *msg) { - char *request_id = NULL; - - bundle_get_str(msg, "request_id", &request_id); - LOGD("id : %s", request_id); + unsigned int request_id_num = get_request_id(msg); const char **language_array = NULL; int len_language_array = 0; @@ -158,7 +163,7 @@ static void process_language_detect_result(nlp_h nh, bundle *msg) } if (nh->language_detect_result_cb) - nh->language_detect_result_cb((unsigned int)atoi(request_id), detect_language, nh->language_detect_result_data); + nh->language_detect_result_cb(request_id_num, detect_language, nh->language_detect_result_data); } //LCOV_EXCL_START @@ -166,13 +171,11 @@ static void _notify_cb(void *user_data, const char *sender, bundle *msg) { nlp_h nh = user_data; LOGD("sender: %s", sender); - char *request_id = NULL; char *command = NULL; - bundle_get_str(msg, "request_id", &request_id); bundle_get_str(msg, "command", &command); - LOGD("id : %s, command : %s", request_id, command); + LOGD("command : %s", command); if (!command) return; @@ -357,7 +360,7 @@ int send_request(nlp_h nh, const char *command, const char *str, unsigned int *r return NLP_ERROR_OPERATION_FAILED; } - char *req_id = get_request_id(); + char *req_id = generate_request_id(); bundle *msg = bundle_create(); bundle_add_str(msg, "command", command); -- 2.34.1