Skip to content

Commit e732ed0

Browse files
liuh-80StormLiangMS
authored andcommitted
Prevent sonic-db-cli generate core dump (#749)
#### Why I did it 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. but in c++ version crash will generate a core dump. Fix sonic-db-cli to avoid a core dump file generated. #### How I did it Catch all exception in sonic-db-cli to avoid core dump generated. #### How to verify it Pass all UT and E2E test cases. #### Which release branch to backport (provide reason below if selected) <!-- - Note we only backport fixes to a release branch, *not* features! - Please also provide a reason for the backporting below. - e.g. - [x] 202006 --> - [ ] 201811 - [ ] 201911 - [ ] 202006 - [ ] 202012 - [ ] 202106 - [ ] 202111 #### Description for the changelog Fix sonic-db-cli to avoid a core dump file generated. #### Link to config_db schema for YANG module changes <!-- Provide a link to config_db schema for the table for which YANG model is defined Link should point to correct section on https://github.com/Azure/SONiC/wiki/Configuration. --> #### A picture of a cute animal (not mandatory but encouraged)
1 parent 28adcb4 commit e732ed0

4 files changed

Lines changed: 72 additions & 1 deletion

File tree

sonic-db-cli/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "sonic-db-cli.h"
22
#include "common/dbconnector.h"
3+
#include <iostream>
34

45
using namespace swss;
56
using namespace std;
@@ -16,7 +17,7 @@ int main(int argc, char** argv)
1617
SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE);
1718
};
1819

19-
return sonic_db_cli(
20+
return cli_exception_wrapper(
2021
argc,
2122
argv,
2223
initializeGlobalConfig,

sonic-db-cli/sonic-db-cli.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,38 @@ int sonic_db_cli(
343343
return 0;
344344
}
345345

346+
347+
int cli_exception_wrapper(
348+
int argc,
349+
char** argv,
350+
function<void()> initializeGlobalConfig,
351+
function<void()> initializeConfig)
352+
{
353+
try
354+
{
355+
return sonic_db_cli(
356+
argc,
357+
argv,
358+
initializeGlobalConfig,
359+
initializeConfig);
360+
}
361+
catch (const exception& e)
362+
{
363+
// sonic-db-cli is porting from python version.
364+
// in python version, when any exception happen sonic-db-cli will crash but will not generate core dump file.
365+
// catch all exception here to avoid a core dump file generated.
366+
cerr << "An exception of type " << e.what() << " occurred. Arguments:" << endl;
367+
for (int idx = 0; idx < argc; idx++)
368+
{
369+
cerr << argv[idx] << " ";
370+
}
371+
cerr << endl;
372+
373+
// when python version crash, exit code is 1.
374+
return 1;
375+
}
376+
}
377+
346378
string getCommandName(vector<string>& commands)
347379
{
348380
if (commands.size() == 0)

sonic-db-cli/sonic-db-cli.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ int sonic_db_cli(
5454
std::function<void()> initializeGlobalConfig,
5555
std::function<void()> initializeConfig);
5656

57+
int cli_exception_wrapper(
58+
int argc,
59+
char** argv,
60+
std::function<void()> initializeGlobalConfig,
61+
std::function<void()> initializeConfig);
62+
5763
std::string getCommandName(std::vector<std::string>& command);

tests/cli_ut.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,35 @@ TEST(sonic_db_cli, test_parallel_cmd) {
508508
auto parallen_time = float( clock () - begin_time );
509509
EXPECT_TRUE(parallen_time < sequential_time);
510510
}
511+
512+
TEST(sonic_db_cli, test_cli_not_throw_exception)
513+
{
514+
char *args[5];
515+
args[0] = "sonic-db-cli";
516+
args[1] = "TEST_DB";
517+
518+
// set key to test DB
519+
args[2] = "SET";
520+
args[3] = "testkey";
521+
args[4] = "testvalue";
522+
523+
// data base file does not exist, will throw exception
524+
auto initializeGlobalConfig = []()
525+
{
526+
throw std::system_error();
527+
};
528+
529+
auto initializeConfig = []()
530+
{
531+
throw std::system_error();
532+
};
533+
534+
optind = 0;
535+
int exit_code = cli_exception_wrapper(
536+
5,
537+
args,
538+
initializeGlobalConfig,
539+
initializeConfig);
540+
541+
EXPECT_EQ(1, exit_code);
542+
}

0 commit comments

Comments
 (0)