Skip to content

Commit

Permalink
Merge pull request zeromq#3116 from sigiesec/fix-some-more-style-issues
Browse files Browse the repository at this point in the history
Fix some more code style issues
  • Loading branch information
bluca authored May 18, 2018
2 parents 6a5051f + 4e616f3 commit c178193
Show file tree
Hide file tree
Showing 55 changed files with 451 additions and 349 deletions.
12 changes: 8 additions & 4 deletions src/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#ifndef __ZMQ_BLOB_HPP_INCLUDED__
#define __ZMQ_BLOB_HPP_INCLUDED__

#include "err.hpp"

#include <stdlib.h>
#include <string.h>
#include <algorithm>
Expand Down Expand Up @@ -73,19 +75,21 @@ struct blob_t

// Creates a blob_t of a given size, with uninitialized content.
explicit blob_t (const size_t size) :
data_ ((unsigned char *) malloc (size)),
data_ (static_cast<unsigned char *> (malloc (size))),
size_ (size),
owned_ (true)
{
alloc_assert (data_);
}

// Creates a blob_t of a given size, an initializes content by copying
// from another buffer.
blob_t (const unsigned char *const data, const size_t size) :
data_ ((unsigned char *) malloc (size)),
data_ (static_cast<unsigned char *> (malloc (size))),
size_ (size),
owned_ (true)
{
alloc_assert (data_);
memcpy (data_, data, size_);
}

Expand Down Expand Up @@ -120,7 +124,7 @@ struct blob_t
void set_deep_copy (blob_t const &other)
{
clear ();
data_ = (unsigned char *) malloc (other.size_);
data_ = static_cast<unsigned char *> (malloc (other.size_));
size_ = other.size_;
owned_ = true;
memcpy (data_, other.data_, size_);
Expand All @@ -130,7 +134,7 @@ struct blob_t
void set (const unsigned char *const data, const size_t size)
{
clear ();
data_ = (unsigned char *) malloc (size);
data_ = static_cast<unsigned char *> (malloc (size));
size_ = size;
owned_ = true;
memcpy (data_, data, size_);
Expand Down
2 changes: 1 addition & 1 deletion src/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ uint64_t zmq::clock_t::now_us ()
// Convert the tick number into the number of seconds
// since the system was started.
double ticks_div = ticksPerSecond.QuadPart / 1000000.0;
return (uint64_t) (tick.QuadPart / ticks_div);
return static_cast<uint64_t> (tick.QuadPart / ticks_div);

#elif defined HAVE_CLOCK_GETTIME \
&& (defined CLOCK_MONOTONIC || defined ZMQ_HAVE_VXWORKS)
Expand Down
11 changes: 7 additions & 4 deletions src/ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ bool zmq::ctx_t::start ()
int ios = io_thread_count;
opt_sync.unlock ();
slot_count = mazmq + ios + 2;
slots = (i_mailbox **) malloc (sizeof (i_mailbox *) * slot_count);
slots =
static_cast<i_mailbox **> (malloc (sizeof (i_mailbox *) * slot_count));
if (!slots) {
errno = ENOMEM;
goto fail;
Expand All @@ -311,7 +312,8 @@ bool zmq::ctx_t::start ()
reaper->start ();

// Create I/O thread objects and launch them.
for (int32_t i = (int32_t) slot_count - 1; i >= (int32_t) 2; i--) {
for (int32_t i = static_cast<int32_t> (slot_count) - 1;
i >= static_cast<int32_t> (2); i--) {
slots[i] = NULL;
}

Expand All @@ -331,7 +333,8 @@ bool zmq::ctx_t::start ()
}

// In the unused part of the slot array, create a list of empty slots.
for (int32_t i = (int32_t) slot_count - 1; i >= (int32_t) ios + 2; i--) {
for (int32_t i = static_cast<int32_t> (slot_count) - 1;
i >= static_cast<int32_t> (ios) + 2; i--) {
empty_slots.push_back (i);
}

Expand Down Expand Up @@ -377,7 +380,7 @@ zmq::socket_base_t *zmq::ctx_t::create_socket (int type_)
empty_slots.pop_back ();

// Generate new unique socket ID.
int sid = ((int) max_socket_id.add (1)) + 1;
int sid = (static_cast<int> (max_socket_id.add (1))) + 1;

// Create the socket and register its mailbox.
socket_base_t *s = socket_base_t::create (type_, this, slot, sid);
Expand Down
7 changes: 4 additions & 3 deletions src/curve_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ int zmq::curve_client_t::produce_initiate (msg_t *msg_)
{
const size_t metadata_length = basic_properties_len ();
unsigned char *metadata_plaintext =
(unsigned char *) malloc (metadata_length);
static_cast<unsigned char *> (malloc (metadata_length));
alloc_assert (metadata_plaintext);

add_basic_properties (metadata_plaintext, metadata_length);
Expand Down Expand Up @@ -217,10 +217,11 @@ int zmq::curve_client_t::process_ready (const uint8_t *msg_data,
const size_t clen = (msg_size - 14) + crypto_box_BOXZEROBYTES;

uint8_t ready_nonce[crypto_box_NONCEBYTES];
uint8_t *ready_plaintext = (uint8_t *) malloc (crypto_box_ZEROBYTES + clen);
uint8_t *ready_plaintext =
static_cast<uint8_t *> (malloc (crypto_box_ZEROBYTES + clen));
alloc_assert (ready_plaintext);
uint8_t *ready_box =
(uint8_t *) malloc (crypto_box_BOXZEROBYTES + 16 + clen);
static_cast<uint8_t *> (malloc (crypto_box_BOXZEROBYTES + 16 + clen));
alloc_assert (ready_box);

memset (ready_box, 0, crypto_box_BOXZEROBYTES);
Expand Down
8 changes: 4 additions & 4 deletions src/curve_client_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ struct curve_client_tools_t
return -1;

uint8_t initiate_nonce[crypto_box_NONCEBYTES];
uint8_t *initiate_box =
(uint8_t *) malloc (crypto_box_BOXZEROBYTES + 144 + metadata_length);
uint8_t *initiate_box = static_cast<uint8_t *> (
malloc (crypto_box_BOXZEROBYTES + 144 + metadata_length));
alloc_assert (initiate_box);
uint8_t *initiate_plaintext =
(uint8_t *) malloc (crypto_box_ZEROBYTES + 128 + metadata_length);
uint8_t *initiate_plaintext = static_cast<uint8_t *> (
malloc (crypto_box_ZEROBYTES + 128 + metadata_length));
alloc_assert (initiate_plaintext);

// Create Box [C + vouch + metadata](C'->S')
Expand Down
6 changes: 3 additions & 3 deletions src/curve_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ int zmq::curve_server_t::produce_ready (msg_t *msg_)
uint8_t ready_nonce[crypto_box_NONCEBYTES];

uint8_t *ready_plaintext =
(uint8_t *) malloc (crypto_box_ZEROBYTES + metadata_length);
static_cast<uint8_t *> (malloc (crypto_box_ZEROBYTES + metadata_length));
alloc_assert (ready_plaintext);

// Create Box [metadata](S'->C')
Expand All @@ -443,8 +443,8 @@ int zmq::curve_server_t::produce_ready (msg_t *msg_)
memcpy (ready_nonce, "CurveZMQREADY---", 16);
put_uint64 (ready_nonce + 16, cn_nonce);

uint8_t *ready_box =
(uint8_t *) malloc (crypto_box_BOXZEROBYTES + 16 + metadata_length);
uint8_t *ready_box = static_cast<uint8_t *> (
malloc (crypto_box_BOXZEROBYTES + 16 + metadata_length));
alloc_assert (ready_box);

int rc = crypto_box_afternm (ready_box, ready_plaintext, mlen, ready_nonce,
Expand Down
7 changes: 4 additions & 3 deletions src/dish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ int zmq::dish_session_t::push_msg (msg_t *msg_)
goto has_group;

// Set the message group
rc = msg_->set_group ((char *) group_msg.data (), group_msg.size ());
rc = msg_->set_group (static_cast<char *> (group_msg.data ()),
group_msg.size ());
errno_assert (rc == 0);

// We set the group, so we don't need the group_msg anymore
Expand Down Expand Up @@ -328,7 +329,7 @@ int zmq::dish_session_t::pull_msg (msg_t *msg_)
if (!msg_->is_join () && !msg_->is_leave ())
return rc;
else {
int group_length = (int) strlen (msg_->group ());
int group_length = static_cast<int> (strlen (msg_->group ()));

msg_t command;
int offset;
Expand All @@ -346,7 +347,7 @@ int zmq::dish_session_t::pull_msg (msg_t *msg_)
}

command.set_flags (msg_t::command);
char *command_data = (char *) command.data ();
char *command_data = static_cast<char *> (command.data ());

// Copy the group
memcpy (command_data + offset, msg_->group (), group_length);
Expand Down
2 changes: 1 addition & 1 deletion src/dist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void zmq::dist_t::distribute (msg_t *msg_)

// Add matching-1 references to the message. We already hold one reference,
// that's why -1.
msg_->add_refs ((int) matching - 1);
msg_->add_refs (static_cast<int> (matching) - 1);

// Push copy of the message to each matching pipe.
int failed = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ template <typename T> class encoder_base_t : public i_encoder
next (NULL),
new_msg_flag (false),
bufsize (bufsize_),
buf ((unsigned char *) malloc (bufsize_)),
buf (static_cast<unsigned char *> (malloc (bufsize_))),
in_progress (NULL)
{
alloc_assert (buf);
Expand Down Expand Up @@ -146,7 +146,7 @@ template <typename T> class encoder_base_t : public i_encoder
step_t next_,
bool new_msg_flag_)
{
write_pos = (unsigned char *) write_pos_;
write_pos = static_cast<unsigned char *> (write_pos_);
to_write = to_write_;
next = next_;
new_msg_flag = new_msg_flag_;
Expand Down
4 changes: 2 additions & 2 deletions src/err.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ void zmq::win_error (char *buffer_, size_t buffer_size_)
#else
DWORD rc = FormatMessageA (
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_,
NULL);
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buffer_,
static_cast<DWORD> (buffer_size_), NULL);
#endif
zmq_assert (rc);
}
Expand Down
8 changes: 8 additions & 0 deletions src/err.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@
namespace zmq
{
const char *errno_to_string (int errno_);
#if defined __clang__
#if __has_feature(attribute_analyzer_noreturn)
void zmq_abort (const char *errmsg_) __attribute__ ((analyzer_noreturn));
#endif
#elif defined __MSCVER__
__declspec(noreturn) void zmq_abort (const char *errmsg_);
#else
void zmq_abort (const char *errmsg_);
#endif
void print_backtrace (void);
}

Expand Down
5 changes: 4 additions & 1 deletion src/fd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ enum
#else
typedef SOCKET fd_t;
enum
#if _MSC_VER >= 1800
: fd_t
#endif
{
retired_fd = (fd_t) INVALID_SOCKET
retired_fd = INVALID_SOCKET
};
#endif
#else
Expand Down
2 changes: 1 addition & 1 deletion src/generic_mtrie_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void zmq::generic_mtrie_t<T>::rm_helper (value_t *pipe_,
// Adjust the buffer.
if (buffsize_ >= maxbuffsize_) {
maxbuffsize_ = buffsize_ + 256;
*buff_ = (unsigned char *) realloc (*buff_, maxbuffsize_);
*buff_ = static_cast<unsigned char *> (realloc (*buff_, maxbuffsize_));
alloc_assert (*buff_);
}

Expand Down
2 changes: 1 addition & 1 deletion src/io_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) :
object_t (ctx_, tid_),
mailbox_handle ((poller_t::handle_t) NULL)
mailbox_handle (static_cast<poller_t::handle_t> (NULL))
{
poller = new (std::nothrow) poller_t (*ctx_);
alloc_assert (poller);
Expand Down
50 changes: 30 additions & 20 deletions src/ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ void zmq::enable_ipv4_mapping (fd_t s_)
#else
int flag = 0;
#endif
int rc =
setsockopt (s_, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &flag, sizeof (flag));
int rc = setsockopt (s_, IPPROTO_IPV6, IPV6_V6ONLY,
reinterpret_cast<char *> (&flag), sizeof (flag));
#ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR);
#else
Expand All @@ -156,7 +156,8 @@ int zmq::get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_)
#else
socklen_t addrlen = sizeof ss;
#endif
rc = getpeername (sockfd_, (struct sockaddr *) &ss, &addrlen);
rc = getpeername (sockfd_, reinterpret_cast<struct sockaddr *> (&ss),
&addrlen);
#ifdef ZMQ_HAVE_WINDOWS
if (rc == SOCKET_ERROR) {
const int last_error = WSAGetLastError ();
Expand All @@ -173,8 +174,8 @@ int zmq::get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_)
#endif

char host[NI_MAXHOST];
rc = getnameinfo ((struct sockaddr *) &ss, addrlen, host, sizeof host, NULL,
0, NI_NUMERICHOST);
rc = getnameinfo (reinterpret_cast<struct sockaddr *> (&ss), addrlen, host,
sizeof host, NULL, 0, NI_NUMERICHOST);
if (rc != 0)
return 0;

Expand All @@ -187,7 +188,7 @@ int zmq::get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_)
} u;

u.sa_stor = ss;
return (int) u.sa.sa_family;
return static_cast<int> (u.sa.sa_family);
}

void zmq::set_ip_type_of_service (fd_t s_, int iptos)
Expand Down Expand Up @@ -311,8 +312,9 @@ void zmq::shutdown_network ()
static void tune_socket (const SOCKET socket)
{
BOOL tcp_nodelay = 1;
int rc = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY,
(char *) &tcp_nodelay, sizeof tcp_nodelay);
int rc =
setsockopt (socket, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char *> (&tcp_nodelay), sizeof tcp_nodelay);
wsa_assert (rc != SOCKET_ERROR);

zmq::tcp_tune_loopback_fast_path (socket);
Expand Down Expand Up @@ -415,7 +417,8 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
// Set SO_REUSEADDR and TCP_NODELAY on listening socket.
BOOL so_reuseaddr = 1;
int rc = setsockopt (listener, SOL_SOCKET, SO_REUSEADDR,
(char *) &so_reuseaddr, sizeof so_reuseaddr);
reinterpret_cast<char *> (&so_reuseaddr),
sizeof so_reuseaddr);
wsa_assert (rc != SOCKET_ERROR);

tune_socket (listener);
Expand All @@ -441,12 +444,14 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
}

// Bind listening socket to signaler port.
rc = bind (listener, (const struct sockaddr *) &addr, sizeof addr);
rc = bind (listener, reinterpret_cast<const struct sockaddr *> (&addr),
sizeof addr);

if (rc != SOCKET_ERROR && signaler_port == 0) {
// Retrieve ephemeral port number
int addrlen = sizeof addr;
rc = getsockname (listener, (struct sockaddr *) &addr, &addrlen);
rc = getsockname (listener, reinterpret_cast<struct sockaddr *> (&addr),
&addrlen);
}

// Listen for incoming connections.
Expand All @@ -455,7 +460,8 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)

// Connect writer to the listener.
if (rc != SOCKET_ERROR)
rc = connect (*w_, (struct sockaddr *) &addr, sizeof addr);
rc = connect (*w_, reinterpret_cast<struct sockaddr *> (&addr),
sizeof addr);

// Accept connection from writer.
if (rc != SOCKET_ERROR)
Expand All @@ -466,22 +472,26 @@ int zmq::make_fdpair (fd_t *r_, fd_t *w_)
if (*r_ != INVALID_SOCKET) {
size_t dummy_size =
1024 * 1024; // 1M to overload default receive buffer
unsigned char *dummy = (unsigned char *) malloc (dummy_size);
unsigned char *dummy =
static_cast<unsigned char *> (malloc (dummy_size));
wsa_assert (dummy);

int still_to_send = (int) dummy_size;
int still_to_recv = (int) dummy_size;
int still_to_send = static_cast<int> (dummy_size);
int still_to_recv = static_cast<int> (dummy_size);
while (still_to_send || still_to_recv) {
int nbytes;
if (still_to_send > 0) {
nbytes =
::send (*w_, (char *) (dummy + dummy_size - still_to_send),
still_to_send, 0);
nbytes = ::send (
*w_,
reinterpret_cast<char *> (dummy + dummy_size - still_to_send),
still_to_send, 0);
wsa_assert (nbytes != SOCKET_ERROR);
still_to_send -= nbytes;
}
nbytes = ::recv (*r_, (char *) (dummy + dummy_size - still_to_recv),
still_to_recv, 0);
nbytes = ::recv (
*r_,
reinterpret_cast<char *> (dummy + dummy_size - still_to_recv),
still_to_recv, 0);
wsa_assert (nbytes != SOCKET_ERROR);
still_to_recv -= nbytes;
}
Expand Down
Loading

0 comments on commit c178193

Please sign in to comment.