From 7c2d1c18246dc97d5cf1240d893f62f6885492f0 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Mon, 28 May 2018 18:46:36 +0200 Subject: [PATCH] Problem: magic literals for UCHAR_MAX Solution: use UCHAR_MAX constant instead --- src/mechanism.cpp | 3 ++- src/options.cpp | 5 +++-- src/stream_engine.cpp | 6 ++++-- src/thread.cpp | 2 +- src/v1_decoder.cpp | 5 +++-- src/v1_encoder.cpp | 6 ++++-- src/v2_encoder.cpp | 6 ++++-- 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/mechanism.cpp b/src/mechanism.cpp index a99e9dfb..35110742 100644 --- a/src/mechanism.cpp +++ b/src/mechanism.cpp @@ -29,6 +29,7 @@ #include "precompiled.hpp" #include +#include #include "mechanism.hpp" #include "options.hpp" @@ -124,7 +125,7 @@ static size_t property_len (size_t name_len_, size_t value_len_) static size_t name_len (const char *name_) { const size_t name_len = strlen (name_); - zmq_assert (name_len <= 255); + zmq_assert (name_len <= UCHAR_MAX); return name_len; } diff --git a/src/options.cpp b/src/options.cpp index 0efe20b4..e1152789 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -29,6 +29,7 @@ #include "precompiled.hpp" #include +#include #include #include "options.hpp" @@ -491,7 +492,7 @@ int zmq::options_t::setsockopt (int option_, case ZMQ_TCP_ACCEPT_FILTER: { std::string filter_str; int rc = do_setsockopt_string_allow_empty_strict ( - optval_, optvallen_, &filter_str, 255); + optval_, optvallen_, &filter_str, UCHAR_MAX); if (rc == 0) { if (filter_str.empty ()) { tcp_accept_filters.clear (); @@ -559,7 +560,7 @@ int zmq::options_t::setsockopt (int option_, case ZMQ_ZAP_DOMAIN: return do_setsockopt_string_allow_empty_relaxed ( - optval_, optvallen_, &zap_domain, 255); + optval_, optvallen_, &zap_domain, UCHAR_MAX); break; // If curve encryption isn't built, these options provoke EINVAL diff --git a/src/stream_engine.cpp b/src/stream_engine.cpp index f57da8dc..94ceff20 100644 --- a/src/stream_engine.cpp +++ b/src/stream_engine.cpp @@ -30,6 +30,7 @@ #include "precompiled.hpp" #include "macros.hpp" +#include #include #ifndef ZMQ_HAVE_WINDOWS @@ -232,7 +233,7 @@ void zmq::stream_engine_t::plug (io_thread_t *io_thread_, // Send the 'length' and 'flags' fields of the routing id message. // The 'length' field is encoded in the long format. _outpos = _greeting_send; - _outpos[_outsize++] = 0xff; + _outpos[_outsize++] = UCHAR_MAX; put_uint64 (&_outpos[_outsize], _options.routing_id_size + 1); _outsize += 8; _outpos[_outsize++] = 0x7f; @@ -587,7 +588,8 @@ bool zmq::stream_engine_t::handshake () // Since there is no way to tell the encoder to // skip the message header, we simply throw that // header data away. - const size_t header_size = _options.routing_id_size + 1 >= 255 ? 10 : 2; + const size_t header_size = + _options.routing_id_size + 1 >= UCHAR_MAX ? 10 : 2; unsigned char tmp[10], *bufferp = tmp; // Prepare the routing id message and load it into encoder. diff --git a/src/thread.cpp b/src/thread.cpp index d1a483b0..e100556a 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -146,7 +146,7 @@ void zmq::thread_t:: { int priority = (_thread_priority >= 0 ? _thread_priority : DEFAULT_PRIORITY); - priority = (priority < 255 ? priority : DEFAULT_PRIORITY); + priority = (priority < UCHAR_MAX ? priority : DEFAULT_PRIORITY); if (_descriptor != NULL || _descriptor > 0) { taskPrioritySet (_descriptor, priority); } diff --git a/src/v1_decoder.cpp b/src/v1_decoder.cpp index af55bd3f..b002dc9d 100644 --- a/src/v1_decoder.cpp +++ b/src/v1_decoder.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include "decoder.hpp" #include "v1_decoder.hpp" @@ -57,10 +58,10 @@ zmq::v1_decoder_t::~v1_decoder_t () int zmq::v1_decoder_t::one_byte_size_ready (unsigned char const *) { - // First byte of size is read. If it is 0xff read 8-byte size. + // First byte of size is read. If it is UCHAR_MAX (0xff) read 8-byte size. // Otherwise allocate the buffer for message data and read the // message data into it. - if (*_tmpbuf == 0xff) + if (*_tmpbuf == UCHAR_MAX) next_step (_tmpbuf, 8, &v1_decoder_t::eight_byte_size_ready); else { // There has to be at least one byte (the flags) in the message). diff --git a/src/v1_encoder.cpp b/src/v1_encoder.cpp index faca3516..cc78d417 100644 --- a/src/v1_encoder.cpp +++ b/src/v1_encoder.cpp @@ -33,6 +33,8 @@ #include "msg.hpp" #include "wire.hpp" +#include + zmq::v1_encoder_t::v1_encoder_t (size_t bufsize_) : encoder_base_t (bufsize_) { @@ -62,12 +64,12 @@ void zmq::v1_encoder_t::message_ready () // For messages less than 255 bytes long, write one byte of message size. // For longer messages write 0xff escape character followed by 8-byte // message size. In both cases 'flags' field follows. - if (size < 255) { + if (size < UCHAR_MAX) { _tmpbuf[0] = static_cast (size); _tmpbuf[1] = (in_progress->flags () & msg_t::more); next_step (_tmpbuf, 2, &v1_encoder_t::size_ready, false); } else { - _tmpbuf[0] = 0xff; + _tmpbuf[0] = UCHAR_MAX; put_uint64 (_tmpbuf + 1, size); _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 893b7d7e..1a869068 100644 --- a/src/v2_encoder.cpp +++ b/src/v2_encoder.cpp @@ -34,6 +34,8 @@ #include "likely.hpp" #include "wire.hpp" +#include + zmq::v2_encoder_t::v2_encoder_t (size_t bufsize_) : encoder_base_t (bufsize_) { @@ -52,7 +54,7 @@ void zmq::v2_encoder_t::message_ready () protocol_flags = 0; if (in_progress->flags () & msg_t::more) protocol_flags |= v2_protocol_t::more_flag; - if (in_progress->size () > 255) + if (in_progress->size () > UCHAR_MAX) protocol_flags |= v2_protocol_t::large_flag; if (in_progress->flags () & msg_t::command) protocol_flags |= v2_protocol_t::command_flag; @@ -61,7 +63,7 @@ void zmq::v2_encoder_t::message_ready () // 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 (); - if (unlikely (size > 255)) { + if (unlikely (size > UCHAR_MAX)) { put_uint64 (_tmp_buf + 1, size); next_step (_tmp_buf, 9, &v2_encoder_t::size_ready, false); } else {