Fix issue detected by static analysis tool 21/275721/2
authorJihoon Kim <jihoon48.kim@samsung.com>
Tue, 31 May 2022 02:15:17 +0000 (11:15 +0900)
committerJihoon Kim <jihoon48.kim@samsung.com>
Tue, 31 May 2022 02:34:41 +0000 (11:34 +0900)
Change-Id: I61e701ba02e81a3b87f8a0cece47be07ffea2071
Signed-off-by: Jihoon Kim <jihoon48.kim@samsung.com>
client/nlp_client.c

index 8f73486c71561b16d2a3dadf59dbf09888204846..c6ef7bb354475515f30ef08979788156d04d37d9 100644 (file)
@@ -68,16 +68,18 @@ static void process_pos_tag_result(nlp_h nh, bundle *msg)
     tag_array = bundle_get_str_array(msg, "return_tag", &len_tag_array);
     token_array = bundle_get_str_array(msg, "return_token", &len_token_array);
 
-    for (int i=0; i < len_tag_array; i++) {
-        pos_tag_pair = (pos_tag_s* )calloc(1, sizeof(pos_tag_s));
-        if (pos_tag_pair) {
-            pos_tag_pair->token = token_array[i];
-            pos_tag_pair->tag = tag_array[i];
-        }
+    if (tag_array && token_array) {
+        for (int i=0; i < len_tag_array; i++) {
+            pos_tag_pair = (pos_tag_s* )calloc(1, sizeof(pos_tag_s));
+            if (pos_tag_pair) {
+                pos_tag_pair->token = token_array[i];
+                pos_tag_pair->tag = tag_array[i];
+            }
 
-        LOGD("tag: %s, token : %s", tag_array[i], token_array[i]);
+            LOGD("tag: %s, token : %s", tag_array[i], token_array[i]);
 
-        glist = g_list_append(glist, pos_tag_pair);
+            glist = g_list_append(glist, pos_tag_pair);
+        }
     }
 
     pos_tag_result_h = glist;
@@ -100,15 +102,22 @@ static void process_word_tokenize_result(nlp_h nh, bundle *msg)
 
     word_array = bundle_get_str_array(msg, "return_token", &len_word_array);
 
-    for (int i=0; i < len_word_array; i++) {
-        LOGD("word : %s", word_array[i]);
-        glist = g_list_append(glist, (void *)word_array[i]);
+    if (word_array) {
+        for (int i=0; i < len_word_array; i++) {
+            LOGD("word : %s", word_array[i]);
+            glist = g_list_append(glist, (void *)word_array[i]);
+        }
     }
 
     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);
+
+    if (g_list_length(glist) > 0) {
+        g_list_free(glist);
+        glist = NULL;
+    }
 }
 
 static void process_language_detect_result(nlp_h nh, bundle *msg)
@@ -118,22 +127,20 @@ static void process_language_detect_result(nlp_h nh, bundle *msg)
     bundle_get_str(msg, "request_id", &request_id);
     LOGD("id : %s", request_id);
 
-    const char **word_array = NULL;
-    int len_word_array = 0;
-    //GList *glist = NULL;
-    //nlp_word_tokenize_result_h word_tokenize_result_h = NULL;
-
-    word_array = bundle_get_str_array(msg, "return_token", &len_word_array);
+    const char **language_array = NULL;
+    int len_language_array = 0;
+    const char *detect_language = NULL;
+    language_array = bundle_get_str_array(msg, "return_token", &len_language_array);
+    if (language_array) {
+        for (int i=0; i < len_language_array; i++) {
+            LOGD("language : %s", language_array[i]);
+        }
 
-    for (int i=0; i < len_word_array; i++) {
-        LOGD("language : %s", word_array[i]);
-        //glist = g_list_append(glist, (void *)word_array[i]);
+        detect_language = language_array[0];
     }
 
-    //word_tokenize_result_h = glist;
-
     if (nh->language_detect_result_cb)
-        nh->language_detect_result_cb((unsigned int)atoi(request_id), word_array[0], nh->language_detect_result_data);
+        nh->language_detect_result_cb((unsigned int)atoi(request_id), detect_language, nh->language_detect_result_data);
 }
 
 //LCOV_EXCL_START
@@ -472,15 +479,12 @@ EXPORT_API int nlp_foreach_word_tokenize(nlp_word_tokenize_result_h word_tokeniz
             /* next item */
             iter = g_list_next(iter);
         }
-
-        g_list_free(glist);
-        glist = NULL;
     }
 
     return NLP_ERROR_NONE;
 }
 
-EXPORT_API int nlp_request_language_detect(nlp_h nh, const char *str, unsigned int *request_id)
+ EXPORT_API int nlp_request_language_detect(nlp_h nh, const char *str, unsigned int *request_id)
 {
     return send_request(nh, "langdetect", str, request_id);
 }