From 24a7b2f894cf52d9083087894c4413d4b65bf0d4 Mon Sep 17 00:00:00 2001 From: liuh-80 Date: Fri, 17 Feb 2023 04:16:01 +0000 Subject: [PATCH 1/3] Prevent sonic-db-cli generate core dump --- sonic-db-cli/main.cpp | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/sonic-db-cli/main.cpp b/sonic-db-cli/main.cpp index b0fde5ea5..452152ecc 100755 --- a/sonic-db-cli/main.cpp +++ b/sonic-db-cli/main.cpp @@ -16,9 +16,28 @@ int main(int argc, char** argv) SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE); }; - return sonic_db_cli( - argc, - argv, - initializeGlobalConfig, - initializeConfig); + + try + { + return sonic_db_cli( + argc, + argv, + initializeGlobalConfig, + initializeConfig); + } + catch (const exception& e) + { + // sonic-db-cli is porting from python version. + // in python version, when any exception happen sonic-db-cli will crash but will not generate core dump file. + // catch all exception here to avoid a core dump file generated. + cerr << "An exception of type " << e.what() << " occurred. Arguments:" << endl; + for (int idx = 0; idx < argc; idx++) + { + cerr << argv[idx] << " "; + } + cerr << endl; + + // when python version crash, exit code is 1. + return 1; + } } \ No newline at end of file From 12cc7925f3441bb0eb09aed1b1943107b278e386 Mon Sep 17 00:00:00 2001 From: liuh-80 Date: Fri, 17 Feb 2023 04:33:27 +0000 Subject: [PATCH 2/3] Fix build issue --- sonic-db-cli/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sonic-db-cli/main.cpp b/sonic-db-cli/main.cpp index 452152ecc..a15152d65 100755 --- a/sonic-db-cli/main.cpp +++ b/sonic-db-cli/main.cpp @@ -1,5 +1,6 @@ #include "sonic-db-cli.h" #include "common/dbconnector.h" +#include using namespace swss; using namespace std; From 55a6dcf6924e0d494cfe51eaca2f297914ff307c Mon Sep 17 00:00:00 2001 From: liuh-80 Date: Fri, 17 Feb 2023 06:46:28 +0000 Subject: [PATCH 3/3] Improve code coverage --- sonic-db-cli/main.cpp | 29 +++++------------------------ sonic-db-cli/sonic-db-cli.cpp | 32 ++++++++++++++++++++++++++++++++ sonic-db-cli/sonic-db-cli.h | 6 ++++++ tests/cli_ut.cpp | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 24 deletions(-) diff --git a/sonic-db-cli/main.cpp b/sonic-db-cli/main.cpp index a15152d65..1ddf101d6 100755 --- a/sonic-db-cli/main.cpp +++ b/sonic-db-cli/main.cpp @@ -17,28 +17,9 @@ int main(int argc, char** argv) SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE); }; - - try - { - return sonic_db_cli( - argc, - argv, - initializeGlobalConfig, - initializeConfig); - } - catch (const exception& e) - { - // sonic-db-cli is porting from python version. - // in python version, when any exception happen sonic-db-cli will crash but will not generate core dump file. - // catch all exception here to avoid a core dump file generated. - cerr << "An exception of type " << e.what() << " occurred. Arguments:" << endl; - for (int idx = 0; idx < argc; idx++) - { - cerr << argv[idx] << " "; - } - cerr << endl; - - // when python version crash, exit code is 1. - return 1; - } + return cli_exception_wrapper( + argc, + argv, + initializeGlobalConfig, + initializeConfig); } \ No newline at end of file diff --git a/sonic-db-cli/sonic-db-cli.cpp b/sonic-db-cli/sonic-db-cli.cpp index f0a22cde7..51b916339 100755 --- a/sonic-db-cli/sonic-db-cli.cpp +++ b/sonic-db-cli/sonic-db-cli.cpp @@ -343,6 +343,38 @@ int sonic_db_cli( return 0; } + +int cli_exception_wrapper( + int argc, + char** argv, + function initializeGlobalConfig, + function initializeConfig) +{ + try + { + return sonic_db_cli( + argc, + argv, + initializeGlobalConfig, + initializeConfig); + } + catch (const exception& e) + { + // sonic-db-cli is porting from python version. + // in python version, when any exception happen sonic-db-cli will crash but will not generate core dump file. + // catch all exception here to avoid a core dump file generated. + cerr << "An exception of type " << e.what() << " occurred. Arguments:" << endl; + for (int idx = 0; idx < argc; idx++) + { + cerr << argv[idx] << " "; + } + cerr << endl; + + // when python version crash, exit code is 1. + return 1; + } +} + string getCommandName(vector& commands) { if (commands.size() == 0) diff --git a/sonic-db-cli/sonic-db-cli.h b/sonic-db-cli/sonic-db-cli.h index 810620b41..d1d874f8d 100755 --- a/sonic-db-cli/sonic-db-cli.h +++ b/sonic-db-cli/sonic-db-cli.h @@ -54,4 +54,10 @@ int sonic_db_cli( std::function initializeGlobalConfig, std::function initializeConfig); +int cli_exception_wrapper( + int argc, + char** argv, + std::function initializeGlobalConfig, + std::function initializeConfig); + std::string getCommandName(std::vector& command); \ No newline at end of file diff --git a/tests/cli_ut.cpp b/tests/cli_ut.cpp index 6782a95cc..39c80ceea 100755 --- a/tests/cli_ut.cpp +++ b/tests/cli_ut.cpp @@ -508,3 +508,35 @@ TEST(sonic_db_cli, test_parallel_cmd) { auto parallen_time = float( clock () - begin_time ); EXPECT_TRUE(parallen_time < sequential_time); } + +TEST(sonic_db_cli, test_cli_not_throw_exception) +{ + char *args[5]; + args[0] = "sonic-db-cli"; + args[1] = "TEST_DB"; + + // set key to test DB + args[2] = "SET"; + args[3] = "testkey"; + args[4] = "testvalue"; + + // data base file does not exist, will throw exception + auto initializeGlobalConfig = []() + { + throw std::system_error(); + }; + + auto initializeConfig = []() + { + throw std::system_error(); + }; + + optind = 0; + int exit_code = cli_exception_wrapper( + 5, + args, + initializeGlobalConfig, + initializeConfig); + + EXPECT_EQ(1, exit_code); +} \ No newline at end of file