From: Ji-hoon Lee Date: Mon, 30 Sep 2024 04:25:05 +0000 (+0900) Subject: Fix defects detected by static analysis tool X-Git-Tag: accepted/tizen/unified/20241004.041950~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ecbbebf4d865edad8e207deb17e47262806a7251;p=platform%2Fcore%2Fuifw%2Fmmi-framework.git Fix defects detected by static analysis tool Change-Id: I873ad50bf6a388c8eaf2e25639d06a97568a14d0 --- diff --git a/src/mmi-cli/mmi-cli.cpp b/src/mmi-cli/mmi-cli.cpp index 72b9d0b..3f512f8 100644 --- a/src/mmi-cli/mmi-cli.cpp +++ b/src/mmi-cli/mmi-cli.cpp @@ -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 diff --git a/src/mmi-manager/main.cpp b/src/mmi-manager/main.cpp index d5f79fb..fdedcac 100644 --- a/src/mmi-manager/main.cpp +++ b/src/mmi-manager/main.cpp @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) { communication_channel_factory = std::make_shared(); - 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(); diff --git a/src/mmi-manager/mmi-client.cpp b/src/mmi-manager/mmi-client.cpp index 6c7ab41..6b34ca4 100644 --- a/src/mmi-manager/mmi-client.cpp +++ b/src/mmi-manager/mmi-client.cpp @@ -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; diff --git a/src/mmi-manager/mmi-self-container.h b/src/mmi-manager/mmi-self-container.h index ac6fb28..24877a1 100644 --- a/src/mmi-manager/mmi-self-container.h +++ b/src/mmi-manager/mmi-self-container.h @@ -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 { diff --git a/src/mmi/mmi-data.cpp b/src/mmi/mmi-data.cpp index 773804c..1859925 100644 --- a/src/mmi/mmi-data.cpp +++ b/src/mmi/mmi-data.cpp @@ -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(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(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; diff --git a/src/mmi/mmi-primitive-value.cpp b/src/mmi/mmi-primitive-value.cpp index 45891b4..18681a0 100644 --- a/src/mmi/mmi-primitive-value.cpp +++ b/src/mmi/mmi-primitive-value.cpp @@ -358,6 +358,13 @@ int mmi_primitive_value_clone(mmi_primitive_value_h handle, mmi_primitive_value_ } auto cloned_value = reinterpret_cast(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(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(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; diff --git a/src/mmi/mmi-workflow-internal.h b/src/mmi/mmi-workflow-internal.h index fcb7132..d152d2a 100644 --- a/src/mmi/mmi-workflow-internal.h +++ b/src/mmi/mmi-workflow-internal.h @@ -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}; }; /** diff --git a/src/mmi/mmi-workflow.cpp b/src/mmi/mmi-workflow.cpp index 2963c89..05b703e 100644 --- a/src/mmi/mmi-workflow.cpp +++ b/src/mmi/mmi-workflow.cpp @@ -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(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( + 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; diff --git a/tests/mmi/mmi-ipc-test.cpp b/tests/mmi/mmi-ipc-test.cpp index eeede6a..ef5b7a1 100644 --- a/tests/mmi/mmi-ipc-test.cpp +++ b/tests/mmi/mmi-ipc-test.cpp @@ -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++; } }