From 573815da837a8d97d53f019fa1d629e4be722227 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 09:57:46 +0200 Subject: [PATCH 01/22] Problem: protected data member in encoder_base_t Solution: encapsulate data member properly --- src/encoder.hpp | 21 +++++++++++---------- src/raw_encoder.cpp | 2 +- src/v1_encoder.cpp | 8 ++++---- src/v2_encoder.cpp | 10 +++++----- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/encoder.hpp b/src/encoder.hpp index 93b654bd..aac27515 100644 --- a/src/encoder.hpp +++ b/src/encoder.hpp @@ -54,14 +54,14 @@ namespace zmq template class encoder_base_t : public i_encoder { public: - inline encoder_base_t (size_t bufsize_) : + inline explicit encoder_base_t (size_t bufsize_) : _write_pos (0), _to_write (0), _next (NULL), _new_msg_flag (false), _buf_size (bufsize_), _buf (static_cast (malloc (bufsize_))), - in_progress (NULL) + _in_progress (NULL) { alloc_assert (_buf); } @@ -78,7 +78,7 @@ template class encoder_base_t : public i_encoder unsigned char *buffer = !*data_ ? _buf : *data_; size_t buffersize = !*data_ ? _buf_size : size_; - if (in_progress == NULL) + if (in_progress () == NULL) return 0; size_t pos = 0; @@ -88,11 +88,11 @@ template class encoder_base_t : public i_encoder // in the buffer. if (!_to_write) { if (_new_msg_flag) { - int rc = in_progress->close (); + int rc = _in_progress->close (); errno_assert (rc == 0); - rc = in_progress->init (); + rc = _in_progress->init (); errno_assert (rc == 0); - in_progress = NULL; + _in_progress = NULL; break; } (static_cast (this)->*_next) (); @@ -130,8 +130,8 @@ template class encoder_base_t : public i_encoder void load_msg (msg_t *msg_) { - zmq_assert (in_progress == NULL); - in_progress = msg_; + zmq_assert (in_progress () == NULL); + _in_progress = msg_; (static_cast (this)->*_next) (); } @@ -152,6 +152,8 @@ template class encoder_base_t : public i_encoder _new_msg_flag = new_msg_flag_; } + msg_t *in_progress () { return _in_progress; } + private: // Where to get the data to write from. unsigned char *_write_pos; @@ -172,8 +174,7 @@ template class encoder_base_t : public i_encoder encoder_base_t (const encoder_base_t &); void operator= (const encoder_base_t &); - protected: - msg_t *in_progress; + msg_t *_in_progress; }; } diff --git a/src/raw_encoder.cpp b/src/raw_encoder.cpp index fa510963..39c7633e 100644 --- a/src/raw_encoder.cpp +++ b/src/raw_encoder.cpp @@ -45,6 +45,6 @@ zmq::raw_encoder_t::~raw_encoder_t () void zmq::raw_encoder_t::raw_message_ready () { - next_step (in_progress->data (), in_progress->size (), + next_step (in_progress ()->data (), in_progress ()->size (), &raw_encoder_t::raw_message_ready, true); } diff --git a/src/v1_encoder.cpp b/src/v1_encoder.cpp index cc78d417..8ef22ae7 100644 --- a/src/v1_encoder.cpp +++ b/src/v1_encoder.cpp @@ -49,14 +49,14 @@ zmq::v1_encoder_t::~v1_encoder_t () void zmq::v1_encoder_t::size_ready () { // Write message body into the buffer. - next_step (in_progress->data (), in_progress->size (), + next_step (in_progress ()->data (), in_progress ()->size (), &v1_encoder_t::message_ready, true); } void zmq::v1_encoder_t::message_ready () { // Get the message size. - size_t size = in_progress->size (); + size_t size = in_progress ()->size (); // Account for the 'flags' byte. size++; @@ -66,12 +66,12 @@ void zmq::v1_encoder_t::message_ready () // message size. In both cases 'flags' field follows. if (size < UCHAR_MAX) { _tmpbuf[0] = static_cast (size); - _tmpbuf[1] = (in_progress->flags () & msg_t::more); + _tmpbuf[1] = (in_progress ()->flags () & msg_t::more); next_step (_tmpbuf, 2, &v1_encoder_t::size_ready, false); } else { _tmpbuf[0] = UCHAR_MAX; put_uint64 (_tmpbuf + 1, size); - _tmpbuf[9] = (in_progress->flags () & msg_t::more); + _tmpbuf[9] = (in_progress ()->flags () & msg_t::more); next_step (_tmpbuf, 10, &v1_encoder_t::size_ready, false); } } diff --git a/src/v2_encoder.cpp b/src/v2_encoder.cpp index 1a869068..88f005a9 100644 --- a/src/v2_encoder.cpp +++ b/src/v2_encoder.cpp @@ -52,17 +52,17 @@ void zmq::v2_encoder_t::message_ready () // Encode flags. unsigned char &protocol_flags = _tmp_buf[0]; protocol_flags = 0; - if (in_progress->flags () & msg_t::more) + if (in_progress ()->flags () & msg_t::more) protocol_flags |= v2_protocol_t::more_flag; - if (in_progress->size () > UCHAR_MAX) + if (in_progress ()->size () > UCHAR_MAX) protocol_flags |= v2_protocol_t::large_flag; - if (in_progress->flags () & msg_t::command) + if (in_progress ()->flags () & msg_t::command) protocol_flags |= v2_protocol_t::command_flag; // Encode the message length. For messages less then 256 bytes, // the length is encoded as 8-bit unsigned integer. For larger // messages, 64-bit unsigned integer in network byte order is used. - const size_t size = in_progress->size (); + const size_t size = in_progress ()->size (); if (unlikely (size > UCHAR_MAX)) { put_uint64 (_tmp_buf + 1, size); next_step (_tmp_buf, 9, &v2_encoder_t::size_ready, false); @@ -75,6 +75,6 @@ void zmq::v2_encoder_t::message_ready () void zmq::v2_encoder_t::size_ready () { // Write message body into the buffer. - next_step (in_progress->data (), in_progress->size (), + next_step (in_progress ()->data (), in_progress ()->size (), &v2_encoder_t::message_ready, true); } From 314ac28dbdc96e7f7a572a90cd537d9fd3549e45 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 10:03:55 +0200 Subject: [PATCH 02/22] Problem: protected data member in ip_resolver_t Solution: declare private --- src/ip_resolver.hpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/ip_resolver.hpp b/src/ip_resolver.hpp index 161f8885..8089ebe6 100644 --- a/src/ip_resolver.hpp +++ b/src/ip_resolver.hpp @@ -88,6 +88,17 @@ class ip_resolver_t int resolve (ip_addr_t *ip_addr_, const char *name_); protected: + // Virtual functions that are overridden in tests + virtual int do_getaddrinfo (const char *node_, + const char *service_, + const struct addrinfo *hints_, + struct addrinfo **res_); + + virtual void do_freeaddrinfo (struct addrinfo *res_); + + virtual unsigned int do_if_nametoindex (const char *ifname_); + + private: ip_resolver_options_t _options; int resolve_nic_name (ip_addr_t *ip_addr_, const char *nic_); @@ -97,16 +108,6 @@ class ip_resolver_t int get_interface_name (unsigned long index_, char **dest_) const; int wchar_to_utf8 (const WCHAR *src_, char **dest_) const; #endif - - // Virtual functions that are overriden in tests - virtual int do_getaddrinfo (const char *node_, - const char *service_, - const struct addrinfo *hints_, - struct addrinfo **res_); - - virtual void do_freeaddrinfo (struct addrinfo *res_); - - virtual unsigned int do_if_nametoindex (const char *ifname_); }; } From 273137741ae35e0ae99a791490dcd1354c2e667b Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 10:28:14 +0200 Subject: [PATCH 03/22] Problem: protected data members in ip_address_t, ip_address_mask_t violates LSP Solution: make ip_address_mask_t independent of ip_address_t, they do not share that much, remove some code duplication between ip_address_t and ip_addr_t --- src/tcp_address.cpp | 71 +++++++++++++++++++++------------------------ src/tcp_address.hpp | 10 +++---- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/src/tcp_address.cpp b/src/tcp_address.cpp index 8003d925..447e53b7 100644 --- a/src/tcp_address.cpp +++ b/src/tcp_address.cpp @@ -71,10 +71,6 @@ zmq::tcp_address_t::tcp_address_t (const sockaddr *sa_, socklen_t sa_len_) : memcpy (&_address.ipv6, sa_, sizeof (_address.ipv6)); } -zmq::tcp_address_t::~tcp_address_t () -{ -} - int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv6_) { // Test the ';' to know if we have a source address in name_ @@ -117,7 +113,7 @@ int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv6_) return resolver.resolve (&_address, name_); } -int zmq::tcp_address_t::to_string (std::string &addr_) +int zmq::tcp_address_t::to_string (std::string &addr_) const { if (_address.family () != AF_INET && _address.family () != AF_INET6) { addr_.clear (); @@ -127,8 +123,8 @@ int zmq::tcp_address_t::to_string (std::string &addr_) // Not using service resolving because of // https://github.com/zeromq/libzmq/commit/1824574f9b5a8ce786853320e3ea09fe1f822bc4 char hbuf[NI_MAXHOST]; - int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof (hbuf), NULL, 0, - NI_NUMERICHOST); + const int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof (hbuf), NULL, + 0, NI_NUMERICHOST); if (rc != 0) { addr_.clear (); return rc; @@ -148,28 +144,22 @@ int zmq::tcp_address_t::to_string (std::string &addr_) const sockaddr *zmq::tcp_address_t::addr () const { - return &_address.generic; + return _address.as_sockaddr (); } socklen_t zmq::tcp_address_t::addrlen () const { - if (_address.generic.sa_family == AF_INET6) - return static_cast (sizeof (_address.ipv6)); - - return static_cast (sizeof (_address.ipv4)); + return _address.sockaddr_len (); } const sockaddr *zmq::tcp_address_t::src_addr () const { - return &_source_address.generic; + return _source_address.as_sockaddr (); } socklen_t zmq::tcp_address_t::src_addrlen () const { - if (_address.family () == AF_INET6) - return static_cast (sizeof (_source_address.ipv6)); - - return static_cast (sizeof (_source_address.ipv4)); + return _source_address.sockaddr_len (); } bool zmq::tcp_address_t::has_src_addr () const @@ -186,10 +176,9 @@ sa_family_t zmq::tcp_address_t::family () const return _address.family (); } -zmq::tcp_address_mask_t::tcp_address_mask_t () : - tcp_address_t (), - _address_mask (-1) +zmq::tcp_address_mask_t::tcp_address_mask_t () : _address_mask (-1) { + memset (&_network_address, 0, sizeof (_network_address)); } int zmq::tcp_address_mask_t::mask () const @@ -224,23 +213,26 @@ int zmq::tcp_address_mask_t::resolve (const char *name_, bool ipv6_) ip_resolver_t resolver (resolver_opts); - const int rc = resolver.resolve (&_address, addr_str.c_str ()); + const int rc = resolver.resolve (&_network_address, addr_str.c_str ()); if (rc != 0) return rc; // Parse the cidr mask number. - const int full_mask_ipv4 = sizeof (_address.ipv4.sin_addr) * CHAR_BIT; - const int full_mask_ipv6 = sizeof (_address.ipv6.sin6_addr) * CHAR_BIT; + const int full_mask_ipv4 = + sizeof (_network_address.ipv4.sin_addr) * CHAR_BIT; + const int full_mask_ipv6 = + sizeof (_network_address.ipv6.sin6_addr) * CHAR_BIT; if (mask_str.empty ()) { - _address_mask = - _address.family () == AF_INET6 ? full_mask_ipv6 : full_mask_ipv4; + _address_mask = _network_address.family () == AF_INET6 ? full_mask_ipv6 + : full_mask_ipv4; } else if (mask_str == "0") _address_mask = 0; else { const int mask = atoi (mask_str.c_str ()); if ((mask < 1) - || (_address.family () == AF_INET6 && mask > full_mask_ipv6) - || (_address.family () != AF_INET6 && mask > full_mask_ipv4)) { + || (_network_address.family () == AF_INET6 && mask > full_mask_ipv6) + || (_network_address.family () != AF_INET6 + && mask > full_mask_ipv4)) { errno = EINVAL; return -1; } @@ -250,9 +242,10 @@ int zmq::tcp_address_mask_t::resolve (const char *name_, bool ipv6_) return 0; } -int zmq::tcp_address_mask_t::to_string (std::string &addr_) +int zmq::tcp_address_mask_t::to_string (std::string &addr_) const { - if (_address.family () != AF_INET && _address.family () != AF_INET6) { + if (_network_address.family () != AF_INET + && _network_address.family () != AF_INET6) { addr_.clear (); return -1; } @@ -262,14 +255,15 @@ int zmq::tcp_address_mask_t::to_string (std::string &addr_) } char hbuf[NI_MAXHOST]; - int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof (hbuf), NULL, 0, - NI_NUMERICHOST); + const int rc = getnameinfo (_network_address.as_sockaddr (), + _network_address.sockaddr_len (), hbuf, + sizeof (hbuf), NULL, 0, NI_NUMERICHOST); if (rc != 0) { addr_.clear (); return rc; } - if (_address.family () == AF_INET6) { + if (_network_address.family () == AF_INET6) { std::stringstream s; s << "[" << hbuf << "]/" << _address_mask; addr_ = s.str (); @@ -285,9 +279,10 @@ bool zmq::tcp_address_mask_t::match_address (const struct sockaddr *ss_, const socklen_t ss_len_) const { zmq_assert (_address_mask != -1 && ss_ != NULL - && ss_len_ >= (socklen_t) sizeof (struct sockaddr)); + && ss_len_ + >= static_cast (sizeof (struct sockaddr))); - if (ss_->sa_family != _address.generic.sa_family) + if (ss_->sa_family != _network_address.generic.sa_family) return false; if (_address_mask > 0) { @@ -298,15 +293,15 @@ bool zmq::tcp_address_mask_t::match_address (const struct sockaddr *ss_, their_bytes = reinterpret_cast ( &((reinterpret_cast (ss_)) ->sin6_addr)); - our_bytes = - reinterpret_cast (&_address.ipv6.sin6_addr); + our_bytes = reinterpret_cast ( + &_network_address.ipv6.sin6_addr); mask = sizeof (struct in6_addr) * 8; } else { zmq_assert (ss_len_ == sizeof (struct sockaddr_in)); their_bytes = reinterpret_cast (&( (reinterpret_cast (ss_))->sin_addr)); - our_bytes = - reinterpret_cast (&_address.ipv4.sin_addr); + our_bytes = reinterpret_cast ( + &_network_address.ipv4.sin_addr); mask = sizeof (struct in_addr) * 8; } if (_address_mask < mask) diff --git a/src/tcp_address.hpp b/src/tcp_address.hpp index 44c47a2d..78c15132 100644 --- a/src/tcp_address.hpp +++ b/src/tcp_address.hpp @@ -44,7 +44,6 @@ class tcp_address_t public: tcp_address_t (); tcp_address_t (const sockaddr *sa_, socklen_t sa_len_); - virtual ~tcp_address_t (); // This function translates textual TCP address into an address // structure. If 'local' is true, names are resolved as local interface @@ -53,7 +52,7 @@ class tcp_address_t int resolve (const char *name_, bool local_, bool ipv6_); // The opposite to resolve() - virtual int to_string (std::string &addr_); + int to_string (std::string &addr_) const; #if defined ZMQ_HAVE_WINDOWS unsigned short family () const; @@ -67,13 +66,13 @@ class tcp_address_t socklen_t src_addrlen () const; bool has_src_addr () const; - protected: + private: ip_addr_t _address; ip_addr_t _source_address; bool _has_src_addr; }; -class tcp_address_mask_t : public tcp_address_t +class tcp_address_mask_t { public: tcp_address_mask_t (); @@ -84,7 +83,7 @@ class tcp_address_mask_t : public tcp_address_t int resolve (const char *name_, bool ipv6_); // The opposite to resolve() - int to_string (std::string &addr_); + int to_string (std::string &addr_) const; int mask () const; @@ -92,6 +91,7 @@ class tcp_address_mask_t : public tcp_address_t const socklen_t ss_len_) const; private: + ip_addr_t _network_address; int _address_mask; }; } From a96a87f34300b5a586ed952d2a34a3d48cf84742 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 10:35:01 +0200 Subject: [PATCH 04/22] Problem: problematic atoi function is used (CERT ERR-34C) Solution: use strtol instead --- src/tcp_address.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tcp_address.cpp b/src/tcp_address.cpp index 447e53b7..01583e10 100644 --- a/src/tcp_address.cpp +++ b/src/tcp_address.cpp @@ -228,7 +228,7 @@ int zmq::tcp_address_mask_t::resolve (const char *name_, bool ipv6_) } else if (mask_str == "0") _address_mask = 0; else { - const int mask = atoi (mask_str.c_str ()); + const long mask = strtol (mask_str.c_str (), NULL, 10); if ((mask < 1) || (_network_address.family () == AF_INET6 && mask > full_mask_ipv6) || (_network_address.family () != AF_INET6 @@ -236,7 +236,7 @@ int zmq::tcp_address_mask_t::resolve (const char *name_, bool ipv6_) errno = EINVAL; return -1; } - _address_mask = mask; + _address_mask = static_cast (mask); } return 0; From bd76926f5b5d64e1c10172e23545a0559ea75d1a Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 13:13:31 +0200 Subject: [PATCH 05/22] Problem: code style issues in options_t (C-style cast, suboptimal std::string::find call, redundant method is_valid) Solution: resolved these issues --- src/options.cpp | 38 +++++++++++++++++++------------------- src/options.hpp | 2 -- src/socket_base.cpp | 5 ----- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/options.cpp b/src/options.cpp index 33f392de..d8d40324 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -183,16 +183,20 @@ int do_setsockopt_set (const void *const optval_, if (optvallen_ == 0 && optval_ == NULL) { set_->clear (); return 0; - } else if (optvallen_ == sizeof (T) && optval_ != NULL) { - set_->insert (*((const T *) optval_)); + } + if (optvallen_ == sizeof (T) && optval_ != NULL) { + set_->insert (*(static_cast (optval_))); return 0; } return sockopt_invalid (); } +// TODO why is 1000 a sensible default? +const int default_hwm = 1000; + zmq::options_t::options_t () : - sndhwm (1000), - rcvhwm (1000), + sndhwm (default_hwm), + rcvhwm (default_hwm), affinity (0), routing_id_size (0), rate (100), @@ -321,7 +325,7 @@ int zmq::options_t::setsockopt (int option_, case ZMQ_ROUTING_ID: // Routing id is any binary string from 1 to 255 octets - if (optvallen_ > 0 && optvallen_ < 256) { + if (optvallen_ > 0 && optvallen_ <= UCHAR_MAX) { routing_id_size = static_cast (optvallen_); memcpy (routing_id, optval_, routing_id_size); return 0; @@ -538,7 +542,8 @@ int zmq::options_t::setsockopt (int option_, if (optvallen_ == 0 && optval_ == NULL) { mechanism = ZMQ_NULL; return 0; - } else if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) { + } else if (optvallen_ > 0 && optvallen_ <= UCHAR_MAX + && optval_ != NULL) { plain_username.assign (static_cast (optval_), optvallen_); as_server = 0; @@ -551,7 +556,8 @@ int zmq::options_t::setsockopt (int option_, if (optvallen_ == 0 && optval_ == NULL) { mechanism = ZMQ_NULL; return 0; - } else if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) { + } else if (optvallen_ > 0 && optvallen_ <= UCHAR_MAX + && optval_ != NULL) { plain_password.assign (static_cast (optval_), optvallen_); as_server = 0; @@ -610,7 +616,7 @@ int zmq::options_t::setsockopt (int option_, break; case ZMQ_GSSAPI_PRINCIPAL: - if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) { + if (optvallen_ > 0 && optvallen_ <= UCHAR_MAX && optval_ != NULL) { gss_principal.assign ((const char *) optval_, optvallen_); mechanism = ZMQ_GSSAPI; return 0; @@ -618,7 +624,7 @@ int zmq::options_t::setsockopt (int option_, break; case ZMQ_GSSAPI_SERVICE_PRINCIPAL: - if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) { + if (optvallen_ > 0 && optvallen_ <= UCHAR_MAX && optval_ != NULL) { gss_service_principal.assign ((const char *) optval_, optvallen_); mechanism = ZMQ_GSSAPI; @@ -721,11 +727,11 @@ int zmq::options_t::setsockopt (int option_, case ZMQ_METADATA: if (optvallen_ > 0 && !is_int) { - const std::string s (reinterpret_cast (optval_)); - const size_t pos = s.find (":"); + const std::string s (static_cast (optval_)); + const size_t pos = s.find (':'); if (pos != std::string::npos && pos != 0 && pos != s.length () - 1) { - std::string key = s.substr (0, pos); + const std::string key = s.substr (0, pos); if (key.compare (0, 2, "X-") == 0 && key.length () <= UCHAR_MAX) { std::string val = s.substr (pos + 1, s.length ()); @@ -773,7 +779,7 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_) const { - bool is_int = (*optvallen_ == sizeof (int)); + const bool is_int = (*optvallen_ == sizeof (int)); int *value = static_cast (optval_); #if defined(ZMQ_ACT_MILITANT) bool malformed = true; // Did caller pass a bad option value? @@ -1156,9 +1162,3 @@ int zmq::options_t::getsockopt (int option_, errno = EINVAL; return -1; } - -bool zmq::options_t::is_valid (int option_) const -{ - LIBZMQ_UNUSED (option_); - return true; -} diff --git a/src/options.hpp b/src/options.hpp index bf7f4e69..8cf08c72 100644 --- a/src/options.hpp +++ b/src/options.hpp @@ -69,8 +69,6 @@ struct options_t int setsockopt (int option_, const void *optval_, size_t optvallen_); int getsockopt (int option_, void *optval_, size_t *optvallen_) const; - bool is_valid (int option_) const; - // High-water marks for message pipes. int sndhwm; int rcvhwm; diff --git a/src/socket_base.cpp b/src/socket_base.cpp index f9bd64ad..f8657f33 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -364,11 +364,6 @@ int zmq::socket_base_t::setsockopt (int option_, { scoped_optional_lock_t sync_lock (_thread_safe ? &_sync : NULL); - if (!options.is_valid (option_)) { - errno = EINVAL; - return -1; - } - if (unlikely (_ctx_terminated)) { errno = ETERM; return -1; From 0179b7577affeeb492a9765c17a0f31c25d06a77 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 13:22:34 +0200 Subject: [PATCH 06/22] Problem: literals protocol names used at various places Solution: introduced named constants --- src/address.cpp | 20 ++++++++++---------- src/address.hpp | 17 +++++++++++++++++ src/ipc_connecter.cpp | 2 +- src/session_base.cpp | 15 ++++++++------- src/socket_base.cpp | 37 +++++++++++++++++++------------------ src/socks_connecter.cpp | 2 +- src/tcp_connecter.cpp | 2 +- src/zmq.cpp | 8 ++++---- 8 files changed, 61 insertions(+), 42 deletions(-) diff --git a/src/address.cpp b/src/address.cpp index ed98cdca..80d9697b 100644 --- a/src/address.cpp +++ b/src/address.cpp @@ -56,33 +56,33 @@ zmq::address_t::address_t (const std::string &protocol_, zmq::address_t::~address_t () { - if (protocol == "tcp") { + if (protocol == protocol_name::tcp) { if (resolved.tcp_addr) { LIBZMQ_DELETE (resolved.tcp_addr); } } - if (protocol == "udp") { + if (protocol == protocol_name::udp) { if (resolved.udp_addr) { LIBZMQ_DELETE (resolved.udp_addr); } } #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ && !defined ZMQ_HAVE_VXWORKS - else if (protocol == "ipc") { + else if (protocol == protocol_name::ipc) { if (resolved.ipc_addr) { LIBZMQ_DELETE (resolved.ipc_addr); } } #endif #if defined ZMQ_HAVE_TIPC - else if (protocol == "tipc") { + else if (protocol == protocol_name::tipc) { if (resolved.tipc_addr) { LIBZMQ_DELETE (resolved.tipc_addr); } } #endif #if defined ZMQ_HAVE_VMCI - else if (protocol == "vmci") { + else if (protocol == protocol_name::vmci) { if (resolved.vmci_addr) { LIBZMQ_DELETE (resolved.vmci_addr); } @@ -92,29 +92,29 @@ zmq::address_t::~address_t () int zmq::address_t::to_string (std::string &addr_) const { - if (protocol == "tcp") { + if (protocol == protocol_name::tcp) { if (resolved.tcp_addr) return resolved.tcp_addr->to_string (addr_); } - if (protocol == "udp") { + if (protocol == protocol_name::udp) { if (resolved.udp_addr) return resolved.udp_addr->to_string (addr_); } #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ && !defined ZMQ_HAVE_VXWORKS - else if (protocol == "ipc") { + else if (protocol == protocol_name::ipc) { if (resolved.ipc_addr) return resolved.ipc_addr->to_string (addr_); } #endif #if defined ZMQ_HAVE_TIPC - else if (protocol == "tipc") { + else if (protocol == protocol_name::tipc) { if (resolved.tipc_addr) return resolved.tipc_addr->to_string (addr_); } #endif #if defined ZMQ_HAVE_VMCI - else if (protocol == "vmci") { + else if (protocol == protocol_name::vmci) { if (resolved.vmci_addr) return resolved.vmci_addr->to_string (addr_); } diff --git a/src/address.hpp b/src/address.hpp index 61b6d717..bcc5c549 100644 --- a/src/address.hpp +++ b/src/address.hpp @@ -46,6 +46,23 @@ class tipc_address_t; #if defined ZMQ_HAVE_VMCI class vmci_address_t; #endif + +namespace protocol_name +{ +static const char tcp[] = "tcp"; +static const char udp[] = "udp"; +#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ + && !defined ZMQ_HAVE_VXWORKS +static const char ipc[] = "ipc"; +#endif +#if defined ZMQ_HAVE_TIPC +static const char tipc[] = "tipc"; +#endif +#if defined ZMQ_HAVE_VMCI +static const char vmci[] = "vmci"; +#endif +} + struct address_t { address_t (const std::string &protocol_, diff --git a/src/ipc_connecter.cpp b/src/ipc_connecter.cpp index 7109caa2..9e472108 100644 --- a/src/ipc_connecter.cpp +++ b/src/ipc_connecter.cpp @@ -67,7 +67,7 @@ zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_, current_reconnect_ivl (options.reconnect_ivl) { zmq_assert (addr); - zmq_assert (addr->protocol == "ipc"); + zmq_assert (addr->protocol == protocol_name::ipc); addr->to_string (endpoint); socket = session->get_socket (); } diff --git a/src/session_base.cpp b/src/session_base.cpp index 8137dcb7..db899eab 100644 --- a/src/session_base.cpp +++ b/src/session_base.cpp @@ -515,7 +515,7 @@ void zmq::session_base_t::reconnect () // and reestablish later on if (_pipe && options.immediate == 1 && _addr->protocol != "pgm" && _addr->protocol != "epgm" && _addr->protocol != "norm" - && _addr->protocol != "udp") { + && _addr->protocol != protocol_name::udp) { _pipe->hiccup (); _pipe->terminate (false); _terminating_pipes.insert (_pipe); @@ -557,10 +557,11 @@ void zmq::session_base_t::start_connecting (bool wait_) // Create the connecter object. - if (_addr->protocol == "tcp") { + if (_addr->protocol == protocol_name::tcp) { if (!options.socks_proxy_address.empty ()) { address_t *proxy_address = new (std::nothrow) - address_t ("tcp", options.socks_proxy_address, this->get_ctx ()); + address_t (protocol_name::tcp, options.socks_proxy_address, + this->get_ctx ()); alloc_assert (proxy_address); socks_connecter_t *connecter = new (std::nothrow) socks_connecter_t (io_thread, this, options, _addr, proxy_address, @@ -578,7 +579,7 @@ void zmq::session_base_t::start_connecting (bool wait_) #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ && !defined ZMQ_HAVE_VXWORKS - if (_addr->protocol == "ipc") { + if (_addr->protocol == protocol_name::ipc) { ipc_connecter_t *connecter = new (std::nothrow) ipc_connecter_t (io_thread, this, options, _addr, wait_); alloc_assert (connecter); @@ -587,7 +588,7 @@ void zmq::session_base_t::start_connecting (bool wait_) } #endif #if defined ZMQ_HAVE_TIPC - if (_addr->protocol == "tipc") { + if (_addr->protocol == protocol_name::tipc) { tipc_connecter_t *connecter = new (std::nothrow) tipc_connecter_t (io_thread, this, options, _addr, wait_); alloc_assert (connecter); @@ -596,7 +597,7 @@ void zmq::session_base_t::start_connecting (bool wait_) } #endif - if (_addr->protocol == "udp") { + if (_addr->protocol == protocol_name::udp) { zmq_assert (options.type == ZMQ_DISH || options.type == ZMQ_RADIO || options.type == ZMQ_DGRAM); @@ -698,7 +699,7 @@ void zmq::session_base_t::start_connecting (bool wait_) #endif // ZMQ_HAVE_NORM #if defined ZMQ_HAVE_VMCI - if (_addr->protocol == "vmci") { + if (_addr->protocol == protocol_name::vmci) { vmci_connecter_t *connecter = new (std::nothrow) vmci_connecter_t (io_thread, this, options, _addr, wait_); alloc_assert (connecter); diff --git a/src/socket_base.cpp b/src/socket_base.cpp index f8657f33..a0884930 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -295,9 +295,9 @@ int zmq::socket_base_t::check_protocol (const std::string &protocol_) if (protocol_ != "inproc" #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ && !defined ZMQ_HAVE_VXWORKS - && protocol_ != "ipc" + && protocol_ != protocol_name::ipc #endif - && protocol_ != "tcp" + && protocol_ != protocol_name::tcp #if defined ZMQ_HAVE_OPENPGM // pgm/epgm transports only available if 0MQ is compiled with OpenPGM. && protocol_ != "pgm" @@ -305,15 +305,15 @@ int zmq::socket_base_t::check_protocol (const std::string &protocol_) #endif #if defined ZMQ_HAVE_TIPC // TIPC transport is only available on Linux. - && protocol_ != "tipc" + && protocol_ != protocol_name::tipc #endif #if defined ZMQ_HAVE_NORM && protocol_ != "norm" #endif #if defined ZMQ_HAVE_VMCI - && protocol_ != "vmci" + && protocol_ != protocol_name::vmci #endif - && protocol_ != "udp") { + && protocol_ != protocol_name::udp) { errno = EPROTONOSUPPORT; return -1; } @@ -330,7 +330,7 @@ int zmq::socket_base_t::check_protocol (const std::string &protocol_) } #endif - if (protocol_ == "udp" + if (protocol_ == protocol_name::udp && (options.type != ZMQ_DISH && options.type != ZMQ_RADIO && options.type != ZMQ_DGRAM)) { errno = ENOCOMPATPROTO; @@ -511,7 +511,7 @@ int zmq::socket_base_t::bind (const char *addr_) return rc; } - if (protocol == "udp") { + if (protocol == protocol_name::udp) { if (!(options.type == ZMQ_DGRAM || options.type == ZMQ_DISH)) { errno = ENOCOMPATPROTO; return -1; @@ -575,7 +575,7 @@ int zmq::socket_base_t::bind (const char *addr_) return -1; } - if (protocol == "tcp") { + if (protocol == protocol_name::tcp) { tcp_listener_t *listener = new (std::nothrow) tcp_listener_t (io_thread, this, options); alloc_assert (listener); @@ -596,7 +596,7 @@ int zmq::socket_base_t::bind (const char *addr_) #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ && !defined ZMQ_HAVE_VXWORKS - if (protocol == "ipc") { + if (protocol == protocol_name::ipc) { ipc_listener_t *listener = new (std::nothrow) ipc_listener_t (io_thread, this, options); alloc_assert (listener); @@ -616,7 +616,7 @@ int zmq::socket_base_t::bind (const char *addr_) } #endif #if defined ZMQ_HAVE_TIPC - if (protocol == "tipc") { + if (protocol == protocol_name::tipc) { tipc_listener_t *listener = new (std::nothrow) tipc_listener_t (io_thread, this, options); alloc_assert (listener); @@ -636,7 +636,7 @@ int zmq::socket_base_t::bind (const char *addr_) } #endif #if defined ZMQ_HAVE_VMCI - if (protocol == "vmci") { + if (protocol == protocol_name::vmci) { vmci_listener_t *listener = new (std::nothrow) vmci_listener_t (io_thread, this, options); alloc_assert (listener); @@ -809,7 +809,7 @@ int zmq::socket_base_t::connect (const char *addr_) alloc_assert (paddr); // Resolve address (if needed by the protocol) - if (protocol == "tcp") { + if (protocol == protocol_name::tcp) { // Do some basic sanity checks on tcp:// address syntax // - hostname starts with digit or letter, with embedded '-' or '.' // - IPv6 address may contain hex chars and colons. @@ -853,7 +853,7 @@ int zmq::socket_base_t::connect (const char *addr_) } #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ && !defined ZMQ_HAVE_VXWORKS - else if (protocol == "ipc") { + else if (protocol == protocol_name::ipc) { paddr->resolved.ipc_addr = new (std::nothrow) ipc_address_t (); alloc_assert (paddr->resolved.ipc_addr); int rc = paddr->resolved.ipc_addr->resolve (address.c_str ()); @@ -864,7 +864,7 @@ int zmq::socket_base_t::connect (const char *addr_) } #endif - if (protocol == "udp") { + if (protocol == protocol_name::udp) { if (options.type != ZMQ_RADIO) { errno = ENOCOMPATPROTO; LIBZMQ_DELETE (paddr); @@ -897,7 +897,7 @@ int zmq::socket_base_t::connect (const char *addr_) } #endif #if defined ZMQ_HAVE_TIPC - else if (protocol == "tipc") { + else if (protocol == protocol_name::tipc) { paddr->resolved.tipc_addr = new (std::nothrow) tipc_address_t (); alloc_assert (paddr->resolved.tipc_addr); int rc = paddr->resolved.tipc_addr->resolve (address.c_str ()); @@ -917,7 +917,7 @@ int zmq::socket_base_t::connect (const char *addr_) } #endif #if defined ZMQ_HAVE_VMCI - else if (protocol == "vmci") { + else if (protocol == protocol_name::vmci) { paddr->resolved.vmci_addr = new (std::nothrow) vmci_address_t (this->get_ctx ()); alloc_assert (paddr->resolved.vmci_addr); @@ -937,7 +937,8 @@ int zmq::socket_base_t::connect (const char *addr_) // PGM does not support subscription forwarding; ask for all data to be // sent to this pipe. (same for NORM, currently?) bool subscribe_to_all = protocol == "pgm" || protocol == "epgm" - || protocol == "norm" || protocol == "udp"; + || protocol == "norm" + || protocol == protocol_name::udp; pipe_t *newpipe = NULL; if (options.immediate != 1 || subscribe_to_all) { @@ -1039,7 +1040,7 @@ int zmq::socket_base_t::term_endpoint (const char *addr_) // IPv4-in-IPv6 mapping (EG: tcp://[::ffff:127.0.0.1]:9999), so try to // resolve before giving up. Given at this stage we don't know whether a // socket is connected or bound, try with both. - if (protocol == "tcp") { + if (protocol == protocol_name::tcp) { if (_endpoints.find (resolved_addr) == _endpoints.end ()) { tcp_address_t *tcp_addr = new (std::nothrow) tcp_address_t (); alloc_assert (tcp_addr); diff --git a/src/socks_connecter.cpp b/src/socks_connecter.cpp index 91bcc423..99cbc3df 100644 --- a/src/socks_connecter.cpp +++ b/src/socks_connecter.cpp @@ -72,7 +72,7 @@ zmq::socks_connecter_t::socks_connecter_t (class io_thread_t *io_thread_, _current_reconnect_ivl (options.reconnect_ivl) { zmq_assert (_addr); - zmq_assert (_addr->protocol == "tcp"); + zmq_assert (_addr->protocol == protocol_name::tcp); _proxy_addr->to_string (_endpoint); _socket = _session->get_socket (); } diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp index 8886a3d6..9ab5647f 100644 --- a/src/tcp_connecter.cpp +++ b/src/tcp_connecter.cpp @@ -82,7 +82,7 @@ zmq::tcp_connecter_t::tcp_connecter_t (class io_thread_t *io_thread_, _socket (_session->get_socket ()) { zmq_assert (_addr); - zmq_assert (_addr->protocol == "tcp"); + zmq_assert (_addr->protocol == protocol_name::tcp); _addr->to_string (_endpoint); // TODO the return value is unused! what if it fails? if this is impossible // or does not matter, change such that endpoint in initialized using an diff --git a/src/zmq.cpp b/src/zmq.cpp index 5e765f43..43423549 100644 --- a/src/zmq.cpp +++ b/src/zmq.cpp @@ -87,10 +87,10 @@ struct iovec #include "msg.hpp" #include "fd.hpp" #include "metadata.hpp" -#include "signaler.hpp" #include "socket_poller.hpp" #include "timers.hpp" #include "ip.hpp" +#include "address.hpp" #if defined ZMQ_HAVE_OPENPGM #define __PGM_WININT_H__ @@ -1489,7 +1489,7 @@ int zmq_device (int /* type */, void *frontend_, void *backend_) int zmq_has (const char *capability_) { #if !defined(ZMQ_HAVE_WINDOWS) && !defined(ZMQ_HAVE_OPENVMS) - if (strcmp (capability_, "ipc") == 0) + if (strcmp (capability_, zmq::protocol_name::ipc) == 0) return true; #endif #if defined(ZMQ_HAVE_OPENPGM) @@ -1497,7 +1497,7 @@ int zmq_has (const char *capability_) return true; #endif #if defined(ZMQ_HAVE_TIPC) - if (strcmp (capability_, "tipc") == 0) + if (strcmp (capability_, zmq::protocol_name::tipc) == 0) return true; #endif #if defined(ZMQ_HAVE_NORM) @@ -1513,7 +1513,7 @@ int zmq_has (const char *capability_) return true; #endif #if defined(ZMQ_HAVE_VMCI) - if (strcmp (capability_, "vmci") == 0) + if (strcmp (capability_, zmq::protocol_name::vmci) == 0) return true; #endif #if defined(ZMQ_BUILD_DRAFT_API) From 9c8fde09ca93d7e5eb4ffd03366b61195cbd43e5 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 13:30:19 +0200 Subject: [PATCH 07/22] Problem: inconsistent control structures in address.cpp, redundant check for NULLness before delete Solution: harmonize, remove redundant checks --- src/address.cpp | 53 +++++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/src/address.cpp b/src/address.cpp index 80d9697b..bc406b14 100644 --- a/src/address.cpp +++ b/src/address.cpp @@ -57,67 +57,46 @@ zmq::address_t::address_t (const std::string &protocol_, zmq::address_t::~address_t () { if (protocol == protocol_name::tcp) { - if (resolved.tcp_addr) { - LIBZMQ_DELETE (resolved.tcp_addr); - } - } - if (protocol == protocol_name::udp) { - if (resolved.udp_addr) { - LIBZMQ_DELETE (resolved.udp_addr); - } + LIBZMQ_DELETE (resolved.tcp_addr); + } else if (protocol == protocol_name::udp) { + LIBZMQ_DELETE (resolved.udp_addr); } #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ && !defined ZMQ_HAVE_VXWORKS else if (protocol == protocol_name::ipc) { - if (resolved.ipc_addr) { - LIBZMQ_DELETE (resolved.ipc_addr); - } + LIBZMQ_DELETE (resolved.ipc_addr); } #endif #if defined ZMQ_HAVE_TIPC else if (protocol == protocol_name::tipc) { - if (resolved.tipc_addr) { - LIBZMQ_DELETE (resolved.tipc_addr); - } + LIBZMQ_DELETE (resolved.tipc_addr); } #endif #if defined ZMQ_HAVE_VMCI else if (protocol == protocol_name::vmci) { - if (resolved.vmci_addr) { - LIBZMQ_DELETE (resolved.vmci_addr); - } + LIBZMQ_DELETE (resolved.vmci_addr); } #endif } int zmq::address_t::to_string (std::string &addr_) const { - if (protocol == protocol_name::tcp) { - if (resolved.tcp_addr) - return resolved.tcp_addr->to_string (addr_); - } - if (protocol == protocol_name::udp) { - if (resolved.udp_addr) - return resolved.udp_addr->to_string (addr_); - } + if (protocol == protocol_name::tcp && resolved.tcp_addr) + return resolved.tcp_addr->to_string (addr_); + if (protocol == protocol_name::udp && resolved.udp_addr) + return resolved.udp_addr->to_string (addr_); #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ && !defined ZMQ_HAVE_VXWORKS - else if (protocol == protocol_name::ipc) { - if (resolved.ipc_addr) - return resolved.ipc_addr->to_string (addr_); - } + if (protocol == protocol_name::ipc && resolved.ipc_addr) + return resolved.ipc_addr->to_string (addr_); #endif #if defined ZMQ_HAVE_TIPC - else if (protocol == protocol_name::tipc) { - if (resolved.tipc_addr) - return resolved.tipc_addr->to_string (addr_); - } + if (protocol == protocol_name::tipc && resolved.tipc_addr) + return resolved.tipc_addr->to_string (addr_); #endif #if defined ZMQ_HAVE_VMCI - else if (protocol == protocol_name::vmci) { - if (resolved.vmci_addr) - return resolved.vmci_addr->to_string (addr_); - } + if (protocol == protocol_name::vmci && resolved.vmci_addr) + return resolved.vmci_addr->to_string (addr_); #endif if (!protocol.empty () && !address.empty ()) { From 93194e7c538b0132a955bc68e19c2533fa17c935 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 13:32:29 +0200 Subject: [PATCH 08/22] Problem: non-portable memset Solution: add dummy pointer meber to union instead --- src/address.cpp | 2 +- src/address.hpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/address.cpp b/src/address.cpp index bc406b14..9b559890 100644 --- a/src/address.cpp +++ b/src/address.cpp @@ -51,7 +51,7 @@ zmq::address_t::address_t (const std::string &protocol_, address (address_), parent (parent_) { - memset (&resolved, 0, sizeof resolved); + resolved.dummy = NULL; } zmq::address_t::~address_t () diff --git a/src/address.hpp b/src/address.hpp index bcc5c549..1275278e 100644 --- a/src/address.hpp +++ b/src/address.hpp @@ -76,8 +76,10 @@ struct address_t ctx_t *const parent; // Protocol specific resolved address + // All members must be pointers to allow for consistent initialization union { + void *dummy; tcp_address_t *tcp_addr; udp_address_t *udp_addr; #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS \ From 0dce223341d45a3fb2b899fbfe05baf8b760aa7e Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:03:19 +0200 Subject: [PATCH 09/22] Problem: no check if noexcept is supported by compiler Solution: add compile check --- CMakeLists.txt | 1 + builds/cmake/Modules/ZMQSourceRunChecks.cmake | 18 ++++++++++++++++++ builds/cmake/platform.hpp.in | 2 ++ 3 files changed, 21 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c754976..082bce5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -415,6 +415,7 @@ if (CMAKE_SYSTEM_NAME MATCHES "SunOS" OR CMAKE_SYSTEM_NAME MATCHES "NetBSD") endif () endif () +zmq_check_noexcept () #----------------------------------------------------------------------------- if (NOT CMAKE_CROSSCOMPILING AND NOT MSVC) diff --git a/builds/cmake/Modules/ZMQSourceRunChecks.cmake b/builds/cmake/Modules/ZMQSourceRunChecks.cmake index 78cedbea..47653a6d 100644 --- a/builds/cmake/Modules/ZMQSourceRunChecks.cmake +++ b/builds/cmake/Modules/ZMQSourceRunChecks.cmake @@ -293,3 +293,21 @@ int main (int argc, char *argv []) " ZMQ_HAVE_GETRANDOM) endmacro() + +macro(zmq_check_noexcept) + message(STATUS "Checking whether noexcept is supported") + check_cxx_source_compiles( +" +struct X +{ + X(int i) noexcept {} +}; + +int main(int argc, char *argv []) +{ + X x(5); + return 0; +} +" + ZMQ_HAVE_NOEXCEPT) +endmacro() diff --git a/builds/cmake/platform.hpp.in b/builds/cmake/platform.hpp.in index 8c8e6cf2..4a910423 100644 --- a/builds/cmake/platform.hpp.in +++ b/builds/cmake/platform.hpp.in @@ -19,6 +19,8 @@ #cmakedefine HAVE_MKDTEMP #cmakedefine ZMQ_HAVE_UIO +#cmakedefine ZMQ_HAVE_NOEXCEPT + #cmakedefine ZMQ_HAVE_EVENTFD #cmakedefine ZMQ_HAVE_EVENTFD_CLOEXEC #cmakedefine ZMQ_HAVE_IFADDRS From a91c7e718cf9de649768ee13dbb7d79747046497 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:04:17 +0200 Subject: [PATCH 10/22] Problem: warnings on violations of CERT ERR-58 Solution: declare functions noexcept --- src/atomic_counter.hpp | 23 ++++++++++++++++------- src/atomic_ptr.hpp | 34 +++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/atomic_counter.hpp b/src/atomic_counter.hpp index 1b0cc58f..a1d3655f 100644 --- a/src/atomic_counter.hpp +++ b/src/atomic_counter.hpp @@ -66,6 +66,14 @@ #include #endif +#if !defined ZMQ_NOEXCEPT +#if defined ZMQ_HAVE_NOEXCEPT +#define ZMQ_NOEXCEPT noexcept +#else +#define ZMQ_NOEXCEPT +#endif +#endif + namespace zmq { // This class represents an integer that can be incremented/decremented @@ -90,15 +98,16 @@ class atomic_counter_t public: typedef uint32_t integer_t; - inline atomic_counter_t (integer_t value_ = 0) : _value (value_) {} - - inline ~atomic_counter_t () {} + inline atomic_counter_t (integer_t value_ = 0) ZMQ_NOEXCEPT + : _value (value_) + { + } // Set counter _value (not thread-safe). - inline void set (integer_t value_) { _value = value_; } + inline void set (integer_t value_) ZMQ_NOEXCEPT { _value = value_; } // Atomic addition. Returns the old _value. - inline integer_t add (integer_t increment_) + inline integer_t add (integer_t increment_) ZMQ_NOEXCEPT { integer_t old_value; @@ -143,7 +152,7 @@ class atomic_counter_t } // Atomic subtraction. Returns false if the counter drops to zero. - inline bool sub (integer_t decrement_) + inline bool sub (integer_t decrement_) ZMQ_NOEXCEPT { #if defined ZMQ_ATOMIC_COUNTER_WINDOWS LONG delta = -((LONG) decrement_); @@ -198,7 +207,7 @@ class atomic_counter_t #endif } - inline integer_t get () const { return _value; } + inline integer_t get () const ZMQ_NOEXCEPT { return _value; } private: #if defined ZMQ_ATOMIC_COUNTER_CXX11 diff --git a/src/atomic_ptr.hpp b/src/atomic_ptr.hpp index 5b85073c..9940dae1 100644 --- a/src/atomic_ptr.hpp +++ b/src/atomic_ptr.hpp @@ -64,6 +64,14 @@ #include #endif +#if !defined ZMQ_NOEXCEPT +#if defined ZMQ_HAVE_NOEXCEPT +#define ZMQ_NOEXCEPT noexcept +#else +#define ZMQ_NOEXCEPT +#endif +#endif + namespace zmq { #if !defined ZMQ_ATOMIC_PTR_CXX11 @@ -73,7 +81,7 @@ inline void *atomic_xchg_ptr (void **ptr_, , mutex_t &_sync #endif -) + ) ZMQ_NOEXCEPT { #if defined ZMQ_ATOMIC_PTR_WINDOWS return InterlockedExchangePointer ((PVOID *) ptr_, val_); @@ -120,7 +128,7 @@ inline void *atomic_cas (void *volatile *ptr_, , mutex_t &_sync #endif -) + ) ZMQ_NOEXCEPT { #if defined ZMQ_ATOMIC_PTR_WINDOWS return InterlockedCompareExchangePointer ((volatile PVOID *) ptr_, val_, @@ -176,19 +184,16 @@ template class atomic_ptr_t { public: // Initialise atomic pointer - inline atomic_ptr_t () { _ptr = NULL; } - - // Destroy atomic pointer - inline ~atomic_ptr_t () {} + inline atomic_ptr_t () ZMQ_NOEXCEPT { _ptr = NULL; } // Set value of atomic pointer in a non-threadsafe way // Use this function only when you are sure that at most one // thread is accessing the pointer at the moment. - inline void set (T *ptr_) { _ptr = ptr_; } + inline void set (T *ptr_) ZMQ_NOEXCEPT { _ptr = ptr_; } // Perform atomic 'exchange pointers' operation. Pointer is set // to the 'val_' value. Old value is returned. - inline T *xchg (T *val_) + inline T *xchg (T *val_) ZMQ_NOEXCEPT { #if defined ZMQ_ATOMIC_PTR_CXX11 return _ptr.exchange (val_, std::memory_order_acq_rel); @@ -206,7 +211,7 @@ template class atomic_ptr_t // The pointer is compared to 'cmp' argument and if they are // equal, its value is set to 'val_'. Old value of the pointer // is returned. - inline T *cas (T *cmp_, T *val_) + inline T *cas (T *cmp_, T *val_) ZMQ_NOEXCEPT { #if defined ZMQ_ATOMIC_PTR_CXX11 _ptr.compare_exchange_strong (cmp_, val_, std::memory_order_acq_rel); @@ -240,11 +245,14 @@ template class atomic_ptr_t struct atomic_value_t { - atomic_value_t (const int value_) : _value (value_) {} + atomic_value_t (const int value_) ZMQ_NOEXCEPT : _value (value_) {} - atomic_value_t (const atomic_value_t &src_) : _value (src_.load ()) {} + atomic_value_t (const atomic_value_t &src_) ZMQ_NOEXCEPT + : _value (src_.load ()) + { + } - void store (const int value_) + void store (const int value_) ZMQ_NOEXCEPT { #if defined ZMQ_ATOMIC_PTR_CXX11 _value.store (value_, std::memory_order_release); @@ -258,7 +266,7 @@ struct atomic_value_t #endif } - int load () const + int load () const ZMQ_NOEXCEPT { #if defined ZMQ_ATOMIC_PTR_CXX11 return _value.load (std::memory_order_acquire); From b59d7d574dac56d61e8c78caf5d70c16b28f7c5f Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:08:30 +0200 Subject: [PATCH 11/22] Problem: typo in parameter name Solution: correct spelling --- src/io_object.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/io_object.hpp b/src/io_object.hpp index 4d006152..b0527a72 100644 --- a/src/io_object.hpp +++ b/src/io_object.hpp @@ -65,7 +65,7 @@ class io_object_t : public i_poll_events void reset_pollin (handle_t handle_); void set_pollout (handle_t handle_); void reset_pollout (handle_t handle_); - void add_timer (int timout_, int id_); + void add_timer (int timeout_, int id_); void cancel_timer (int id_); // i_poll_events interface implementation. From 29f1f39df323a8b5d64d759412d9faa9beb03aa1 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:11:07 +0200 Subject: [PATCH 12/22] Problem: unused parameter warning Solution: mark parameter as unused depending on platform definitions --- src/ip.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ip.cpp b/src/ip.cpp index 4c527382..8f3a53e4 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -667,14 +667,14 @@ void zmq::make_socket_noninheritable (fd_t sock_) const BOOL brc = SetHandleInformation (reinterpret_cast (sock_), HANDLE_FLAG_INHERIT, 0); win_assert (brc); -#endif - -#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \ +#elif (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \ && defined FD_CLOEXEC // If there 's no SOCK_CLOEXEC, let's try the second best option. // Race condition can cause socket not to be closed (if fork happens // between accept and this point). const int rc = fcntl (sock_, F_SETFD, FD_CLOEXEC); errno_assert (rc != -1); +#else + LIBZMQ_UNUSED (sock_); #endif } From da4309da135a712f84819506e9e369fc930d975a Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:13:54 +0200 Subject: [PATCH 13/22] Problem: violation of parameter naming conventions Solution: change to comply with naming conventions --- src/ipc_address.cpp | 8 ++++---- src/ipc_address.hpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ipc_address.cpp b/src/ipc_address.cpp index 4791d65d..1352c6f2 100644 --- a/src/ipc_address.cpp +++ b/src/ipc_address.cpp @@ -43,13 +43,13 @@ zmq::ipc_address_t::ipc_address_t () memset (&address, 0, sizeof address); } -zmq::ipc_address_t::ipc_address_t (const sockaddr *sa, socklen_t sa_len) +zmq::ipc_address_t::ipc_address_t (const sockaddr *sa_, socklen_t sa_len_) { - zmq_assert (sa && sa_len > 0); + zmq_assert (sa_ && sa_len_ > 0); memset (&address, 0, sizeof address); - if (sa->sa_family == AF_UNIX) - memcpy (&address, sa, sa_len); + if (sa_->sa_family == AF_UNIX) + memcpy (&address, sa_, sa_len_); } zmq::ipc_address_t::~ipc_address_t () diff --git a/src/ipc_address.hpp b/src/ipc_address.hpp index e9b74c30..5ce87331 100644 --- a/src/ipc_address.hpp +++ b/src/ipc_address.hpp @@ -44,7 +44,7 @@ class ipc_address_t { public: ipc_address_t (); - ipc_address_t (const sockaddr *sa, socklen_t sa_len); + ipc_address_t (const sockaddr *sa_, socklen_t sa_len_); ~ipc_address_t (); // This function sets up the address for UNIX domain transport. From 6c67fca7e0bf81716cfce882a7a4a18148b4bc88 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:20:22 +0200 Subject: [PATCH 14/22] Problem: numerous hicpp-signed-bitwise warnings that cannot easily be solved because of externally defined types Solution: disable this warning for now --- .clang-tidy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index a5059308..2d55278b 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -27,6 +27,7 @@ Checks: "*,\ # not easily possible to implement (maybe replace by specific exclusions),\ -cppcoreguidelines-pro-type-vararg,\ -cppcoreguidelines-pro-type-reinterpret-cast,\ +-hicpp-signed-bitwise,\ # duplicates,\ -google-readability-braces-around-statements,\ -cppcoreguidelines-pro-type-cstyle-cast,\ @@ -38,7 +39,7 @@ Checks: "*,\ -hicpp-use-auto,\ -hicpp-use-nullptr,\ -hicpp-no-array-decay,\ --hicpp-member-init" +-hicpp-member-init“ WarningsAsErrors: '' HeaderFilterRegex: '' # AnalyzeTemporaryDtors: false From 88646438d2822e01fc2323dced71e7cb822018fb Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:24:16 +0200 Subject: [PATCH 15/22] Problem: differing parameter names between declaration and definition Solution: harmonize --- src/mechanism.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/mechanism.cpp b/src/mechanism.cpp index 6c6f265b..53a015fb 100644 --- a/src/mechanism.cpp +++ b/src/mechanism.cpp @@ -60,12 +60,12 @@ void zmq::mechanism_t::peer_routing_id (msg_t *msg_) msg_->set_flags (msg_t::routing_id); } -void zmq::mechanism_t::set_user_id (const void *data_, size_t size_) +void zmq::mechanism_t::set_user_id (const void *user_id_, size_t size_) { - _user_id.set (static_cast (data_), size_); + _user_id.set (static_cast (user_id_), size_); _zap_properties.ZMQ_MAP_INSERT_OR_EMPLACE ( std::string (ZMQ_MSG_PROPERTY_USER_ID), - std::string (reinterpret_cast (data_), size_)); + std::string (reinterpret_cast (user_id_), size_)); } const zmq::blob_t &zmq::mechanism_t::get_user_id () const @@ -159,20 +159,20 @@ size_t zmq::mechanism_t::property_len (const char *name_, size_t value_len_) #define ZMTP_PROPERTY_SOCKET_TYPE "Socket-Type" #define ZMTP_PROPERTY_IDENTITY "Identity" -size_t zmq::mechanism_t::add_basic_properties (unsigned char *buf_, - size_t buf_capacity_) const +size_t zmq::mechanism_t::add_basic_properties (unsigned char *ptr_, + size_t ptr_capacity_) const { - unsigned char *ptr = buf_; + unsigned char *ptr = ptr_; // Add socket type property const char *socket_type = socket_type_string (options.type); - ptr += add_property (ptr, buf_capacity_, ZMTP_PROPERTY_SOCKET_TYPE, + ptr += add_property (ptr, ptr_capacity_, ZMTP_PROPERTY_SOCKET_TYPE, socket_type, strlen (socket_type)); // Add identity (aka routing id) property if (options.type == ZMQ_REQ || options.type == ZMQ_DEALER || options.type == ZMQ_ROUTER) { - ptr += add_property (ptr, buf_capacity_ - (ptr - buf_), + ptr += add_property (ptr, ptr_capacity_ - (ptr - ptr_), ZMTP_PROPERTY_IDENTITY, options.routing_id, options.routing_id_size); } @@ -182,10 +182,10 @@ size_t zmq::mechanism_t::add_basic_properties (unsigned char *buf_, options.app_metadata.begin (); it != options.app_metadata.end (); ++it) ptr += - add_property (ptr, buf_capacity_ - (ptr - buf_), it->first.c_str (), + add_property (ptr, ptr_capacity_ - (ptr - ptr_), it->first.c_str (), it->second.c_str (), strlen (it->second.c_str ())); - return ptr - buf_; + return ptr - ptr_; } size_t zmq::mechanism_t::basic_properties_len () const From 24edc828abacee63d94f6fafaf70c94c7b95b9ec Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:30:29 +0200 Subject: [PATCH 16/22] Problem: redundant else after return Solution: remove redundant else --- src/msg.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/msg.cpp b/src/msg.cpp index 8b12d6dc..16ad1e54 100644 --- a/src/msg.cpp +++ b/src/msg.cpp @@ -59,19 +59,18 @@ int zmq::msg_t::init (void *data_, content_t *content_) { if (size_ < max_vsm_size) { - int const rc = init_size (size_); + const int rc = init_size (size_); if (rc != -1) { memcpy (data (), data_, size_); return 0; } return -1; - - } else if (content_) { - return init_external_storage (content_, data_, size_, ffn_, hint_); - } else { - return init_data (data_, size_, ffn_, hint_); } + if (content_) { + return init_external_storage (content_, data_, size_, ffn_, hint_); + } + return init_data (data_, size_, ffn_, hint_); } int zmq::msg_t::init () From 0f2979a38ef062a8431d737206553ff2471fb121 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:31:06 +0200 Subject: [PATCH 17/22] Problem: assignment used as condition warning Solution: changed condition --- src/socket_base.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/socket_base.hpp b/src/socket_base.hpp index 924b8594..0d5a188c 100644 --- a/src/socket_base.hpp +++ b/src/socket_base.hpp @@ -328,9 +328,8 @@ class routing_socket_base_t : public socket_base_t { bool res = false; for (out_pipes_t::iterator it = _out_pipes.begin (); - it != _out_pipes.end (); ++it) { - if (res |= func (*it->second.pipe)) - break; + it != _out_pipes.end () && !res; ++it) { + res |= func (*it->second.pipe); } return res; From 6fa12bd6926f65d6bc2d11368faa3c22f44a8b56 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:32:53 +0200 Subject: [PATCH 18/22] Problem: unused stored value warning Solution: add LIBZMQ_UNUSED --- src/router.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/router.cpp b/src/router.cpp index fe02f74b..ab9dcb7b 100644 --- a/src/router.cpp +++ b/src/router.cpp @@ -80,6 +80,8 @@ void zmq::router_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_) rc = pipe_->write (&probe_msg); // zmq_assert (rc) is not applicable here, since it is not a bug. + LIBZMQ_UNUSED (rc); + pipe_->flush (); rc = probe_msg.close (); From 9e6f1c9a3c000dd56a5d61fe19ac8f7cd1c5432f Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:34:42 +0200 Subject: [PATCH 19/22] Problem: redundant else after return Solution: remove redundant else --- src/signaler.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/signaler.cpp b/src/signaler.cpp index 0c4e609d..b46d3d85 100644 --- a/src/signaler.cpp +++ b/src/signaler.cpp @@ -250,12 +250,13 @@ int zmq::signaler_t::wait (int timeout_) if (unlikely (rc < 0)) { errno_assert (errno == EINTR); return -1; - } else if (unlikely (rc == 0)) { + } + if (unlikely (rc == 0)) { errno = EAGAIN; return -1; } #ifdef HAVE_FORK - else if (unlikely (pid != getpid ())) { + if (unlikely (pid != getpid ())) { // we have forked and the file descriptor is closed. Emulate an interrupt // response. //printf("Child process %d signaler_t::wait returning simulating interrupt #2\n", getpid()); From ce4b71c3fad2359f8f8041c537ea8ad805b2edbd Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:39:57 +0200 Subject: [PATCH 20/22] Problem: cppcoreguidelines-pro-type-static-cast-downcast warnings that are not meant to be resolved Solution: disable cppcoreguidelines-pro-type-static-cast-downcast warning --- .clang-tidy | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-tidy b/.clang-tidy index 2d55278b..2b61fff4 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -9,6 +9,7 @@ Checks: "*,\ -fuchsia-default-arguments,\ -google-readability-todo,\ -cppcoreguidelines-pro-type-member-init,\ +-cppcoreguidelines-pro-type-static-cast-downcast,\ # not currently a coding convention, C++11-specific, but conceivable,\ -modernize-use-nullptr,\ -modernize-use-equals-default,\ From c641644bb200dab1a95202959e6b8964210c75bb Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:46:29 +0200 Subject: [PATCH 21/22] Problem: inconsistent parameter names Solution: harmonize --- src/socket_base.cpp | 29 +++++++++++++++-------------- src/socket_base.hpp | 20 ++++++++++---------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/socket_base.cpp b/src/socket_base.cpp index a0884930..bf933213 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -1577,7 +1577,7 @@ void zmq::socket_base_t::extract_flags (msg_t *msg_) _rcvmore = (msg_->flags () & msg_t::more) != 0; } -int zmq::socket_base_t::monitor (const char *addr_, int events_) +int zmq::socket_base_t::monitor (const char *endpoint_, int events_) { scoped_lock_t lock (_monitor_sync); @@ -1587,14 +1587,14 @@ int zmq::socket_base_t::monitor (const char *addr_, int events_) } // Support deregistering monitoring endpoints as well - if (addr_ == NULL) { + if (endpoint_ == NULL) { stop_monitor (); return 0; } // Parse addr_ string. std::string protocol; std::string address; - if (parse_uri (addr_, protocol, address) || check_protocol (protocol)) + if (parse_uri (endpoint_, protocol, address) || check_protocol (protocol)) return -1; // Event notification only supported over inproc:// @@ -1620,7 +1620,7 @@ int zmq::socket_base_t::monitor (const char *addr_, int events_) stop_monitor (false); // Spawn the monitor socket endpoint - rc = zmq_bind (_monitor_socket, addr_); + rc = zmq_bind (_monitor_socket, endpoint_); if (rc == -1) stop_monitor (false); return rc; @@ -1809,34 +1809,35 @@ std::string zmq::routing_socket_base_t::extract_connect_routing_id () return res; } -void zmq::routing_socket_base_t::add_out_pipe (blob_t routing_id, pipe_t *pipe_) +void zmq::routing_socket_base_t::add_out_pipe (blob_t routing_id_, + pipe_t *pipe_) { // Add the record into output pipes lookup table const out_pipe_t outpipe = {pipe_, true}; const bool ok = - _out_pipes.ZMQ_MAP_INSERT_OR_EMPLACE (ZMQ_MOVE (routing_id), outpipe) + _out_pipes.ZMQ_MAP_INSERT_OR_EMPLACE (ZMQ_MOVE (routing_id_), outpipe) .second; zmq_assert (ok); } -bool zmq::routing_socket_base_t::has_out_pipe (const blob_t &routing_id) const +bool zmq::routing_socket_base_t::has_out_pipe (const blob_t &routing_id_) const { - return 0 != _out_pipes.count (routing_id); + return 0 != _out_pipes.count (routing_id_); } zmq::routing_socket_base_t::out_pipe_t * -zmq::routing_socket_base_t::lookup_out_pipe (const blob_t &routing_id) +zmq::routing_socket_base_t::lookup_out_pipe (const blob_t &routing_id_) { // TODO we could probably avoid constructor a temporary blob_t to call this function - out_pipes_t::iterator it = _out_pipes.find (routing_id); + out_pipes_t::iterator it = _out_pipes.find (routing_id_); return it == _out_pipes.end () ? NULL : &it->second; } const zmq::routing_socket_base_t::out_pipe_t * -zmq::routing_socket_base_t::lookup_out_pipe (const blob_t &routing_id) const +zmq::routing_socket_base_t::lookup_out_pipe (const blob_t &routing_id_) const { // TODO we could probably avoid constructor a temporary blob_t to call this function - out_pipes_t::const_iterator it = _out_pipes.find (routing_id); + out_pipes_t::const_iterator it = _out_pipes.find (routing_id_); return it == _out_pipes.end () ? NULL : &it->second; } @@ -1847,9 +1848,9 @@ void zmq::routing_socket_base_t::erase_out_pipe (pipe_t *pipe_) } zmq::routing_socket_base_t::out_pipe_t -zmq::routing_socket_base_t::try_erase_out_pipe (const blob_t &routing_id) +zmq::routing_socket_base_t::try_erase_out_pipe (const blob_t &routing_id_) { - const out_pipes_t::iterator it = _out_pipes.find (routing_id); + const out_pipes_t::iterator it = _out_pipes.find (routing_id_); out_pipe_t res = {NULL, false}; if (it != _out_pipes.end ()) { res = it->second; diff --git a/src/socket_base.hpp b/src/socket_base.hpp index 0d5a188c..e3b6918d 100644 --- a/src/socket_base.hpp +++ b/src/socket_base.hpp @@ -137,8 +137,8 @@ class socket_base_t : public own_t, // Query the state of a specific peer. The default implementation // always returns an ENOTSUP error. - virtual int get_peer_state (const void *identity_, - size_t identity_size_) const; + virtual int get_peer_state (const void *routing_id_, + size_t routing_id_size_) const; protected: socket_base_t (zmq::ctx_t *parent_, @@ -186,7 +186,7 @@ class socket_base_t : public own_t, private: // test if event should be sent and then dispatch it - void event (const std::string &addr_, intptr_t fd_, int type_); + void event (const std::string &addr_, intptr_t value_, int type_); // Socket event data dispatch void monitor_event (int event_, intptr_t value_, const std::string &addr_); @@ -318,18 +318,18 @@ class routing_socket_base_t : public socket_base_t bool active; }; - void add_out_pipe (blob_t routing_id, pipe_t *pipe_); - bool has_out_pipe (const blob_t &routing_id) const; - out_pipe_t *lookup_out_pipe (const blob_t &routing_id); - const out_pipe_t *lookup_out_pipe (const blob_t &routing_id) const; + void add_out_pipe (blob_t routing_id_, pipe_t *pipe_); + bool has_out_pipe (const blob_t &routing_id_) const; + out_pipe_t *lookup_out_pipe (const blob_t &routing_id_); + const out_pipe_t *lookup_out_pipe (const blob_t &routing_id_) const; void erase_out_pipe (pipe_t *pipe_); - out_pipe_t try_erase_out_pipe (const blob_t &routing_id); - template bool any_of_out_pipes (Func func) + out_pipe_t try_erase_out_pipe (const blob_t &routing_id_); + template bool any_of_out_pipes (Func func_) { bool res = false; for (out_pipes_t::iterator it = _out_pipes.begin (); it != _out_pipes.end () && !res; ++it) { - res |= func (*it->second.pipe); + res |= func_ (*it->second.pipe); } return res; From a2f91c5509c907173798f94db2036a25986fc2e8 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 30 May 2018 22:49:54 +0200 Subject: [PATCH 22/22] Problem: unnecessary passing of non-const data Solution: make const --- src/socks.cpp | 6 +++--- src/socks.hpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/socks.cpp b/src/socks.cpp index 93ac63bd..516570f9 100644 --- a/src/socks.cpp +++ b/src/socks.cpp @@ -45,7 +45,7 @@ zmq::socks_greeting_t::socks_greeting_t (uint8_t method_) : num_methods (1) methods[0] = method_; } -zmq::socks_greeting_t::socks_greeting_t (uint8_t *methods_, +zmq::socks_greeting_t::socks_greeting_t (const uint8_t *methods_, uint8_t num_methods_) : num_methods (num_methods_) { @@ -273,8 +273,8 @@ bool zmq::socks_response_decoder_t::message_ready () const return _bytes_read == 10; if (atyp == 0x03) return _bytes_read > 4 && _bytes_read == 4 + 1 + _buf[4] + 2u; - else - return _bytes_read == 22; + + return _bytes_read == 22; } zmq::socks_response_t zmq::socks_response_decoder_t::decode () diff --git a/src/socks.hpp b/src/socks.hpp index fa9fb608..de76786e 100644 --- a/src/socks.hpp +++ b/src/socks.hpp @@ -39,7 +39,7 @@ namespace zmq struct socks_greeting_t { socks_greeting_t (uint8_t method_); - socks_greeting_t (uint8_t *methods_, uint8_t num_methods_); + socks_greeting_t (const uint8_t *methods_, uint8_t num_methods_); uint8_t methods[UINT8_MAX]; const size_t num_methods;