From 210e7de24efd5412eebc002ade16b4fc48a4027d Mon Sep 17 00:00:00 2001 From: Seungha Son Date: Tue, 8 Aug 2017 13:50:26 +0900 Subject: [PATCH] Add OOM exception handler Signed-off-by: Seungha Son Change-Id: Ic86db1a8e8e713effe6af78bf1979e382e3c9071 --- src/wgt/step/configuration/step_parse.cc | 77 ++++++++++++++++++++++ src/wgt/step/filesystem/step_wgt_patch_icons.cc | 4 ++ .../step/security/step_add_default_privileges.cc | 14 ++++ .../security/step_check_extension_privileges.cc | 17 +++++ .../step/security/step_check_wgt_ime_privilege.cc | 14 ++++ 5 files changed, 126 insertions(+) diff --git a/src/wgt/step/configuration/step_parse.cc b/src/wgt/step/configuration/step_parse.cc index 47605b4..a9e3ca1 100644 --- a/src/wgt/step/configuration/step_parse.cc +++ b/src/wgt/step/configuration/step_parse.cc @@ -65,11 +65,28 @@ const char kTTSCategoryName[] = "http://tizen.org/category/tts"; const char kResWgt[] = "res/wgt"; const char kConfigFileName[] = "config.xml"; +void FreeMetadataList(gpointer data) +{ + metadata_x* metadata = reinterpret_cast(data); + if (metadata) { + if (metadata->key) + free(const_cast(metadata->key)); + if (metadata->value) + free(const_cast(metadata->value)); + free(metadata); + } +} + GList* GenerateMetadataListX(const wgt::parse::MetaDataInfo& meta_info) { GList* list = nullptr; for (auto& meta : meta_info.metadata()) { metadata_x* new_meta = static_cast(calloc(1, sizeof(metadata_x))); + if (!new_meta) { + LOG(ERROR) << "Out of memory"; + g_list_free_full(list, &FreeMetadataList); + return nullptr; + } new_meta->key = strdup(meta.first.c_str()); if (!meta.second.empty()) new_meta->value = strdup(meta.second.c_str()); @@ -84,6 +101,10 @@ void AppendWidgetMetadata(GList** metadatas, for (auto& meta : metadata) { metadata_x* new_meta = static_cast(calloc(1, sizeof(metadata_x))); + if (!new_meta) { + LOG(ERROR) << "Out of memory"; + return; + } new_meta->key = strdup(meta.first.c_str()); if (!meta.second.empty()) new_meta->value = strdup(meta.second.c_str()); @@ -114,6 +135,10 @@ template void AppendLabel(T* root, const std::string& label, const std::string& locale) { label_x* label_item = reinterpret_cast(calloc(1, sizeof(label_x))); + if (!label_item) { + LOG(ERROR) << "Out of memory"; + return; + } label_item->name = strdup(label.c_str()); label_item->text = strdup(label.c_str()); label_item->lang = !locale.empty() ? @@ -190,6 +215,10 @@ bool StepParse::FillIconPaths(manifest_x* manifest) { found_locales.insert(locale); icon_x* icon = reinterpret_cast(calloc(1, sizeof(icon_x))); + if (!icon) { + LOG(ERROR) << "Out of memory"; + return false; + } bf::path icon_path = context_->root_application_path.get() / app_info->package() / "res" / "wgt" / application_icon.path(); icon->text = strdup(icon_path.c_str()); @@ -220,6 +249,10 @@ bool StepParse::FillWidgetInfo(manifest_x* manifest) { for (auto& item : wgt_info->description_set()) { description_x* description = reinterpret_cast (calloc(1, sizeof(description_x))); + if (!description) { + LOG(ERROR) << "Out of memory"; + return false; + } description->text = strdup(item.second.c_str()); description->lang = !item.first.empty() ? strdup(item.first.c_str()) : strdup(DEFAULT_LOCALE); @@ -243,6 +276,10 @@ bool StepParse::FillWidgetInfo(manifest_x* manifest) { } author_x* author = reinterpret_cast(calloc(1, sizeof(author_x))); + if (!author) { + LOG(ERROR) << "Out of memory"; + return false; + } if (!wgt_info->author().empty()) author->text = strdup(wgt_info->author().c_str()); if (!wgt_info->author_email().empty()) @@ -336,6 +373,10 @@ bool StepParse::FillMainApplicationInfo(manifest_x* manifest) { // application data application_x* application = reinterpret_cast( calloc(1, sizeof(application_x))); + if (!application) { + LOG(ERROR) << "Out of memory"; + return false; + } application->component_type = has_watch_category ? strdup("watchapp") : strdup("uiapp"); application->mainapp = strdup("true"); @@ -371,6 +412,10 @@ bool StepParse::FillMainApplicationInfo(manifest_x* manifest) { application->launch_mode = strdup(app_info->launch_mode().c_str()); for (auto& icon : GListRange(manifest->icon)) { icon_x* app_icon = reinterpret_cast(calloc(1, sizeof(icon_x))); + if (!app_icon) { + LOG(ERROR) << "Out of memory"; + return false; + } app_icon->text = strdup(icon->text); app_icon->lang = strdup(icon->lang); application->icon = g_list_append(application->icon, app_icon); @@ -392,6 +437,10 @@ bool StepParse::FillServiceApplicationInfo(manifest_x* manifest) { for (auto& service_info : service_list->services) { application_x* application = reinterpret_cast (calloc(1, sizeof(application_x))); + if (!application) { + LOG(ERROR) << "Out of memory"; + return false; + } application->component_type = strdup("svcapp"); application->mainapp = strdup("false"); application->appid = strdup(service_info.id().c_str()); @@ -417,6 +466,10 @@ bool StepParse::FillServiceApplicationInfo(manifest_x* manifest) { bf::path icon_path = context_->root_application_path.get() / manifest->package / "res" / "wgt" / service_info.icon(); icon_x* icon = reinterpret_cast(calloc(1, sizeof(icon_x))); + if (!icon) { + LOG(ERROR) << "Out of memory"; + return false; + } icon->text = strdup(icon_path.c_str()); icon->lang = strdup(DEFAULT_LOCALE); application->icon = g_list_append(application->icon, icon); @@ -430,6 +483,10 @@ bool StepParse::FillServiceApplicationInfo(manifest_x* manifest) { for (auto& pair : service_info.metadata_set()) { metadata_x* item = reinterpret_cast( calloc(1, sizeof(metadata_x))); + if (!item) { + LOG(ERROR) << "Out of memory"; + return false; + } item->key = strdup(pair.first.c_str()); if (!pair.second.empty()) item->value = strdup(pair.second.c_str()); @@ -450,6 +507,10 @@ bool StepParse::FillWidgetApplicationInfo(manifest_x* manifest) { for (auto& app_widget : appwidget_info->app_widgets()) { application_x* application = reinterpret_cast (calloc(1, sizeof(application_x))); + if (!application) { + LOG(ERROR) << "Out of memory"; + return false; + } application->component_type = strdup("widgetapp"); application->mainapp = strdup("false"); application->appid = strdup(app_widget.id.c_str()); @@ -473,6 +534,10 @@ bool StepParse::FillWidgetApplicationInfo(manifest_x* manifest) { if (!app_widget.icon_src.empty()) { icon_x* icon = reinterpret_cast(calloc(1, sizeof(icon_x))); + if (!icon) { + LOG(ERROR) << "Out of memory"; + return false; + } icon->text = strdup(app_widget.icon_src.c_str()); icon->lang = strdup(DEFAULT_LOCALE); application->icon = g_list_append(application->icon, icon); @@ -544,6 +609,10 @@ bool StepParse::FillAppControl(manifest_x* manifest) { for (const auto& control : app_info_list->controls) { appcontrol_x* app_control = static_cast(calloc(1, sizeof(appcontrol_x))); + if (!app_control) { + LOG(ERROR) << "Out of memory"; + return false; + } app_control->operation = strdup(control.operation().c_str()); app_control->mime = strdup(control.mime().c_str()); app_control->uri = strdup(control.uri().c_str()); @@ -564,6 +633,10 @@ bool StepParse::FillPrivileges(manifest_x* manifest) { for (auto& priv : privileges) { privilege_x* privilege = reinterpret_cast(calloc(1, sizeof(privilege_x))); + if (!privilege) { + LOG(ERROR) << "Out of memory"; + return false; + } privilege->type = strdup(common_installer::kWebPrivilegeType); privilege->value = strdup(priv.c_str()); manifest->privileges = g_list_append(manifest->privileges, privilege); @@ -829,6 +902,10 @@ common_installer::Step::Status StepParse::process() { manifest_x* manifest = static_cast(calloc(1, sizeof(manifest_x))); + if (!manifest) { + LOG(ERROR) << "Out of memory"; + return common_installer::Step::Status::ERROR; + } if (!FillManifestX(manifest)) { LOG(ERROR) << "[Parse] Storing manifest_x failed. " << parser_->GetErrorMessage(); diff --git a/src/wgt/step/filesystem/step_wgt_patch_icons.cc b/src/wgt/step/filesystem/step_wgt_patch_icons.cc index 8f0a0a1..89e6fff 100644 --- a/src/wgt/step/filesystem/step_wgt_patch_icons.cc +++ b/src/wgt/step/filesystem/step_wgt_patch_icons.cc @@ -87,6 +87,10 @@ common_installer::Step::Status StepWgtPatchIcons::process() { return Status::ICON_ERROR; } icon_x* icon = reinterpret_cast(calloc(1, sizeof(icon_x))); + if (!icon) { + LOG(ERROR) << "Out of memory"; + return Status::ICON_ERROR; + } icon->text = strdup(icon_path.c_str()); icon->lang = strdup(DEFAULT_LOCALE); app->icon = g_list_append(app->icon, icon); diff --git a/src/wgt/step/security/step_add_default_privileges.cc b/src/wgt/step/security/step_add_default_privileges.cc index 47098d3..2718011 100644 --- a/src/wgt/step/security/step_add_default_privileges.cc +++ b/src/wgt/step/security/step_add_default_privileges.cc @@ -26,8 +26,22 @@ common_installer::Step::Status StepAddDefaultPrivileges::process() { manifest_x* m = context_->manifest_data.get(); privilege_x* privilege = reinterpret_cast(calloc(1, sizeof(privilege_x))); + if (!privilege) { + LOG(ERROR) << "Out of memory"; + return Status::ERROR; + } privilege->type = strdup(common_installer::kWebPrivilegeType); + if (!privilege->type) { + LOG(ERROR) << "Out of memory"; + common_installer::FreePrivilegeX(privilege); + return Status::ERROR; + } privilege->value = strdup(common_installer::privileges::kPrivForWebApp); + if (!privilege->value) { + LOG(ERROR) << "Out of memory"; + common_installer::FreePrivilegeX(privilege); + return Status::ERROR; + } m->privileges = g_list_append(m->privileges, privilege); return Status::OK; } diff --git a/src/wgt/step/security/step_check_extension_privileges.cc b/src/wgt/step/security/step_check_extension_privileges.cc index 47f874e..78187d2 100755 --- a/src/wgt/step/security/step_check_extension_privileges.cc +++ b/src/wgt/step/security/step_check_extension_privileges.cc @@ -73,8 +73,25 @@ common_installer::Step::Status StepCheckExtensionPrivileges::process() { if (current_privileges.find(priv) == current_privileges.end()) { privilege_x* privilege = reinterpret_cast(calloc(1, sizeof(privilege_x))); + if (!privilege) { + LOG(ERROR) << "Out of memory"; + g_list_free_full(privileges, &common_installer::FreePrivilegeX); + return Status::ERROR; + } privilege->type = strdup(common_installer::kWebPrivilegeType); + if (!privilege->type) { + LOG(ERROR) << "Out of memory"; + common_installer::FreePrivilegeX(privilege); + g_list_free_full(privileges, &common_installer::FreePrivilegeX); + return Status::ERROR; + } privilege->value = strdup(priv.c_str()); + if (!privilege->value) { + LOG(ERROR) << "Out of memory"; + common_installer::FreePrivilegeX(privilege); + g_list_free_full(privileges, &common_installer::FreePrivilegeX); + return Status::ERROR; + } privileges = g_list_append(privileges, privilege); } } diff --git a/src/wgt/step/security/step_check_wgt_ime_privilege.cc b/src/wgt/step/security/step_check_wgt_ime_privilege.cc index 2703b7e..2a6d325 100644 --- a/src/wgt/step/security/step_check_wgt_ime_privilege.cc +++ b/src/wgt/step/security/step_check_wgt_ime_privilege.cc @@ -51,8 +51,22 @@ common_installer::Step::Status StepCheckWgtImePrivilege::process() { // be sure there's a privilege in manifest privilege_x* privilege = reinterpret_cast(calloc(1, sizeof(privilege_x))); + if (!privilege) { + LOG(ERROR) << "Out of memory"; + return Status::ERROR; + } privilege->type = strdup(common_installer::kWebPrivilegeType); + if (!privilege->type) { + LOG(ERROR) << "Out of memory"; + common_installer::FreePrivilegeX(privilege); + return Status::ERROR; + } privilege->value = strdup(common_installer::privileges::kImePrivilegeName); + if (!privilege->value) { + LOG(ERROR) << "Out of memory"; + common_installer::FreePrivilegeX(privilege); + return Status::ERROR; + } context_->manifest_data.get()->privileges = g_list_append(context_->manifest_data.get()->privileges, privilege); } -- 2.7.4