From 4ffcb9c55e086b7666a0b4fec1eeb2213539dd44 Mon Sep 17 00:00:00 2001 From: hyunho Date: Mon, 24 Jun 2019 10:53:32 +0900 Subject: [PATCH] Fix wrong exception handling nothrow only guarantee that new operation will not throw exception. Using nothrow cannot prevent exceptions thrown by a constructor. Change-Id: I1cafbe6c582f64abb007c12dcafd79fac569ffcb Signed-off-by: hyunho --- watchface-complication/complication.cc | 42 ++++++++++++++++----------- watchface-complication/db-manager.cc | 9 +++--- watchface-complication/design-element.cc | 11 +++---- watchface-complication/editables-container.cc | 7 +++-- watchface-complication/editables-manager.cc | 8 ++--- watchface-complication/received-editable.cc | 10 +++---- watchface-complication/watchface-editable.cc | 18 ++++++++---- watchface-editor/watchface-editor.cc | 17 ++++++----- 8 files changed, 70 insertions(+), 52 deletions(-) diff --git a/watchface-complication/complication.cc b/watchface-complication/complication.cc index 4687b6d..7d997cb 100644 --- a/watchface-complication/complication.cc +++ b/watchface-complication/complication.cc @@ -191,7 +191,7 @@ void Complication::Impl::UpdatedProcess(GVariant* parameters) { char* provider_id; int type; int complication_id; - bundle_raw* raw = NULL; + bundle_raw* raw = nullptr; g_variant_get(parameters, "(&sii&s)", &provider_id, &type, &complication_id, &raw); @@ -203,17 +203,18 @@ void Complication::Impl::UpdatedProcess(GVariant* parameters) { return; } - if (raw == nullptr) { + if (strlen(reinterpret_cast(raw)) == 0) { LOGW("Empty data !!"); return; } - last_data_.reset(new (std::nothrow) Bundle( - string(reinterpret_cast(raw)))); - if (last_data_.get() == nullptr) { - LOGE("Out of memmory, create bundle fail"); + try { + last_data_.reset(new Bundle(string(reinterpret_cast(raw)))); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return; } + LOGI("data: %s, %d, cur_type :%d", provider_id, complication_id, cur_type_); parent_->OnDataUpdated(std::string(provider_id), cur_type_, last_data_); } @@ -760,11 +761,14 @@ int Complication::UpdateLastContext() { impl_->context_data_.get()); if (ret != WATCHFACE_COMPLICATION_ERROR_NONE) return ret; - Bundle* new_ctx = new (std::nothrow) Bundle( - (impl_->context_data_.get())->GetHandle()); - if (new_ctx == nullptr) + + try { + Bundle* new_ctx = new Bundle(impl_->context_data_->GetHandle()); + impl_->last_context_data_.reset(new_ctx); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; - impl_->last_context_data_.reset(new_ctx); + } return WATCHFACE_COMPLICATION_ERROR_NONE; } @@ -864,9 +868,11 @@ int Complication::Impl::GetNotSupportedEvents( } int Complication::Impl::AddCandidate(std::string provider_id, int type) { - unique_ptr data = unique_ptr(new (std::nothrow) Bundle()); - if (data.get() == nullptr) { - LOGE("Out of memory"); + unique_ptr data; + try { + data = unique_ptr(new Bundle()); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; } @@ -962,12 +968,14 @@ int Complication::Impl::LoadContext() { cur_provider_id_.c_str()); if (context_data_ == nullptr) return WATCHFACE_COMPLICATION_ERROR_NONE; - Bundle* ctx = new (std::nothrow) Bundle(context_data_.get()->GetHandle()); - if (ctx == nullptr) { - LOGE("Out of memory"); + + try { + Bundle* ctx = new Bundle(context_data_->GetHandle()); + last_context_data_.reset(ctx); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; } - last_context_data_.reset(ctx); return WATCHFACE_COMPLICATION_ERROR_NONE; } diff --git a/watchface-complication/db-manager.cc b/watchface-complication/db-manager.cc index b650765..82e991d 100644 --- a/watchface-complication/db-manager.cc +++ b/watchface-complication/db-manager.cc @@ -126,12 +126,13 @@ std::unique_ptr DBManager::GetDefaultData(const char* provider_id, goto out; } - default_data = std::unique_ptr( - new (std::nothrow) Bundle(std::string(raw_data))); - if (default_data.get() == nullptr) { - LOGE("fail to create default data"); + try { + default_data = std::unique_ptr(new Bundle(std::string(raw_data))); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); goto out; } + util::ConvertPathToAppPath( provider_app_id.c_str(), default_data.get()->GetHandle()); } diff --git a/watchface-complication/design-element.cc b/watchface-complication/design-element.cc index 6b95681..8a327e6 100644 --- a/watchface-complication/design-element.cc +++ b/watchface-complication/design-element.cc @@ -180,13 +180,14 @@ int DesignElement::UpdateLastContext() { LOGI("Empty context"); return WATCHFACE_COMPLICATION_ERROR_NONE; } - Bundle* ctx = new (std::nothrow) Bundle( - (impl_->context_data_.get())->GetHandle()); - if (ctx == nullptr) { - LOGE("Out of memory"); + + try { + Bundle* ctx = new Bundle(impl_->context_data_->GetHandle()); + impl_->last_context_data_.reset(ctx); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; } - impl_->last_context_data_.reset(ctx); return WATCHFACE_COMPLICATION_ERROR_NONE; } diff --git a/watchface-complication/editables-container.cc b/watchface-complication/editables-container.cc index e191d2f..285513d 100644 --- a/watchface-complication/editables-container.cc +++ b/watchface-complication/editables-container.cc @@ -110,9 +110,10 @@ void EditablesContainer::Impl::OnSignal(GDBusConnection* connection, unique_ptr ctx_ptr; std::string ctx_str = std::string(context); if (!ctx_str.empty()) { - ctx_ptr = std::unique_ptr(new (std::nothrow) Bundle(ctx_str)); - if (ctx_ptr.get() == nullptr) { - LOGE("Fail to create bundle"); + try { + ctx_ptr = std::unique_ptr(new Bundle(ctx_str)); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return; } } diff --git a/watchface-complication/editables-manager.cc b/watchface-complication/editables-manager.cc index 240240c..8734a79 100644 --- a/watchface-complication/editables-manager.cc +++ b/watchface-complication/editables-manager.cc @@ -217,10 +217,10 @@ std::unique_ptr EditablesManager::LoadContext(int editable_id, if (sqlite3_step(stmt) == SQLITE_ROW) { raw_data = reinterpret_cast(sqlite3_column_text(stmt, 0)); - context_data = std::unique_ptr( - new (std::nothrow) Bundle(std::string(raw_data))); - if (context_data.get() == nullptr) { - LOGE("Out of memory"); + try { + context_data = std::unique_ptr(new Bundle(std::string(raw_data))); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); sqlite3_finalize(stmt); return nullptr; } diff --git a/watchface-complication/received-editable.cc b/watchface-complication/received-editable.cc index fb6da37..6f600b1 100644 --- a/watchface-complication/received-editable.cc +++ b/watchface-complication/received-editable.cc @@ -222,13 +222,13 @@ int ReceivedEditable::UpdateLastContext() { return WATCHFACE_COMPLICATION_ERROR_NONE; } - Bundle* ctx = new (std::nothrow) Bundle( - (impl_->context_data_.get())->GetHandle()); - if (ctx == nullptr) { - LOGE("Out of memory"); + try { + Bundle* ctx = new Bundle(impl_->context_data_->GetHandle()); + impl_->last_context_data_.reset(ctx); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; } - impl_->last_context_data_.reset(ctx); return WATCHFACE_COMPLICATION_ERROR_NONE; } diff --git a/watchface-complication/watchface-editable.cc b/watchface-complication/watchface-editable.cc index c10d185..798c3ff 100644 --- a/watchface-complication/watchface-editable.cc +++ b/watchface-complication/watchface-editable.cc @@ -204,12 +204,12 @@ extern "C" EXPORT_API int watchface_editable_add_design_element( if (data == nullptr) continue; - Bundle* new_data = new (std::nothrow) Bundle(data); - if (new_data == nullptr) { - LOGE("Out of memory"); + try { + new_list.emplace_back(new Bundle(data)); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; } - new_list.emplace_back(move(new_data)); } if (ec->IsExist(edit_id)) { @@ -317,8 +317,14 @@ extern "C" EXPORT_API int watchface_editable_add_edit_ready_cb( if (cb == nullptr) return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER; - if (__container == nullptr) - __container = new (std::nothrow) EditablesContainerStub(); + if (__container == nullptr) { + try { + __container = new EditablesContainerStub(); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); + return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; + } + } if (__container == nullptr) { LOGE("Out of memory"); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; diff --git a/watchface-editor/watchface-editor.cc b/watchface-editor/watchface-editor.cc index 558bae1..65f8bd9 100644 --- a/watchface-editor/watchface-editor.cc +++ b/watchface-editor/watchface-editor.cc @@ -190,10 +190,10 @@ extern "C" EXPORT_API int watchface_editor_add_request_edit_cb( } if (__stub == nullptr) { - __stub = std::unique_ptr( - new (std::nothrow) EditablesEditorStub()); - if (__stub == nullptr) { - LOGE("Out of memory"); + try { + __stub = std::unique_ptr(new EditablesEditorStub()); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; } } @@ -438,12 +438,13 @@ extern "C" EXPORT_API int watchface_editor_set_context( int re = WATCHFACE_COMPLICATION_ERROR_NONE; IEditable* ed = static_cast(handle); if (new_context) { - std::unique_ptr b(new (std::nothrow) Bundle(new_context)); - if (b.get() == nullptr) { - LOGE("Out of memory"); + try { + std::unique_ptr b(new Bundle(new_context)); + re = ed->SetContext(std::move(b)); + } catch (const std::exception& e) { + LOGE("Exception occurred : %s", e.what()); return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; } - re = ed->SetContext(std::move(b)); } else { re = ed->SetContext(std::unique_ptr{}); } -- 2.7.4