Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent sonic-db-cli generate core dump #749

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sonic-db-cli/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "sonic-db-cli.h"
#include "common/dbconnector.h"
#include <iostream>

using namespace swss;
using namespace std;
Expand All @@ -16,7 +17,7 @@ int main(int argc, char** argv)
SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE);
};

return sonic_db_cli(
return cli_exception_wrapper(
argc,
argv,
initializeGlobalConfig,
Expand Down
32 changes: 32 additions & 0 deletions sonic-db-cli/sonic-db-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,38 @@ int sonic_db_cli(
return 0;
}


int cli_exception_wrapper(
int argc,
char** argv,
function<void()> initializeGlobalConfig,
function<void()> 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<string>& commands)
{
if (commands.size() == 0)
Expand Down
6 changes: 6 additions & 0 deletions sonic-db-cli/sonic-db-cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ int sonic_db_cli(
std::function<void()> initializeGlobalConfig,
std::function<void()> initializeConfig);

int cli_exception_wrapper(
int argc,
char** argv,
std::function<void()> initializeGlobalConfig,
std::function<void()> initializeConfig);

std::string getCommandName(std::vector<std::string>& command);
32 changes: 32 additions & 0 deletions tests/cli_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}