diff options
author | Kyungwook Tak <k.tak@samsung.com> | 2016-03-11 17:13:54 +0900 |
---|---|---|
committer | Kyungwook Tak <k.tak@samsung.com> | 2016-03-11 17:17:50 +0900 |
commit | 7ca6ec72345061a12575dd9bac4ef4db866d6622 (patch) | |
tree | 130a6f4c01196f07510788c06215b11cfe96d867 /tools | |
parent | 753a5219058dd3ffa11b0f213a7badb22b7a8e3e (diff) | |
download | key-manager-7ca6ec72345061a12575dd9bac4ef4db866d6622.tar.gz key-manager-7ca6ec72345061a12575dd9bac4ef4db866d6622.tar.bz2 key-manager-7ca6ec72345061a12575dd9bac4ef4db866d6622.zip |
Fix SVACE defects
Use thread-safe functions
Initialize values in constructor
Catch all exceptions
Change-Id: I7ce649b7ba1a11e45949e8f8fca257be4eb7f37d
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/ckm_db_tool/ckm_db_tool.cpp | 122 | ||||
-rw-r--r-- | tools/ckm_so_loader.cpp | 61 |
2 files changed, 96 insertions, 87 deletions
diff --git a/tools/ckm_db_tool/ckm_db_tool.cpp b/tools/ckm_db_tool/ckm_db_tool.cpp index 378ff9ed..ae5087e1 100644 --- a/tools/ckm_db_tool/ckm_db_tool.cpp +++ b/tools/ckm_db_tool/ckm_db_tool.cpp @@ -118,13 +118,13 @@ void DbWrapper::process(const string& acmd) displayRow(row, trim); } } catch (const DB::SqlConnection::Exception::Base& e) { - cout << e.GetMessage() << endl; + cerr << e.GetMessage() << endl; } catch (const Exc::Exception &e) { - cout << e.message() << endl; + cerr << e.message() << endl; } catch (const std::exception &e) { - cout << e.what() << endl; + cerr << e.what() << endl; } catch (...) { - cout << "Unexpected exception occurred" << endl; + cerr << "Unexpected exception occurred" << endl; } } @@ -166,70 +166,76 @@ void internalHelp() { int main(int argc, char* argv[]) { - if(argc < 2 || !argv[1]) { - usage(); - return -1; - } + try { + if (argc < 2 || !argv[1]) { + usage(); + return -1; + } - // read uid - stringstream ss(argv[1]); - uid_t uid; - if(!(ss >> uid)) { - usage(); - return -1; - } + // read uid + stringstream ss(argv[1]); + uid_t uid; + if (!(ss >> uid)) { + usage(); + return -1; + } - int idx = 2; + int idx = 2; - // read password - Password pass; - if(uid >= 5000) { - if(argc > idx) { - pass = argv[idx]; - idx++; + // read password + Password pass; + if (uid >= 5000) { + if (argc > idx) { + pass = argv[idx]; + idx++; + } } - } - // read sqlite3 command - string argcmd; - if(argc > idx) - argcmd = argv[idx]; + // read sqlite3 command + string argcmd; + if (argc > idx) + argcmd = argv[idx]; + + // unlock db + DbWrapper dbw(uid, pass); + int retCode = dbw.unlock(); + if (retCode != CKM_API_SUCCESS ) { + cerr << "Unlocking database failed: " << retCode << endl; + return -1; + } + cout << "Database unlocked" << endl; + + while (true) { + string cmd; + if (argcmd.empty()) { + cout << "> "; + if(!getline(cin, cmd)) { + cout << "exit" << endl; + break; // EOF + } + } else { + cmd = argcmd; + } - // unlock db - DbWrapper dbw(uid, pass); - int retCode = dbw.unlock(); - if (retCode != CKM_API_SUCCESS ) { - cout << "Unlocking database failed: " << retCode << endl; - return -1; - } - cout << "Database unlocked" << endl; - - for(;;) { - string cmd; - if (argcmd.empty()) { - cout << "> "; - if(!getline(cin, cmd)) { - cout << "exit" << endl; - break; // EOF + if(cmd == "exit") + break; + if(cmd == "help") { + internalHelp(); + continue; } - } else { - cmd = argcmd; - } - if(cmd == "exit") - break; - if(cmd == "help") { - internalHelp(); - continue; + dbw.process(cmd); + + if(!argcmd.empty()) + break; } - dbw.process(cmd); + dbw.lock(); + cout << "Database locked" << endl; - if(!argcmd.empty()) - break; + return 0; + } catch (...) { + cerr << "Unexpected exception occurred" << endl; + return -1; } - dbw.lock(); - cout << "Database locked" << endl; - - return 0; } diff --git a/tools/ckm_so_loader.cpp b/tools/ckm_so_loader.cpp index a39adb69..085c7367 100644 --- a/tools/ckm_so_loader.cpp +++ b/tools/ckm_so_loader.cpp @@ -43,7 +43,7 @@ void clear_cache() sync(); ofstream of("/proc/sys/vm/drop_caches"); if (of.bad()) { - cerr << "Cache clearing failed: " << strerror(errno) << endl; + cerr << "Cache clearing failed with errno: " << errno << endl; return; } of << "3"; @@ -89,36 +89,39 @@ int main(int argc, char* argv[]) return -1; } - int flags = stoi(argv[1]); // let it throw - int repeats = stoi(argv[2]); // let it throw - string so_path(argv[3]); - string symbol(argv[4]); + try { + int flags = stoi(argv[1]); // let it throw + int repeats = stoi(argv[2]); // let it throw + string so_path(argv[3]); + string symbol(argv[4]); - cout << "dlopen[us];dlsym[us]" << endl; - for (int cnt = 0 ; cnt < repeats; cnt++) - { - /* - * It has to be a different process each time. Glibc somehow caches the library information - * and consecutive calls are faster - */ - pid_t pid = fork(); - if (pid < 0) { - cerr << "fork failed: " << strerror(errno) << endl; - return -1; - } - if (pid == 0) { - test(flags, so_path, symbol); - exit(0); - } - else - { - int status; - pid_t ret = waitpid(pid,&status, 0); - if (ret != pid) { - cerr << "waitpid failed: " << strerror(errno) << endl; - exit(1); + cout << "dlopen[us];dlsym[us]" << endl; + for (int cnt = 0 ; cnt < repeats; cnt++) { + /* + * It has to be a different process each time. Glibc somehow caches the library information + * and consecutive calls are faster + */ + pid_t pid = fork(); + if (pid < 0) { + cerr << "fork failed with errno: " << errno << endl; + return -1; + } else if (pid == 0) { + test(flags, so_path, symbol); + exit(0); + } else { + + int status; + pid_t ret = waitpid(pid, &status, 0); + if (ret != pid) { + cerr << "waitpid failed with errno: " << errno << endl; + exit(1); + } } } + + return 0; + } catch (...) { + cerr << "Unexpected exception occured" << endl; + return -1; } - return 0; } |