From 1306658d780e938ec4304f37a4791470a4269048 Mon Sep 17 00:00:00 2001 From: Kevin Schoedel <67607049+kpschoedel@users.noreply.github.com> Date: Tue, 7 Sep 2021 18:25:40 -0400 Subject: [PATCH] Don't clobber chip-tool error (#9504) * Don't clobber chip-tool error #### Problem 82cf1b132f22 (PR #9086) could clobber a returned error code with a different value `GetCommandExitStatus()`, which could lead to `chip-tool` exiting successfully when it shouldn't. (This doesn't actually happen at present because nothing sets `err` after `RunCommand()`, but could be triggered by otherwise innocuous code changes.) #### Change overview Check for an existing error in preference to `GetCommandExitStatus()`. #### Testing Manual run of `chip-tool` with edits. * stop task before getting status --- examples/chip-tool/commands/common/Commands.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/examples/chip-tool/commands/common/Commands.cpp b/examples/chip-tool/commands/common/Commands.cpp index ea8d5e4d8b52f3..aa98ac9dc38871 100644 --- a/examples/chip-tool/commands/common/Commands.cpp +++ b/examples/chip-tool/commands/common/Commands.cpp @@ -121,22 +121,17 @@ int Commands::Run(int argc, char ** argv) #endif // !CONFIG_USE_SEPARATE_EVENTLOOP exit: - if (err != CHIP_NO_ERROR) - { - ChipLogError(chipTool, "Run command failure: %s", chip::ErrorStr(err)); - } - #if CONFIG_USE_SEPARATE_EVENTLOOP chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); #endif // CONFIG_USE_SEPARATE_EVENTLOOP - if (command) + if ((err == CHIP_NO_ERROR) && (command != nullptr)) { err = command->GetCommandExitStatus(); - if (err != CHIP_NO_ERROR) - { - ChipLogError(chipTool, "Run command failure: %s", chip::ErrorStr(err)); - } + } + if (err != CHIP_NO_ERROR) + { + ChipLogError(chipTool, "Run command failure: %s", chip::ErrorStr(err)); } if (command)