UIHV: Fix data_buf initialization 28/103528/2
authorAlexander Aksenov <a.aksenov@samsung.com>
Wed, 7 Dec 2016 16:25:27 +0000 (19:25 +0300)
committerDmitry Kovalenko <d.kovalenko@samsung.com>
Fri, 9 Dec 2016 12:19:38 +0000 (04:19 -0800)
Previously data_buf was NULL for log.length > 0 &&
log.length < sizeof(log.data)

Svace issue.

Change-Id: I74004cef90c2c7b05397af3b7256292a07928e85
Signed-off-by: Alexander Aksenov <a.aksenov@samsung.com>
ui_viewer/ui_viewer_lib.c

index 5ebfbbd..280c398 100644 (file)
@@ -316,6 +316,33 @@ free_data_buf:
        return ret;
 }
 
+/* Allocate memory and gets payload for message */
+static char *get_msg_payload(log_t *log)
+{
+       char *data_buf = NULL;
+       ssize_t recvlen;
+
+       if (log->length <= 0)
+               return NULL;
+
+       data_buf = malloc(log->length);
+       if (data_buf == NULL) {
+               PRINTERR("Cannot allocate buffer for message. Size %d",
+                        log->length);
+               return NULL;
+       }
+
+       recvlen = recv(gTraceInfo.socket.daemonSock, data_buf, log->length,
+                      MSG_WAITALL);
+       if (recvlen != log->length) {
+               PRINTERR("Cannot receive data!");
+               free(data_buf);
+               data_buf = NULL;
+       }
+
+       return data_buf;
+}
+
 static void *recvThread(void __unused *data)
 {
        fd_set readfds, workfds;
@@ -373,36 +400,21 @@ static void *recvThread(void __unused *data)
 
                        if(recvlen > 0) // recv succeed
                        {
-
-                               if (log.length > 0 &&
-                                   (unsigned int)log.length <= sizeof(log.data)) {
-                                       data_buf = malloc(log.length);
-                                       if (data_buf == NULL) {
-                                               PRINTERR("cannot allocate buf to recv msg");
-                                               break;
-                                       }
-                                       recvlen = recv(gTraceInfo.socket.daemonSock, data_buf,
-                                               log.length, MSG_WAITALL);
-                                       if (recvlen != log.length) {
-                                               PRINTERR("Can not recv data from\n");
+                               if (log.type == APP_MSG_CONFIG) {
+                                       data_buf = get_msg_payload(&log);
+                                       if (data_buf != NULL) {
+                                               _configure(data_buf);
                                                goto free_data_buf;
                                        }
-                               }
-
-                               if (log.type == APP_MSG_CONFIG) {
-                                       _configure(data_buf);
                                } else if(log.type == APP_MSG_STOP) {
                                        PRINTMSG("APP_MSG_STOP");
-                                       if (data_buf) {
-                                               free(data_buf);
-                                               data_buf = NULL;
-                                       }
                                        application_exit();
                                        break;
                                } else if(log.type == APP_MSG_TYPE_AND_INFO) {
                                        PRINTMSG("APP_MSG_TYPE_AND_INFO");
                                } else if(log.type == APP_MSG_GET_UI_HIERARCHY) {
                                        enum ErrorCode err_code = ERR_UNKNOWN;
+                                       Eina_Bool rendering;
 
                                        print_log_ui_viewer_hierarchy_status(&err_code);
 
@@ -411,12 +423,12 @@ static void *recvThread(void __unused *data)
                                                continue;
                                        }
 
-                                       if(log.length > 0) {
-                                               Eina_Bool rendering;
-
-                                               rendering = (enum rendering_option_t)*((uint8_t *)data_buf);
+                                       rendering = (enum rendering_option_t)
+                                           *((uint8_t *)get_msg_payload(&log));
+                                       if(rendering != 0) {
                                                PRINTMSG("APP_MSG_GET_UI_HIERARCHY, rendering option <%d>", rendering);
                                                ecore_set_data_get_uihv(rendering);
+                                               goto free_data_buf;
                                        } else {
                                                PRINTERR("WRONG APP_MSG_GET_UI_PROPERTIES");
                                        }
@@ -427,12 +439,14 @@ static void *recvThread(void __unused *data)
                                        set_hierarchy_status(HIERARCHY_CANCELLED);
                                        continue;
                                } else if(log.type == APP_MSG_GET_UI_SCREENSHOT) {
-                                       if(log.length > 0) {
-                                               Evas_Object *obj;
+                                       Evas_Object *obj;
+                                       obj = (Evas_Object*)(unsigned long)
+                                          *((uint64_t *)get_msg_payload(&log));
+                                       if(obj != 0) {
 
-                                               obj = (Evas_Object*)(unsigned long)*((uint64_t *)data_buf);
                                                PRINTMSG("APP_MSG_GET_UI_SCREENSHOT <0x%lx>", obj);
                                                ecore_set_data_get_obj_screenshot(obj);
+                                               goto free_data_buf;
                                        } else {
                                                PRINTERR("WRONG APP_MSG_GET_UI_SCREENSHOT");
                                        }
@@ -467,8 +481,5 @@ free_data_buf:
                }
        }
 
-       if (data_buf)
-               free(data_buf);
-
        return NULL;
 }