Fix defects detected by static analysis tool 53/318453/3
authorJi-hoon Lee <dalton.lee@samsung.com>
Mon, 30 Sep 2024 04:25:05 +0000 (13:25 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Mon, 30 Sep 2024 05:49:35 +0000 (14:49 +0900)
Change-Id: I873ad50bf6a388c8eaf2e25639d06a97568a14d0

src/mmi-cli/mmi-cli.cpp
src/mmi-manager/main.cpp
src/mmi-manager/mmi-client.cpp
src/mmi-manager/mmi-self-container.h
src/mmi/mmi-data.cpp
src/mmi/mmi-primitive-value.cpp
src/mmi/mmi-workflow-internal.h
src/mmi/mmi-workflow.cpp
tests/mmi/mmi-ipc-test.cpp

index 72b9d0b8e45a3ceea6774f5e605c7da9ba4b9c05..3f512f8d0f1863a324df0034678d03922f64acd0 100644 (file)
@@ -459,10 +459,13 @@ private:
     mmi_workflow_instance_h m_instance{nullptr};
 };
 
-static Program g_program;
-
-void process_file(std::string filename)
+void process_file(Program *program, std::string filename)
 {
+    if (nullptr == program) {
+        std::cout << "Parameter 'program' is NULL" << std::endl;
+        return;
+    }
+
     std::ifstream file(filename);
     if (!file.is_open()) {
         std::cout << "Failed to open file: " << filename << std::endl;
@@ -487,30 +490,30 @@ void process_file(std::string filename)
                 if (command.compare("sleep") == 0) {
                     sleep(std::stoi(parameters.at(0)));
                 } else if (command.compare("create") == 0) {
-                    g_program.create_instance(std::stoi(parameters.at(0)));
+                    program->create_instance(std::stoi(parameters.at(0)));
                 } else if (command.compare("create_from_script") == 0) {
-                    g_program.create_instance_from_script();
+                    program->create_instance_from_script();
                 } else if (command.compare("create_from_file") == 0) {
-                    g_program.create_instance_from_file(parameters.at(0));
+                    program->create_instance_from_file(parameters.at(0));
                 } else if (command.compare("destroy") == 0) {
-                    g_program.destroy_instance();
+                    program->destroy_instance();
                 } else if (command.compare("set_attribute") == 0) {
-                    g_program.set_attribute(
+                    program->set_attribute(
                         parameters.at(0), parameters.at(1), parameters.at(2));
                 } else if (command.compare("feed_data") == 0) {
                     if (parameters.size() == 5) {
-                        g_program.feed_data(
+                        program->feed_data(
                             parameters.at(0), parameters.at(1), parameters.at(2), parameters.at(3), parameters.at(4));
                     } else {
-                        g_program.feed_data(
+                        program->feed_data(
                             parameters.at(0), parameters.at(1), parameters.at(2), parameters.at(3));
                     }
                 } else if (command.compare("emit_signal") == 0) {
-                    g_program.emit_signal(parameters.at(0));
+                    program->emit_signal(parameters.at(0));
                 } else if (command.compare("activate") == 0) {
-                    g_program.activate_instance();
+                    program->activate_instance();
                 } else if (command.compare("deactivate") == 0) {
-                    g_program.deactivate_instance();
+                    program->deactivate_instance();
                 }
             } catch (std::out_of_range &e) {
                 std::cout << e.what() << std::endl;
@@ -522,15 +525,16 @@ void process_file(std::string filename)
 
 int main(int argc, char *argv[])
 {
-    argh::parser cmdl(argv);
+    Program program;
+    try {
+        argh::parser cmdl(argv);
 
-    std::string filename;
-    if (cmdl("file") >> filename) {
-        process_file(filename);
-        return 0;
-    }
+        std::string filename;
+        if (cmdl("file") >> filename) {
+            process_file(&program, filename);
+            return 0;
+        }
 
-    try {
         cli::SetColor();
 
         cli::CliEcoreSession *ref = nullptr;
@@ -543,69 +547,69 @@ int main(int argc, char *argv[])
         },
         "Start ecore main loop");
 
-        rootMenu->Insert("create", [](std::ostream& out, int type) {
-            g_program.create_instance(type);
+        rootMenu->Insert("create", [&program](std::ostream& out, int type) {
+            program.create_instance(type);
         },
         "Creates a new workflow instance");
 
-        rootMenu->Insert("create_from_script", [](std::ostream& out) {
-            g_program.create_instance_from_script();
+        rootMenu->Insert("create_from_script", [&program](std::ostream& out) {
+            program.create_instance_from_script();
         },
         "Creates a new workflow instance from the predefined script data");
 
-        rootMenu->Insert("create_from_file", [](std::ostream& out, std::string path) {
-            g_program.create_instance_from_file(path);
+        rootMenu->Insert("create_from_file", [&program](std::ostream& out, std::string path) {
+            program.create_instance_from_file(path);
         },
         "Creates a new workflow instance from a script file");
 
-        rootMenu->Insert("destroy", [](std::ostream& out) {
-            g_program.destroy_instance();
+        rootMenu->Insert("destroy", [&program](std::ostream& out) {
+            program.destroy_instance();
         },
         "Destroys the current workflow instance");
 
-        rootMenu->Insert("set_attribute", [](std::ostream& out, std::string name, std::string type, std::string value) {
-            g_program.set_attribute(name, type, value);
+        rootMenu->Insert("set_attribute", [&program](std::ostream& out, std::string name, std::string type, std::string value) {
+            program.set_attribute(name, type, value);
         },
         "Set attributes for the current workflow instance");
 
-        rootMenu->Insert("feed_data", [](std::ostream& out, std::string node_name, std::string port_name, std::string type, std::string value) {
-            g_program.feed_data(node_name, port_name, type, value);
+        rootMenu->Insert("feed_data", [&program](std::ostream& out, std::string node_name, std::string port_name, std::string type, std::string value) {
+            program.feed_data(node_name, port_name, type, value);
         },
         "Set attributes for the current workflow instance");
 
-        rootMenu->Insert("feed_data", [](std::ostream& out, std::string node_name, std::string port_name, std::string type, std::string value1, std::string value2) {
-            g_program.feed_data(node_name, port_name, type, value1, value2);
+        rootMenu->Insert("feed_data", [&program](std::ostream& out, std::string node_name, std::string port_name, std::string type, std::string value1, std::string value2) {
+            program.feed_data(node_name, port_name, type, value1, value2);
         },
         "Set attributes for the current workflow instance");
 
-        rootMenu->Insert("emit_signal", [](std::ostream& out, std::string signal_name) {
-            g_program.emit_signal(signal_name);
+        rootMenu->Insert("emit_signal", [&program](std::ostream& out, std::string signal_name) {
+            program.emit_signal(signal_name);
         },
         "Set attributes for the current workflow instance");
 
-        rootMenu->Insert("activate", [](std::ostream& out) {
-            g_program.activate_instance();
+        rootMenu->Insert("activate", [&program](std::ostream& out) {
+            program.activate_instance();
         },
         "Activates the current workflow instance");
 
-        rootMenu->Insert("deactivate", [](std::ostream& out) {
-            g_program.deactivate_instance();
+        rootMenu->Insert("deactivate", [&program](std::ostream& out) {
+            program.deactivate_instance();
         },
         "Deactivates the current workflow instance");
 
 #ifdef USE_VOICE_TOUCH_TV
-        rootMenu->Insert("title_selection", [](std::ostream& out, std::string value) {
-            g_program.title_selection(value);
+        rootMenu->Insert("title_selection", [&program](std::ostream& out, std::string value) {
+            program.title_selection(value);
         },
         "Find and execute the selected title on the screen");
 
-        rootMenu->Insert("ordinal_selection", [](std::ostream& out, int value) {
-            g_program.ordinal_selection(value);
+        rootMenu->Insert("ordinal_selection", [&program](std::ostream& out, int value) {
+            program.ordinal_selection(value);
         },
         "Select an item with the input value at the focused point on the screen");
 
-        rootMenu->Insert("navigation", [](std::ostream& out, std::string direction, int repeat) {
-            g_program.navigation(direction, repeat);
+        rootMenu->Insert("navigation", [&program](std::ostream& out, std::string direction, int repeat) {
+            program.navigation(direction, repeat);
         },
         "Move in the input direction as many times as specified");
 #endif
index d5f79fbe6ad6a3a29bcded1f5f7dc16e7f8a62ad..fdedcac2a29ba01b9f01b83b8648fbfbda0de605 100644 (file)
@@ -53,7 +53,7 @@ int main(int argc, char *argv[]) {
 
     communication_channel_factory = std::make_shared<mmi::communication::CommunicationChannelManagerFactory>();
 
-    mmi::Manager manager(communication_channel_factory, plugin_module_proxy_factory);
+    mmi::Manager manager(std::move(communication_channel_factory), std::move(plugin_module_proxy_factory));
 
     manager.initialize();
 
index 6c7ab410809b3874df8b6b34d96a689f6ce7b755..6b34ca4c1ef5bca546d468310ddf5415010e0f66 100644 (file)
@@ -66,7 +66,7 @@ int ClientManager::deinitialize() {
 Client* ClientManager::add_client(std::string sender) {
     LOGI("Add client(%s)\n", sender.c_str());
 
-    Client *client = new Client(sender);
+    Client *client = new(std::nothrow) Client(sender);
     if (!client) {
         LOGE("Failed to allocate memory for client !\n");
         return nullptr;
index ac6fb28110830a03ed2b19f7eb600015781a3ec1..24877a1130e3b305e0988f85ddc0119ea9d4f472 100644 (file)
@@ -56,8 +56,8 @@ public:
     static int node_signal_received_cb(mmi_node_instance_h instance, mmi_signal_h signal, void *user_data);
     static int port_input_data_received_cb(mmi_port_instance_h instance, mmi_data_h data, void *user_data);
 
-    mmi_node_callbacks_s default_node_callbacks;
-    mmi_port_callbacks_s default_port_callbacks;
+    mmi_node_callbacks_s default_node_callbacks{nullptr, };
+    mmi_port_callbacks_s default_port_callbacks{nullptr, };
 };
 
 class TestWorkflowPluginModuleWakeuplessCommand : TestWorkflowPluginModule {
@@ -69,8 +69,8 @@ public:
     TestNodePluginModuleScreenAnalyzer();
     static int node_activated_cb(mmi_node_instance_h instance, void *user_data);
 
-    mmi_node_callbacks_s m_node_callbacks;
-    mmi_port_callbacks_s m_screen_info_port_callbacks;
+    mmi_node_callbacks_s m_node_callbacks{nullptr, };
+    mmi_port_callbacks_s m_screen_info_port_callbacks{nullptr, };
 };
 
 class TestNodePluginModuleMatch : public TestNodePluginModule {
@@ -79,8 +79,8 @@ public:
 
     static int text_port_input_data_received_cb(mmi_port_instance_h instance, mmi_data_h data, void *user_data);
 
-    mmi_node_callbacks_s m_node_callbacks;
-    mmi_port_callbacks_s m_text_port_callbacks;
+    mmi_node_callbacks_s m_node_callbacks{nullptr, };
+    mmi_port_callbacks_s m_text_port_callbacks{nullptr, };
 };
 
 class TestNodePluginModuleKeyEvent : public TestNodePluginModule {
@@ -89,8 +89,8 @@ public:
 
     static int key_event_port_input_data_received_cb(mmi_port_instance_h instance, mmi_data_h data, void *user_data);
 
-    mmi_node_callbacks_s m_node_callbacks;
-    mmi_port_callbacks_s m_key_event_port_callbacks;
+    mmi_node_callbacks_s m_node_callbacks{nullptr, };
+    mmi_port_callbacks_s m_key_event_port_callbacks{nullptr, };
 };
 
 class PluginModuleProxySelfContainerTest : public IPluginModuleProxy {
index 773804c8b9df8aa6a9ff4b0dc5d7f05a2c5332f6..1859925e91650be556c219e86f70bc42f94c8303 100644 (file)
@@ -825,6 +825,12 @@ int mmi_data_to_bytes(mmi_data_h data, unsigned char **bytes, size_t *length) {
     if (type == MMI_DATA_TYPE_ARRAY || type == MMI_DATA_TYPE_STRUCT) {
         size_t current_size = sizeof(mmi_data_type_e) + sizeof(size_t);
         unsigned char *buffer = reinterpret_cast<unsigned char *>(malloc(current_size));
+        if (nullptr == buffer) {
+            // LCOV_EXCL_START
+            _E("[ERROR] Failed to allocate memory");
+            return MMI_ERROR_OUT_OF_MEMORY;
+            // LCOV_EXCL_STOP
+        }
 
         size_t count = get_count_of_elements(data);
         unsigned char *current = buffer;
@@ -961,6 +967,13 @@ int mmi_data_from_bytes(unsigned char *bytes, size_t length, mmi_data_h *data) {
         memcpy(buf, current, datalen);
 
         auto value = reinterpret_cast<mmi_data_h>(malloc(sizeof(mmi_data_s)));
+        if (nullptr == value) {
+// LCOV_EXCL_START
+            free(buf);
+            _E("[ERROR] Failed to allocate memory");
+            return MMI_ERROR_OUT_OF_MEMORY;
+// LCOV_EXCL_STOP
+        }
         value->type = type;
         value->datalen = datalen;
         value->data = buf;
index 45891b415d714be06cd4f114062b55066fb4c65b..18681a019ee41b6f54606a5951b417c546bff72c 100644 (file)
@@ -358,6 +358,13 @@ int mmi_primitive_value_clone(mmi_primitive_value_h handle, mmi_primitive_value_
     }
 
     auto cloned_value = reinterpret_cast<mmi_primitive_value_s *>(malloc(sizeof(mmi_primitive_value_s)));
+    if (nullptr == cloned_value) {
+// LCOV_EXCL_START
+        _E("[ERROR] Fail to allocate memory for a new primitive value");
+        return MMI_ERROR_OUT_OF_MEMORY;
+// LCOV_EXCL_STOP
+    }
+
     cloned_value->type = handle->type;
     cloned_value->datalen = handle->datalen;
     cloned_value->data = calloc(1, handle->datalen);
@@ -427,6 +434,12 @@ int mmi_primitive_value_to_bytes(mmi_primitive_value_h handle, unsigned char **b
     if (type == MMI_PRIMITIVE_VALUE_TYPE_ARRAY) {
         size_t current_size = sizeof(mmi_primitive_value_type_e) + sizeof(size_t);
         unsigned char *buffer = reinterpret_cast<unsigned char *>(malloc(current_size));
+        if (nullptr == buffer) {
+// LCOV_EXCL_START
+            _E("[ERROR] Fail to allocate memory for buffer");
+            return MMI_ERROR_OUT_OF_MEMORY;
+// LCOV_EXCL_STOP
+        }
         size_t count = get_array_count(handle);
         unsigned char *current = buffer;
         memcpy(current, &type, sizeof(mmi_primitive_value_type_e));
@@ -562,6 +575,13 @@ int mmi_primitive_value_from_bytes(unsigned char *bytes, size_t size, mmi_primit
         memcpy(data, current, datalen);
 
         auto value = reinterpret_cast<mmi_primitive_value_s *>(malloc(sizeof(mmi_primitive_value_s)));
+        if (nullptr == value) {
+// LCOV_EXCL_START
+            _E("[ERROR] Fail to allocate memory for a new primitive value");
+            free(data);
+            return MMI_ERROR_OUT_OF_MEMORY;
+// LCOV_EXCL_STOP
+        }
         value->type = type;
         value->datalen = datalen;
         value->data = data;
index fcb713219ceeedfa3276376f22855d24cf927fa9..d152d2a3dcbf81bdd275c5ea028bdce1286a127c 100644 (file)
@@ -80,18 +80,18 @@ struct mmi_workflow_output_assignment_info_s {
 
 struct mmi_workflow_s {
     mmi_standard_workflow_type_e type;
-    mmi_workflow_node_info_s *node_infos;
-    size_t node_info_count;
-    mmi_workflow_link_info_s *link_infos;
-    size_t link_info_count;
-    mmi_workflow_attribute_assignment_info_s *attribute_assignment_infos;
-    size_t attribute_assignment_info_count;
-    mmi_workflow_attribute_default_value_info_s *attribute_default_value_infos;
-    size_t attribute_default_value_info_count;
-    mmi_workflow_signal_assignment_info_s *signal_assignment_infos;
-    size_t signal_assignment_info_count;
-    mmi_workflow_output_assignment_info_s *output_assignment_infos;
-    size_t output_assignment_info_count;
+    mmi_workflow_node_info_s *node_infos{nullptr};
+    size_t node_info_count{0};
+    mmi_workflow_link_info_s *link_infos{nullptr};
+    size_t link_info_count{0};
+    mmi_workflow_attribute_assignment_info_s *attribute_assignment_infos{nullptr};
+    size_t attribute_assignment_info_count{0};
+    mmi_workflow_attribute_default_value_info_s *attribute_default_value_infos{nullptr};
+    size_t attribute_default_value_info_count{0};
+    mmi_workflow_signal_assignment_info_s *signal_assignment_infos{nullptr};
+    size_t signal_assignment_info_count{0};
+    mmi_workflow_output_assignment_info_s *output_assignment_infos{nullptr};
+    size_t output_assignment_info_count{0};
 };
 
 /**
index 2963c89046223cbb6d77e16d8bb23e3a5df4facd..05b703e79cd7e6a3181361f29f5a73804cb43cb2 100644 (file)
@@ -338,7 +338,17 @@ MMI_API int mmi_workflow_clone(mmi_workflow_h workflow, mmi_workflow_h *cloned)
     mmi_workflow_s *workflow_s = static_cast<mmi_workflow_s *>(workflow);
     memcpy(cloned_s, workflow_s, sizeof(mmi_workflow_s));
 
-    cloned_s->node_infos = new(std::nothrow) mmi_workflow_node_info_s[workflow_s->node_info_count];
+    if (workflow_s->node_info_count > 0) {
+        cloned_s->node_infos = new(std::nothrow) mmi_workflow_node_info_s[workflow_s->node_info_count];
+        if (nullptr == cloned_s->node_infos) {
+    // LCOV_EXCL_START
+            LOGE("[ERROR] new mmi_workflow_node_info_s failed");
+            mmi_workflow_destroy(workflow_s);
+            return MMI_ERROR_OUT_OF_MEMORY;
+    // LCOV_EXCL_STOP
+        }
+    }
+
     memcpy(cloned_s->node_infos, workflow_s->node_infos, sizeof(mmi_workflow_node_info_s) * workflow_s->node_info_count);
     for (size_t i = 0; i < workflow_s->node_info_count; i++) {
         mmi_node_clone(workflow_s->node_infos[i].node, &(cloned_s->node_infos[i].node));
@@ -346,20 +356,52 @@ MMI_API int mmi_workflow_clone(mmi_workflow_h workflow, mmi_workflow_h *cloned)
 
     if (workflow_s->link_info_count > 0) {
         cloned_s->link_infos = new(std::nothrow) mmi_workflow_link_info_s[workflow_s->link_info_count];
+        if (nullptr == cloned_s->link_infos) {
+    // LCOV_EXCL_START
+            LOGE("[ERROR] new mmi_workflow_link_info_s failed");
+            mmi_workflow_destroy(workflow_s);
+            return MMI_ERROR_OUT_OF_MEMORY;
+    // LCOV_EXCL_STOP
+        }
         memcpy(cloned_s->link_infos, workflow_s->link_infos, sizeof(mmi_workflow_link_info_s) * workflow_s->link_info_count);
     }
 
     if (workflow_s->attribute_assignment_info_count > 0) {
         cloned_s->attribute_assignment_infos = new(std::nothrow) mmi_workflow_attribute_assignment_info_s[workflow_s->attribute_assignment_info_count];
+        if (nullptr == cloned_s->attribute_assignment_infos) {
+    // LCOV_EXCL_START
+            LOGE("[ERROR] new mmi_workflow_attribute_assignment_info_s failed");
+            mmi_workflow_destroy(workflow_s);
+            return MMI_ERROR_OUT_OF_MEMORY;
+    // LCOV_EXCL_STOP
+        }
         memcpy(cloned_s->attribute_assignment_infos, workflow_s->attribute_assignment_infos,
                sizeof(mmi_workflow_attribute_assignment_info_s) * workflow_s->attribute_assignment_info_count);
     }
 
     if (workflow_s->attribute_default_value_info_count > 0) {
-        cloned_s->attribute_default_value_infos = new(std::nothrow) mmi_workflow_attribute_default_value_info_s[workflow_s->attribute_default_value_info_count];
+        cloned_s->attribute_default_value_infos =
+            new(std::nothrow) mmi_workflow_attribute_default_value_info_s[workflow_s->attribute_default_value_info_count];
+        if (nullptr == cloned_s->attribute_default_value_infos) {
+    // LCOV_EXCL_START
+            LOGE("[ERROR] new mmi_workflow_attribute_default_value_info_s failed");
+            mmi_workflow_destroy(workflow_s);
+            return MMI_ERROR_OUT_OF_MEMORY;
+    // LCOV_EXCL_STOP
+        }
         for (size_t i = 0; i < workflow_s->attribute_default_value_info_count; i++) {
+            cloned_s->attribute_default_value_infos[i].serialized_default_value_size =
+                workflow_s->attribute_default_value_infos[i].serialized_default_value_size;
             cloned_s->attribute_default_value_infos[i].serialized_default_value =
-                new(std::nothrow) unsigned char[workflow_s->attribute_default_value_infos[i].serialized_default_value_size];
+                reinterpret_cast<unsigned char *>(
+                        calloc(1, workflow_s->attribute_default_value_infos[i].serialized_default_value_size));
+            if (nullptr == cloned_s->attribute_default_value_infos[i].serialized_default_value) {
+        // LCOV_EXCL_START
+                LOGE("[ERROR] new unsigned char failed");
+                mmi_workflow_destroy(workflow_s);
+                return MMI_ERROR_OUT_OF_MEMORY;
+        // LCOV_EXCL_STOP
+            }
             memcpy(cloned_s->attribute_default_value_infos[i].serialized_default_value,
                    workflow_s->attribute_default_value_infos[i].serialized_default_value,
                    workflow_s->attribute_default_value_infos[i].serialized_default_value_size);
@@ -368,6 +410,13 @@ MMI_API int mmi_workflow_clone(mmi_workflow_h workflow, mmi_workflow_h *cloned)
 
     if (workflow_s->output_assignment_info_count > 0) {
         cloned_s->output_assignment_infos = new(std::nothrow) mmi_workflow_output_assignment_info_s[workflow_s->output_assignment_info_count];
+        if (nullptr == cloned_s->output_assignment_infos) {
+    // LCOV_EXCL_START
+            LOGE("[ERROR] new mmi_workflow_output_assignment_info_s failed");
+            mmi_workflow_destroy(workflow_s);
+            return MMI_ERROR_OUT_OF_MEMORY;
+    // LCOV_EXCL_STOP
+        }
         memcpy(cloned_s->output_assignment_infos, workflow_s->output_assignment_infos,
                sizeof(mmi_workflow_output_assignment_info_s) * workflow_s->output_assignment_info_count);
     }
@@ -391,6 +440,9 @@ MMI_API int mmi_workflow_destroy(mmi_workflow_h workflow) {
     delete [] workflow_s->node_infos;
     delete [] workflow_s->link_infos;
     delete [] workflow_s->attribute_assignment_infos;
+    for (size_t i = 0; i < workflow_s->attribute_default_value_info_count; i++) {
+        free(workflow_s->attribute_default_value_infos[i].serialized_default_value);
+    }
     delete [] workflow_s->attribute_default_value_infos;
     delete [] workflow_s->output_assignment_infos;
 
index eeede6abf4fe7e34d581bf13140a6df073485916..ef5b7a1e12fc166d405734395370256c8faf8be8 100644 (file)
@@ -30,7 +30,7 @@ static void wait_until_connected(int msec) {
     int i = 0;
     while (g_state != MMI_STATE_READY && i < msec) {
         ecore_main_loop_iterate();
-        usleep(1000);
+        usleep(10000);
         i++;
     }
 }