diff --git a/health/aidl/default/Health.cpp b/health/aidl/default/Health.cpp index acdd3362b6..4ad8f50fbd 100644 --- a/health/aidl/default/Health.cpp +++ b/health/aidl/default/Health.cpp @@ -273,7 +273,11 @@ ndk::ScopedAStatus Health::registerCallback(const std::shared_ptr lock(callbacks_lock_); - callbacks_.emplace_back(LinkedCallback::Make(ref(), callback)); + auto linked_callback_result = LinkedCallback::Make(ref(), callback); + if (!linked_callback_result.ok()) { + return ndk::ScopedAStatus::fromStatus(-linked_callback_result.error().code()); + } + callbacks_.emplace_back(std::move(*linked_callback_result)); // unlock } diff --git a/health/aidl/default/LinkedCallback.cpp b/health/aidl/default/LinkedCallback.cpp index 2985ffe959..26e99f95d3 100644 --- a/health/aidl/default/LinkedCallback.cpp +++ b/health/aidl/default/LinkedCallback.cpp @@ -24,7 +24,7 @@ namespace aidl::android::hardware::health { -std::unique_ptr LinkedCallback::Make( +::android::base::Result> LinkedCallback::Make( std::shared_ptr service, std::shared_ptr callback) { std::unique_ptr ret(new LinkedCallback()); binder_status_t linkRet = @@ -32,7 +32,7 @@ std::unique_ptr LinkedCallback::Make( reinterpret_cast(ret.get())); if (linkRet != ::STATUS_OK) { LOG(WARNING) << __func__ << "Cannot link to death: " << linkRet; - return nullptr; + return ::android::base::Error(-linkRet); } ret->service_ = service; ret->callback_ = std::move(callback); diff --git a/health/aidl/default/LinkedCallback.h b/health/aidl/default/LinkedCallback.h index 82490a7015..da494c9191 100644 --- a/health/aidl/default/LinkedCallback.h +++ b/health/aidl/default/LinkedCallback.h @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -34,8 +35,8 @@ class LinkedCallback { // service->death_reciepient() should be from CreateDeathRecipient(). // Not using a strong reference to |service| to avoid circular reference. The lifetime // of |service| must be longer than this LinkedCallback object. - static std::unique_ptr Make(std::shared_ptr service, - std::shared_ptr callback); + static ::android::base::Result> Make( + std::shared_ptr service, std::shared_ptr callback); // Automatically unlinkToDeath upon destruction. So, it is always safe to reinterpret_cast // the cookie back to the LinkedCallback object.