diff --git a/runtime/executor/method_meta.cpp b/runtime/executor/method_meta.cpp index 933670c526f..eb019f64e71 100644 --- a/runtime/executor/method_meta.cpp +++ b/runtime/executor/method_meta.cpp @@ -245,12 +245,15 @@ size_t MethodMeta::num_backends() const { return delegates ? delegates->size() : 0; } -const char* MethodMeta::get_backend_name(size_t index) const { - const auto delegates = s_plan_->delegates(); - if (delegates && index < delegates->size()) { - return delegates->Get(index)->id()->c_str(); - } - return nullptr; +Result MethodMeta::get_backend_name(size_t index) const { + const auto count = num_backends(); + ET_CHECK_OR_RETURN_ERROR( + index < count, + InvalidArgument, + "Index %zu out of range. num_backends: %zu", + index, + count); + return s_plan_->delegates()->Get(index)->id()->c_str(); } size_t MethodMeta::num_instructions() const { diff --git a/runtime/executor/method_meta.h b/runtime/executor/method_meta.h index 9d38d133cbc..d9bb64d68a7 100644 --- a/runtime/executor/method_meta.h +++ b/runtime/executor/method_meta.h @@ -204,10 +204,10 @@ class MethodMeta final { * Get the backend name at the given index. * * @param[in] index The index of the backend name. - * @returns The backend name as a C-style string, or nullptr if the index is - * invalid. + * @returns A Result wrapping the backend name as a C-style string + * on success, or an error if the index is invalid. */ - const char* get_backend_name(size_t index) const; + Result get_backend_name(size_t index) const; /** * Get the number of instructions in this method. diff --git a/runtime/executor/test/backend_integration_test.cpp b/runtime/executor/test/backend_integration_test.cpp index f9e3bb7b188..587c7b353eb 100644 --- a/runtime/executor/test/backend_integration_test.cpp +++ b/runtime/executor/test/backend_integration_test.cpp @@ -362,18 +362,20 @@ TEST_P(BackendIntegrationTest, GetBackendNamesSuccess) { EXPECT_TRUE(method_meta->uses_backend(StubBackend::kName)); // Retrieve the number of backends. - size_t num_backends = method_meta->num_backends(); + const size_t num_backends = method_meta->num_backends(); EXPECT_GT(num_backends, 0u); // Iterate through each backend and verify its name. for (size_t i = 0; i < num_backends; ++i) { - const char* name = method_meta->get_backend_name(i); - EXPECT_NE(name, nullptr); + auto backend_name_result = method_meta->get_backend_name(i); + ASSERT_TRUE(backend_name_result.ok()); + const char* name = backend_name_result.get(); // For this test, we expect that the only backend is StubBackend. EXPECT_STREQ(name, StubBackend::kName); } - // Check that an out-of-range index returns nullptr. - EXPECT_EQ(method_meta->get_backend_name(num_backends), nullptr); + // Check that an out-of-range index returns an error. + auto out_of_range_result = method_meta->get_backend_name(num_backends); + EXPECT_FALSE(out_of_range_result.ok()); } TEST_P(BackendIntegrationTest, FreeingProcessedBufferSucceeds) {