From 1245da52b2ebdb51552048d8c4e2aab0ff86e414 Mon Sep 17 00:00:00 2001 From: ravinsp <33562092+ravinsp@users.noreply.github.com> Date: Sat, 26 Sep 2020 23:03:10 +0530 Subject: [PATCH] Fixed configuration logic issues. --- src/conf.cpp | 13 +++++++++---- src/main.cpp | 9 ++++----- src/util.cpp | 11 +++++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/conf.cpp b/src/conf.cpp index ad97fd48..acb794f7 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -247,12 +247,16 @@ namespace conf const char *ipport_concat = v.as(); // Split the address:port text into two util::split_string(splitted_peers, ipport_concat, ":"); - if (splitted_peers.size() == 2) + + // Push the peer address and the port to peers set + if (splitted_peers.size() != 2) { - // Push the peer address and the port to peers set - cfg.peers.emplace(std::make_pair(splitted_peers.front(), std::stoi(splitted_peers.back()))); - splitted_peers.clear(); + std::cout << "Invalid peer: " << ipport_concat << "\n"; + return -1; } + + cfg.peers.emplace(std::make_pair(splitted_peers.front(), std::stoi(splitted_peers.back()))); + splitted_peers.clear(); } cfg.unl.clear(); @@ -379,6 +383,7 @@ namespace conf catch (const std::exception &e) { std::cout << "Writing to config file failed. " << ctx.config_file << std::endl; + ofs.close(); return -1; } ofs.close(); diff --git a/src/main.cpp b/src/main.cpp index 0af79615..c35b06b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,12 +67,12 @@ int parse_cmd(int argc, char **argv) */ void deinit() { - usr::deinit(); - p2p::deinit(); cons::deinit(); state_sync::deinit(); state_serve::deinit(); read_req::deinit(); + usr::deinit(); + p2p::deinit(); hpfs::deinit(); } @@ -190,9 +190,8 @@ int main(int argc, char **argv) << (conf::cfg.startup_mode == conf::OPERATING_MODE::OBSERVER ? "Observer" : "Proposer"); LOG_INFO << "Public key: " << conf::cfg.pubkeyhex.substr(2); // Public key without 'ed' prefix. - if (hpfs::init() != 0 || read_req::init() != 0 || - state_serve::init() != 0 || state_sync::init() != 0 || cons::init() != 0 || - p2p::init() != 0 || usr::init() != 0) + if (hpfs::init() != 0 || p2p::init() != 0 || usr::init() != 0 || read_req::init() != 0 || + state_serve::init() != 0 || state_sync::init() != 0 || cons::init() != 0) { deinit(); return -1; diff --git a/src/util.cpp b/src/util.cpp index 4de6b0fa..dfe1cc1d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -380,18 +380,21 @@ namespace util if (str.empty()) return; - size_t start = 0U; + size_t start = 0; size_t end = str.find(delimeter); while (end != std::string::npos) { - collection.push_back(std::string(str.substr(start, end - start))); + // Do not add empty strings. + if (start != end) + collection.push_back(std::string(str.substr(start, end - start))); start = end + delimeter.length(); end = str.find(delimeter, start); } - if (collection.empty()) - collection.push_back(std::string(str)); + // If there are any leftover from the source string add the remaining. + if (start < str.size()) + collection.push_back(std::string(str.substr(start))); } } // namespace util