pldmd: fix unsupported command handling A request with a valid PLDM type but an unregistered command currently results in std::out_of_range from the command handler lookup. Unsupported commands are protocol-level errors and should return a PLDM completion code instead of propagating a C++ exception. Replace the throwing command lookup with a guarded lookup and return PLDM_ERROR_UNSUPPORTED_PLDM_CMD when a command is not registered. Update the registration test to verify that unsupported commands return the expected completion code rather than throwing an exception. Tested: - meson test -C build pldmd_registration_test --print-errorlogs Change-Id: Ia85d86fd4d22f7510e8ee52876f31643470eee70 Signed-off-by: Meghana Vangapandu <meghanav@ami.com>
diff --git a/pldmd/handler.hpp b/pldmd/handler.hpp index 89b8a7f..9709f80 100644 --- a/pldmd/handler.hpp +++ b/pldmd/handler.hpp
@@ -36,7 +36,12 @@ Response handle(pldm_tid_t tid, Command pldmCommand, const pldm_msg* request, size_t reqMsgLen) { - return handlers.at(pldmCommand)(tid, request, reqMsgLen); + auto handler = handlers.find(pldmCommand); + if (handler == handlers.end()) + { + return ccOnlyResponse(request, PLDM_ERROR_UNSUPPORTED_PLDM_CMD); + } + return handler->second(tid, request, reqMsgLen); } /** @brief Create a response message containing only cc
diff --git a/test/pldmd_registration_test.cpp b/test/pldmd_registration_test.cpp index e98bb21..6247e2d 100644 --- a/test/pldmd_registration_test.cpp +++ b/test/pldmd_registration_test.cpp
@@ -55,6 +55,8 @@ Invoker invoker{}; const Response kExpectedBadTypeResponse = {0x01, 0x02, 0x03, PLDM_ERROR_INVALID_PLDM_TYPE}; + const Response kExpectedBadCmdResponse = {0x01, 0x02, 0x03, + PLDM_ERROR_UNSUPPORTED_PLDM_CMD}; const std::array<uint8_t, sizeof(pldm_msg)> kDummyPldmRequestBacking = { 0x01, 0x02, 0x03}; const pldm_msg* kDummyPldmRequest = @@ -65,6 +67,6 @@ invoker.registerHandler(testType, std::make_unique<TestHandler>()); uint8_t badCmd = 0xFE; - ASSERT_THROW(invoker.handle(tid, testType, badCmd, nullptr, 0), - std::out_of_range); + EXPECT_EQ(invoker.handle(tid, testType, badCmd, kDummyPldmRequest, 0), + kExpectedBadCmdResponse); }