From 53cb56394ae3d02b912eada16f3fbe630e57aa65 Mon Sep 17 00:00:00 2001 From: xianju6x Date: Wed, 15 Feb 2023 13:49:32 +0800 Subject: [PATCH] fix the issues scanned by Coverity Signed-off-by: xianju6x --- src/guest/aaf.cc | 3 ++- src/guest/config_parser.cc | 4 +--- src/guest/tui.cc | 4 ++++ src/guest/vm_builder.h | 2 +- src/guest/vm_flash.cc | 7 +++---- src/guest/vm_process.cc | 7 +++++++ src/host/app_launcher/app_launcher_client.cc | 3 +-- src/services/client.cc | 11 ++++++++++- src/services/server.h | 2 +- src/utils/utils.cc | 17 ++++++++++++++--- src/vm_manager.cc | 4 ++-- 11 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/guest/aaf.cc b/src/guest/aaf.cc index ccdb0f44..d5bd2ce4 100644 --- a/src/guest/aaf.cc +++ b/src/guest/aaf.cc @@ -57,7 +57,8 @@ void Aaf::Flush(void) { if (tmp.empty()) continue; std::string key = tmp.substr(0, tmp.find(":")); - + if (key.length() == 0) + continue; auto find = data_.find(key); if ((find != data_.end()) && !find->second.empty()) { out << key << ":" << find->second << "\n"; diff --git a/src/guest/config_parser.cc b/src/guest/config_parser.cc index 6bac5852..b8f6f2e7 100644 --- a/src/guest/config_parser.cc +++ b/src/guest/config_parser.cc @@ -53,9 +53,7 @@ bool CivConfig::SanitizeOpts(void) { if (group != kConfigMap.end()) { for (auto& subsec : section.second) { auto key = std::find(group->second.begin(), group->second.end(), subsec.first); - if (key != group->second.end()) { - continue; - } else { + if (key == group->second.end()) { LOG(error) << "Invalid key: " << section.first << "." << subsec.first << "\n"; return false; } diff --git a/src/guest/tui.cc b/src/guest/tui.cc index 60f61ede..bb69d885 100644 --- a/src/guest/tui.cc +++ b/src/guest/tui.cc @@ -274,7 +274,11 @@ void CivTui::SetConfToPtree() { std::string passthrough; for (std::vector::iterator iter = pt_pci_disp_.begin(); iter != pt_pci_disp_.end(); iter++) { std::string selectItem = *iter; + if (selectItem.length() < 0) + continue; size_t pos = selectItem.find(" "); + if (pos < 0) + continue; passthrough += selectItem.substr(0, pos); if (iter != (pt_pci_disp_.end() - 1)) { passthrough += ','; diff --git a/src/guest/vm_builder.h b/src/guest/vm_builder.h index c9c64b92..11c1e470 100644 --- a/src/guest/vm_builder.h +++ b/src/guest/vm_builder.h @@ -36,7 +36,7 @@ class VmBuilder { }; public: - explicit VmBuilder(std::string name) : name_(name) {} + explicit VmBuilder(std::string name) : name_(name), vsock_cid_(0) {} virtual ~VmBuilder() = default; virtual bool BuildVmArgs(void) = 0; virtual void StartVm(void) = 0; diff --git a/src/guest/vm_flash.cc b/src/guest/vm_flash.cc index 6b64018c..83e2647f 100644 --- a/src/guest/vm_flash.cc +++ b/src/guest/vm_flash.cc @@ -269,15 +269,14 @@ bool VmFlasher::FlashGuest(std::string path) { if (!cfg_.ReadConfigFile(p.string())) { LOG(error) << "Failed to read config file"; - return -1; + return false; } std::string emul_type = cfg_.GetValue(kGroupEmul, kEmulType); - if (emul_type.compare(kEmulTypeQemu) == 0) { - return FlashWithQemu(); - } else { + if ((emul_type.compare(kEmulTypeQemu) == 0) || emul_type.empty()) { return FlashWithQemu(); } + return false; } } // namespace vm_manager diff --git a/src/guest/vm_process.cc b/src/guest/vm_process.cc index abcab5b1..9d6a9a3b 100644 --- a/src/guest/vm_process.cc +++ b/src/guest/vm_process.cc @@ -31,12 +31,19 @@ void VmProcSimple::ThreadMon(void) { boost::process::environment env; for (std::string s : env_data_) { + if (s.length() == 0) + continue; env.set(s.substr(0, s.find('=')), s.substr(s.find('=') + 1)); } LOG(info) << "CMD: " << cmd_; size_t exe_pos_begin = cmd_.find_first_not_of(' '); + if (exe_pos_begin == std::string::npos) + return; + size_t exe_pos_end = cmd_.find_first_of(' ', exe_pos_begin); + if (exe_pos_end == std::string::npos) + exe_pos_end = cmd_.size(); std::string exe = cmd_.substr(exe_pos_begin, exe_pos_end - exe_pos_begin); std::string tid = boost::lexical_cast(mon_->get_id()); diff --git a/src/host/app_launcher/app_launcher_client.cc b/src/host/app_launcher/app_launcher_client.cc index 903bbb07..ae4e0095 100644 --- a/src/host/app_launcher/app_launcher_client.cc +++ b/src/host/app_launcher/app_launcher_client.cc @@ -75,12 +75,11 @@ class AppLauncherOptions final { return true; } - uint32_t cid = -1; if (vm_.count("cid") != 1) { PrintHelp(); return false; } - cid = vm_["cid"].as(); + uint32_t cid = vm_["cid"].as(); std::string app_name; if (vm_.count("name") != 1) { diff --git a/src/services/client.cc b/src/services/client.cc index 362f7bce..6baf712e 100644 --- a/src/services/client.cc +++ b/src/services/client.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -43,6 +44,9 @@ void Client::PrepareStartGuestClientShm(const char *path) { [env.size()] (client_shm_.get_segment_manager()); + if (!var_env) + return; + for (std::string s : env._data) { var_env->assign(s.c_str()); var_env++; @@ -73,6 +77,7 @@ CivVmInfo Client::GetCivVmInfo(const char *vm_name) { CivVmInfo *vm_info = client_shm_.find_or_construct ("VmInfo") (0, VmBuilder::VmState::kVmUnknown); + assert(vm_info != nullptr); return std::move(*vm_info); } @@ -89,8 +94,12 @@ bool Client::Notify(CivMsgType t) { (kCivServerObjData) (); + if (!data) { + return false; + } data->type = t; - strncpy(data->payload, client_shm_name_.c_str(), sizeof(data->payload)); + strncpy(data->payload, client_shm_name_.c_str(), sizeof(data->payload) - 1); + data->payload[sizeof(data->payload) - 1] = '\0'; boost::interprocess::scoped_lock lock_cond(sync.first->mutex_cond); sync.first->cond_s.notify_one(); diff --git a/src/services/server.h b/src/services/server.h index 8fb51905..5289e0f3 100644 --- a/src/services/server.h +++ b/src/services/server.h @@ -59,7 +59,7 @@ class Server final { bool stop_server_ = false; - CivMsgSync *sync_; + CivMsgSync *sync_ = nullptr; std::vector> vmis_; diff --git a/src/utils/utils.cc b/src/utils/utils.cc index da61b22a..43dccbfa 100644 --- a/src/utils/utils.cc +++ b/src/utils/utils.cc @@ -48,13 +48,22 @@ const char *GetConfigPath(void) { } if (sudo_uid) { - euid = atoi(sudo_gid); + euid = atoi(sudo_uid); setresuid(ruid, euid, suid); } civ_config_path = new char[MAX_PATH]; memset(civ_config_path, 0, MAX_PATH); - snprintf(civ_config_path, MAX_PATH, "%s%s", getpwuid(euid)->pw_dir, "/.intel/.civ"); + + struct passwd pwd; + struct passwd *ppwd = &pwd; + struct passwd *presult = NULL; + char buffer[4096]; + ret = getpwuid_r(euid, ppwd, buffer, sizeof(buffer), &presult); + if (ret != 0 || presult == NULL) + return NULL; + + snprintf(civ_config_path, MAX_PATH, "%s%s", pwd.pw_dir, "/.intel/.civ"); if (!boost::filesystem::exists(civ_config_path)) { if (!boost::filesystem::create_directories(civ_config_path)) { delete[] civ_config_path; @@ -106,8 +115,10 @@ int Daemonize(void) { close(STDERR_FILENO); int fd = open("/dev/null", O_RDWR); - if (fd != STDIN_FILENO) + if (fd != STDIN_FILENO) { + close(fd); return -1; + } if (dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO) return -1; if (dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) diff --git a/src/vm_manager.cc b/src/vm_manager.cc index f3bac1ad..d3616b50 100644 --- a/src/vm_manager.cc +++ b/src/vm_manager.cc @@ -51,7 +51,7 @@ static bool ListGuest(void) { } auto vm_list = c.GetGuestLists(); std::cout << "=====================" << std::endl; - for (auto it : vm_list) { + for (auto& it : vm_list) { std::cout << it << std::endl; } return true; @@ -70,7 +70,7 @@ static int GetGuestState(std::string name) { LOG(error) << "List guest: " << " Failed!"; } auto vm_list = c.GetGuestLists(); - for (auto it : vm_list) { + for (auto& it : vm_list) { std::vector sp; boost::split(sp, it, boost::is_any_of(":")); if (sp.size() != 2)