Fix svace issue of NLP 86/187886/1
authorjingjin.geng <jingjin.geng@samsung.com>
Wed, 29 Aug 2018 23:31:36 +0000 (07:31 +0800)
committerjingjin.geng <jingjin.geng@samsung.com>
Wed, 29 Aug 2018 23:31:36 +0000 (07:31 +0800)
memory leak

Change-Id: I3dfa0f791b2fa77405c474c9bd21c5ba435b961f

service/src/service.c

index 9617470..0c6cefc 100755 (executable)
@@ -112,6 +112,7 @@ static int __message_send(rpc_port_stub_message_context_h context,
     bundle *reply = bundle_create();
     char *info = NULL;
     char *request_id = NULL;
+    char *tmp_str = NULL;
     PyObject* args = NULL;
     PyObject* lists = NULL;
     unsigned static int len = 0;
@@ -166,7 +167,12 @@ static int __message_send(rpc_port_stub_message_context_h context,
                 if(tokens[i]!=NULL)
                 {
                     memset(tokens[i], 0, BUF_LEN_128);
-                    strncpy(tokens[i], nltk_get_string_from_element(nltk_get_element_from_list_by_index(lists, i)), BUF_LEN_128-1);
+                    tmp_str = nltk_get_string_from_element(nltk_get_element_from_list_by_index(lists, i));
+                    if(tmp_str!=NULL)
+                    {
+                        strncpy(tokens[i], tmp_str, BUF_LEN_128-1);
+                        free(tmp_str);
+                    }
                 }
                 else
                 {
@@ -176,6 +182,10 @@ static int __message_send(rpc_port_stub_message_context_h context,
             bundle_add_str(reply, "command", "word_tokenize");
             bundle_add_str_array(reply, "return_token", tokens, len);
             PINFO("word_tokenize process done");
+            for(int j = 0 ;j < len ;j++)
+            {
+                free(tokens[j]);
+            }
             break;
         case NLTK_CMD_POSTAG:
             args = nltk_make_args_from_string(info);
@@ -194,7 +204,12 @@ static int __message_send(rpc_port_stub_message_context_h context,
                 if(tag[i]!=NULL)
                 {
                     memset(tag[i], 0, BUF_LEN_128);
-                    strncpy(tag[i], nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0)), BUF_LEN_128-1);
+                    tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0));
+                    if(tmp_str!=NULL)
+                    {
+                        strncpy(tag[i], tmp_str, BUF_LEN_128-1);
+                        free(tmp_str);
+                    }
                 }
                 else
                 {
@@ -203,7 +218,12 @@ static int __message_send(rpc_port_stub_message_context_h context,
                 if(token[i]!=NULL)
                 {
                     memset(token[i], 0, BUF_LEN_128);
-                    strncpy(token[i], nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1)), BUF_LEN_128-1);
+                    tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1));
+                    if(tmp_str!=NULL)
+                    {
+                        strncpy(token[i], tmp_str, BUF_LEN_128-1);
+                        free(tmp_str);
+                    }
                 }
                 else
                 {
@@ -213,6 +233,11 @@ static int __message_send(rpc_port_stub_message_context_h context,
             bundle_add_str(reply, "command", "pos_tag");
             bundle_add_str_array(reply, "return_tag", tag, len);
             bundle_add_str_array(reply, "return_token", token, len);
+            for(int j = 0 ;j < len ;j++)
+            {
+                free(tag[j]);
+                free(token[j]);
+            }
             PINFO("pos_tag process done");
             break;
         case NLTK_CMD_NECHUNK:
@@ -237,7 +262,12 @@ static int __message_send(rpc_port_stub_message_context_h context,
                 if(s_tag[i]!=NULL)
                 {
                     memset(s_tag[i], 0, BUF_LEN_128);
-                    strncpy(s_tag[i], nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0)), BUF_LEN_128-1);
+                    tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0));
+                    if(tmp_str!=NULL)
+                    {
+                        strncpy(s_tag[i], tmp_str, BUF_LEN_128-1);
+                        free(tmp_str);
+                    }
                 }
                 else
                 {
@@ -246,7 +276,12 @@ static int __message_send(rpc_port_stub_message_context_h context,
                 if(s_token[i]!=NULL)
                 {
                     memset(s_token[i], 0, BUF_LEN_128);
-                    strncpy(s_token[i], nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1)), BUF_LEN_128-1);
+                    tmp_str = nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1));
+                    if(tmp_str!=NULL)
+                    {
+                        strncpy(s_token[i], tmp_str, BUF_LEN_128-1);
+                        free(tmp_str);
+                    }
                 }
                 else
                 {
@@ -257,6 +292,11 @@ static int __message_send(rpc_port_stub_message_context_h context,
             bundle_add_str_array(reply, "return_tag", s_tag, len);
             bundle_add_str_array(reply, "return_token", s_token, len);
             PINFO("ne_chunk process done");
+            for(int j = 0 ;j < len ;j++)
+            {
+                free(s_tag[j]);
+                free(s_token[j]);
+            }
             break;
         case NLTK_CMD_LEMMATIZE:
             args = nltk_make_args_from_string(info);
@@ -269,10 +309,16 @@ static int __message_send(rpc_port_stub_message_context_h context,
             if(lem_buf[0]!=NULL)
             {
                 memset(lem_buf[0], 0, BUF_LEN_128);
-                strncpy(lem_buf[0], nltk_get_string_from_element(lm_result), BUF_LEN_128-1);
+                tmp_str = nltk_get_string_from_element(lm_result);
+                if(tmp_str!=NULL)
+                {
+                    strncpy(lem_buf[0], tmp_str, BUF_LEN_128-1);
+                    free(tmp_str);
+                }
                 bundle_add_str(reply, "command", "lemmatize");
                 bundle_add_str_array(reply, "return_token", lem_buf, 1);
                 PINFO("lemmatize process done");
+                free(lem_buf[0]);
             }
             else
             {
@@ -288,10 +334,16 @@ static int __message_send(rpc_port_stub_message_context_h context,
             if(lang_buf[0]!=NULL)
             {
                 memset(lang_buf[0], 0, BUF_LEN_128);
-                strncpy(lang_buf[0], nltk_get_string_from_element(ld_result), BUF_LEN_128-1);
+                tmp_str = nltk_get_string_from_element(ld_result);
+                if(tmp_str!=NULL)
+                {
+                    strncpy(lang_buf[0], tmp_str, BUF_LEN_128-1);
+                    free(tmp_str);
+                }
                 bundle_add_str(reply, "command", "langdetect");
                 bundle_add_str_array(reply, "return_token", lang_buf, 1);
                 PINFO("langdetect process done");
+                free(lang_buf[0]);
             }
             else
             {