Check if AIDL service is declared before calling wait am: 781b4ff19e am: c65e1ef8e4 am: 0e17ea32cf

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/2138032

Change-Id: I7d7531ed6fa3f547bd0f4ff2071d6949ac6fe344
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/boot/aidl/client/BootControlClient.cpp b/boot/aidl/client/BootControlClient.cpp
index 28070eb..89258d2 100644
--- a/boot/aidl/client/BootControlClient.cpp
+++ b/boot/aidl/client/BootControlClient.cpp
@@ -327,9 +327,14 @@
     const auto instance_name =
             std::string(::aidl::android::hardware::boot::IBootControl::descriptor) + "/default";
 
-    if (auto module = ::aidl::android::hardware::boot::IBootControl::fromBinder(
+    if (AServiceManager_isDeclared(instance_name.c_str())) {
+        auto module = ::aidl::android::hardware::boot::IBootControl::fromBinder(
                 ndk::SpAIBinder(AServiceManager_waitForService(instance_name.c_str())));
-        module != nullptr) {
+        if (module == nullptr) {
+            LOG(ERROR) << "AIDL " << instance_name
+                       << " is declared but waitForService returned nullptr.";
+            return nullptr;
+        }
         LOG(INFO) << "Using AIDL version of IBootControl";
         return std::make_unique<BootControlClientAidl>(module);
     }