Fix svace issue of NLP 00/187700/11
authorjingjin.geng <jingjin.geng@samsung.com>
Tue, 28 Aug 2018 19:50:07 +0000 (03:50 +0800)
committerjingjin.geng <jingjin.geng@samsung.com>
Wed, 29 Aug 2018 17:24:17 +0000 (01:24 +0800)
Use strncpy instead of strcpy

Change-Id: I5b7a34fcf9d346778d89d774eed1fab6051527a2

service/inc/service.h [changed mode: 0644->0755]
service/src/service.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 3af6cb7..c3cb796
@@ -3,6 +3,9 @@
 
 #include <Python.h>
 
+#define BUF_LEN_128  128
+#define BUF_LEN_256  256
+
 typedef enum {
     NLTK_CMD_NONE = -1,
     NLTK_CMD_TOKENIZE,
old mode 100644 (file)
new mode 100755 (executable)
index 7d069f6..9617470
@@ -159,11 +159,19 @@ static int __message_send(rpc_port_stub_message_context_h context,
             args = nltk_make_args_from_string(info);
             lists = nltk_call_function_with_args(nltk_get_function_handle(globe_nltk,"word_tokenize"), args);
             len = nltk_get_size_from_list(lists);
-            char *tokens[128] = {NULL,};
+            char *tokens[BUF_LEN_128] = {NULL,};
             for(int i = 0 ;i < len ;i++)
             {
-                tokens[i] = (char*)malloc(128*sizeof(char));
-                strcpy(tokens[i]  ,nltk_get_string_from_element(nltk_get_element_from_list_by_index(lists, i)));
+                tokens[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
+                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);
+                }
+                else
+                {
+                    PERR("malloc failed");
+                }
             }
             bundle_add_str(reply, "command", "word_tokenize");
             bundle_add_str_array(reply, "return_token", tokens, len);
@@ -176,15 +184,31 @@ static int __message_send(rpc_port_stub_message_context_h context,
             args = nltk_make_args_from_pyobject(lists);
             postag_result = nltk_call_function_with_args(postag_module, args);
             len = nltk_get_size_from_list(postag_result);
-            char *tag[128] = {NULL,};
-            char *token[128] = {NULL,};
+            char *tag[BUF_LEN_128] = {NULL,};
+            char *token[BUF_LEN_128] = {NULL,};
             for(int i = 0 ;i < len ;i++)
             {
-                token[i] = (char*)malloc(128*sizeof(char));
-                tag[i] = (char*)malloc(128*sizeof(char));
+                token[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
+                tag[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
                 pos_elm_tuple = nltk_get_element_from_list_by_index(postag_result, i);
-                strcpy(tag[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0)));
-                strcpy(token[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1)));
+                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);
+                }
+                else
+                {
+                    PERR("malloc failed");
+                }
+                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);
+                }
+                else
+                {
+                    PERR("malloc failed");
+                }
             }
             bundle_add_str(reply, "command", "pos_tag");
             bundle_add_str_array(reply, "return_tag", tag, len);
@@ -203,15 +227,31 @@ static int __message_send(rpc_port_stub_message_context_h context,
             lv_module = nltk_get_function_handle(ne_result, "leaves");
             lv_result = nltk_call_function_with_args(lv_module, NULL);
             len = nltk_get_size_from_list(lv_result);
-            char *s_tag[128] = {NULL,};
-            char *s_token[128] = {NULL,};
+            char *s_tag[BUF_LEN_128] = {NULL,};
+            char *s_token[BUF_LEN_128] = {NULL,};
             for(int i = 0 ;i < len ;i++)
             {
-                s_token[i] = (char*)malloc(128*sizeof(char));
-                s_tag[i] = (char*)malloc(128*sizeof(char));
+                s_token[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
+                s_tag[i] = (char*)malloc(BUF_LEN_128*sizeof(char));
                 pos_elm_tuple = nltk_get_element_from_list_by_index(lv_result, i);
-                strcpy(s_tag[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 0)));
-                strcpy(s_token[i] , nltk_get_string_from_element(nltk_get_element_from_tuple_by_index(pos_elm_tuple, 1)));
+                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);
+                }
+                else
+                {
+                    PERR("malloc failed");
+                }
+                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);
+                }
+                else
+                {
+                    PERR("malloc failed");
+                }
             }
             bundle_add_str(reply, "command", "ne_chunk");
             bundle_add_str_array(reply, "return_tag", s_tag, len);
@@ -225,22 +265,38 @@ static int __message_send(rpc_port_stub_message_context_h context,
             lm_module = nltk_get_function_handle(lm_object, "lemmatize");
             lm_result = nltk_call_function_with_args(lm_module, args);
             char *lem_buf[1] = {NULL,};
-            lem_buf[0] = (char*)malloc(128*sizeof(char));
-            strcpy(lem_buf[0] , nltk_get_string_from_element(lm_result));
-            bundle_add_str(reply, "command", "lemmatize");
-            bundle_add_str_array(reply, "return_token", lem_buf, 1);
-            PINFO("lemmatize process done");
+            lem_buf[0] = (char*)malloc(BUF_LEN_128*sizeof(char));
+            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);
+                bundle_add_str(reply, "command", "lemmatize");
+                bundle_add_str_array(reply, "return_token", lem_buf, 1);
+                PINFO("lemmatize process done");
+            }
+            else
+            {
+                PERR("malloc failed");
+            }
             break;
         case NLTK_CMD_LANGDETECT:
             args = nltk_make_args_from_string(info);
             ld_module = nltk_get_function_handle(globe_lang,"detect");
             ld_result = nltk_call_function_with_args(ld_module, args);
             char *lang_buf[1] = {NULL,};
-            lang_buf[0] = (char*)malloc(128*sizeof(char));
-            strcpy(lang_buf[0] , nltk_get_string_from_element(ld_result));
-            bundle_add_str(reply, "command", "langdetect");
-            bundle_add_str_array(reply, "return_token", lang_buf, 1);
-            PINFO("langdetect process done");
+            lang_buf[0] = (char*)malloc(BUF_LEN_128*sizeof(char));
+            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);
+                bundle_add_str(reply, "command", "langdetect");
+                bundle_add_str_array(reply, "return_token", lang_buf, 1);
+                PINFO("langdetect process done");
+            }
+            else
+            {
+                PERR("malloc failed");
+            }
             break;
         default:
             PWARNING("unkonw command, command = [%s]", message);
@@ -440,8 +496,14 @@ PyObject* nltk_get_element_from_tuple_by_index(PyObject* tuple, int index)
 char* nltk_get_string_from_element(PyObject* elm)
 {
     PRET_VM(!elm, NULL, "Input parameter [elm] is NULL!");
-    char* ch = (char*) malloc(255);
-    strcpy(ch, PyString_AsString(elm));
+    char* ch = (char*) malloc(BUF_LEN_256);
+    if(ch == NULL)
+    {
+        PERR("malloc failed");
+        return ch;
+    }
+    memset(ch, 0, BUF_LEN_256);
+    strncpy(ch, PyString_AsString(elm), BUF_LEN_256-1);
     return ch;
 }
 
@@ -484,9 +546,10 @@ char* nltk_get_attribute(int z)
 {
     PyObject *pModule, *pFunc;
     PyObject *pArgs;
-    char* ch = (char*) malloc(255);
+    char* ch = (char*) malloc(BUF_LEN_256);
     if (ch != NULL)
     {
+        memset(ch, 0, BUF_LEN_256);
         pModule = PyImport_ImportModule("site");
         if (pModule != NULL)
         {
@@ -497,17 +560,18 @@ char* nltk_get_attribute(int z)
         //call word_tokenize func with args
         pFunc = PyObject_GetAttrString(pModule, "USER_SITE");
         if (pFunc != NULL) {
-            strcpy(ch, PyString_AsString(pFunc));
+            strncpy(ch, PyString_AsString(pFunc), BUF_LEN_256-1);
             } else {
-            strcpy(ch, "attribute get error\n");
+            strncpy(ch, "attribute get error\n", BUF_LEN_256-1);
             }
         } else {
-            strcpy(ch, "nltk module import error\n");
+            strncpy(ch, "nltk module import error\n", BUF_LEN_256-1);
         }
         return ch;
     }
     else
     {
+        PERR("malloc failed");
         return NULL;
     }
 }