From 954914e93220c8bf5c8491cb4fb1ffe12ccf5cb3 Mon Sep 17 00:00:00 2001 From: Ravin Perera <33562092+ravinsp@users.noreply.github.com> Date: Wed, 28 Apr 2021 14:37:26 +0530 Subject: [PATCH] Fixed get status peer address formatting crash. (#295) --- examples/js_client/hp-client-lib.js | 2 +- src/conf.hpp | 17 +++++++++++------ src/msg/bson/usrmsg_bson.cpp | 13 +++++++++---- src/msg/json/usrmsg_json.cpp | 16 +++++++++------- src/msg/usrmsg_common.hpp | 3 +++ 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/examples/js_client/hp-client-lib.js b/examples/js_client/hp-client-lib.js index 3e0880a3..6334d179 100644 --- a/examples/js_client/hp-client-lib.js +++ b/examples/js_client/hp-client-lib.js @@ -684,7 +684,7 @@ catch (e) { liblog(1, e); liblog(0, "Exception deserializing: "); - liblog(0, data || rcvd); + liblog(0, (data && (isTextMode ? data.toString() : data) || rcvd)); // If we get invalid message during handshake, close the socket. if (connectionStatus < 2) diff --git a/src/conf.hpp b/src/conf.hpp index 50cfb383..4d925332 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -15,7 +15,7 @@ namespace conf struct peer_ip_port { std::string host_address; - uint16_t port; + uint16_t port = 0; bool operator==(const peer_ip_port &other) const { @@ -31,6 +31,11 @@ namespace conf { return (host_address == other.host_address) ? port < other.port : host_address < other.host_address; } + + const std::string to_string() const + { + return host_address + ":" + std::to_string(port); + } }; // The role of the contract node. @@ -50,8 +55,8 @@ namespace conf // Max number of shards to keep for primary and raw shards. struct history_configuration { - uint64_t max_primary_shards; // Maximum number of shards for primary shards. - uint64_t max_raw_shards; // Maximum number of shards for raw data shards. + uint64_t max_primary_shards = 0; // Maximum number of shards for primary shards. + uint64_t max_raw_shards = 0; // Maximum number of shards for raw data shards. }; // Log severity levels used in Hot Pocket. @@ -68,8 +73,8 @@ namespace conf std::string loglevel; // Log severity level (debug, info, warn, error) LOG_SEVERITY loglevel_type; // Log severity level enum (debug, info, warn, error) std::unordered_set loggers; // List of enabled loggers (console, file) - size_t max_mbytes_per_file; // Max MB size of a single log file. - size_t max_file_count; // Max no. of log files to keep. + size_t max_mbytes_per_file = 0; // Max MB size of a single log file. + size_t max_file_count = 0; // Max no. of log files to keep. }; struct node_config @@ -189,7 +194,7 @@ namespace conf std::string tls_key_file; // Full path to the tls private key file. std::string tls_cert_file; // Full path to the tls certificate. - int config_fd; // Config file file descriptor. + int config_fd = -1; // Config file file descriptor. struct flock config_lock; // Config file lock. }; diff --git a/src/msg/bson/usrmsg_bson.cpp b/src/msg/bson/usrmsg_bson.cpp index 59724452..96c7a8bb 100644 --- a/src/msg/bson/usrmsg_bson.cpp +++ b/src/msg/bson/usrmsg_bson.cpp @@ -20,8 +20,6 @@ namespace msg::usrmsg::bson * "ledger_hash": * } */ - constexpr const size_t MAX_KNOWN_PEERS_INFO = 10; - void create_status_response(std::vector &msg, const uint64_t lcl_seq_no, std::string_view lcl_hash) { jsoncons::bson::bson_bytes_encoder encoder(msg); @@ -58,8 +56,15 @@ namespace msg::usrmsg::bson encoder.begin_array(); // Currently all peers, up to a max of 10 are sent regardless of state. - for (auto peer = p2p::ctx.peer_connections.begin(); peer != p2p::ctx.peer_connections.end() && count <= max_peers_count; peer++, count++) - encoder.string_value(peer->second->known_ipport->host_address + ":" + std::to_string(peer->second->known_ipport->port)); + for (auto peer = p2p::ctx.peer_connections.begin(); peer != p2p::ctx.peer_connections.end() && count <= max_peers_count; peer++) + { + const p2p::peer_comm_session *sess = peer->second; + if (sess->known_ipport) + { + encoder.string_value(sess->known_ipport->to_string()); + count++; + } + } encoder.end_array(); } diff --git a/src/msg/json/usrmsg_json.cpp b/src/msg/json/usrmsg_json.cpp index e232ba22..783ffceb 100644 --- a/src/msg/json/usrmsg_json.cpp +++ b/src/msg/json/usrmsg_json.cpp @@ -21,8 +21,6 @@ namespace msg::usrmsg::json constexpr const char *OPEN_SQR_BRACKET = "["; constexpr const char *CLOSE_SQR_BRACKET = "]"; - constexpr const size_t MAX_KNOWN_PEERS_INFO = 10; - // std::vector overload to concatonate string. std::vector &operator+=(std::vector &vec, std::string_view sv) { @@ -203,12 +201,16 @@ namespace msg::usrmsg::json size_t count = 1; // Currently all peers, up to a max of 10 are sent regardless of state. - for (auto peer = p2p::ctx.peer_connections.begin(); peer != p2p::ctx.peer_connections.end() && count <= max_peers_count; peer++, count++) + for (auto peer = p2p::ctx.peer_connections.begin(); peer != p2p::ctx.peer_connections.end() && count <= max_peers_count; peer++) { - msg += DOUBLE_QUOTE + peer->second->known_ipport->host_address + ":" + std::to_string(peer->second->known_ipport->port) + DOUBLE_QUOTE; - - if (peer != p2p::ctx.peer_connections.end() && count < max_peers_count) - msg += ","; + const p2p::peer_comm_session *sess = peer->second; + if (sess->known_ipport) + { + if (count > 1) + msg += ","; + msg += DOUBLE_QUOTE + sess->known_ipport->to_string() + DOUBLE_QUOTE; + count++; + } } } diff --git a/src/msg/usrmsg_common.hpp b/src/msg/usrmsg_common.hpp index ed381bac..4117fce7 100644 --- a/src/msg/usrmsg_common.hpp +++ b/src/msg/usrmsg_common.hpp @@ -8,6 +8,9 @@ namespace msg::usrmsg // Length of user random challenge bytes. constexpr size_t CHALLENGE_LEN = 16; + // Max no. of known peers to return in get status. + constexpr const size_t MAX_KNOWN_PEERS_INFO = 10; + // Message field names constexpr const char *FLD_HP_VERSION = "hp_version"; constexpr const char *FLD_TYPE = "type";