Commit 6b9b3694 authored by Simon Giesecke's avatar Simon Giesecke

Problem: zmq_poller_* argument check code is cloned

Solution: extract common code into functions
parent 54aff774
...@@ -1187,17 +1187,53 @@ int zmq_poller_destroy (void **poller_p_) ...@@ -1187,17 +1187,53 @@ int zmq_poller_destroy (void **poller_p_)
return 0; return 0;
} }
int zmq_poller_add (void *poller_, void *s_, void *user_data_, short events_) static int check_poller (void *const poller_)
{ {
if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) { if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) {
errno = EFAULT; errno = EFAULT;
return -1; return -1;
} }
return 0;
}
static int check_poller_registration_args (void *const poller_, void *const s_)
{
if (-1 == check_poller (poller_))
return -1;
if (!s_ || !((zmq::socket_base_t *) s_)->check_tag ()) { if (!s_ || !((zmq::socket_base_t *) s_)->check_tag ()) {
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
return 0;
}
#if defined _WIN32
static int check_poller_fd_registration_args (void *const poller_,
const SOCKET fd_)
#else
static int check_poller_fd_registration_args (void *const poller_,
const int fd_)
#endif
{
if (-1 == check_poller (poller_))
return -1;
if (fd_ == zmq::retired_fd) {
errno = EBADF;
return -1;
}
return 0;
}
int zmq_poller_add (void *poller_, void *s_, void *user_data_, short events_)
{
if (-1 == check_poller_registration_args (poller_, s_))
return -1;
zmq::socket_base_t *socket = (zmq::socket_base_t *) s_; zmq::socket_base_t *socket = (zmq::socket_base_t *) s_;
return ((zmq::socket_poller_t *) poller_) return ((zmq::socket_poller_t *) poller_)
...@@ -1213,14 +1249,8 @@ int zmq_poller_add_fd (void *poller_, ...@@ -1213,14 +1249,8 @@ int zmq_poller_add_fd (void *poller_,
int zmq_poller_add_fd (void *poller_, int fd_, void *user_data_, short events_) int zmq_poller_add_fd (void *poller_, int fd_, void *user_data_, short events_)
#endif #endif
{ {
if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) { if (-1 == check_poller_fd_registration_args (poller_, fd_))
errno = EFAULT;
return -1;
}
if (fd_ == zmq::retired_fd) {
errno = EBADF;
return -1; return -1;
}
return ((zmq::socket_poller_t *) poller_) return ((zmq::socket_poller_t *) poller_)
->add_fd (fd_, user_data_, events_); ->add_fd (fd_, user_data_, events_);
...@@ -1229,15 +1259,9 @@ int zmq_poller_add_fd (void *poller_, int fd_, void *user_data_, short events_) ...@@ -1229,15 +1259,9 @@ int zmq_poller_add_fd (void *poller_, int fd_, void *user_data_, short events_)
int zmq_poller_modify (void *poller_, void *s_, short events_) int zmq_poller_modify (void *poller_, void *s_, short events_)
{ {
if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) { if (-1 == check_poller_registration_args (poller_, s_))
errno = EFAULT;
return -1; return -1;
}
if (!s_ || !((zmq::socket_base_t *) s_)->check_tag ()) {
errno = ENOTSOCK;
return -1;
}
zmq::socket_base_t *socket = (zmq::socket_base_t *) s_; zmq::socket_base_t *socket = (zmq::socket_base_t *) s_;
return ((zmq::socket_poller_t *) poller_)->modify (socket, events_); return ((zmq::socket_poller_t *) poller_)->modify (socket, events_);
...@@ -1250,14 +1274,8 @@ int zmq_poller_modify_fd (void *poller_, SOCKET fd_, short events_) ...@@ -1250,14 +1274,8 @@ int zmq_poller_modify_fd (void *poller_, SOCKET fd_, short events_)
int zmq_poller_modify_fd (void *poller_, int fd_, short events_) int zmq_poller_modify_fd (void *poller_, int fd_, short events_)
#endif #endif
{ {
if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) { if (-1 == check_poller_fd_registration_args (poller_, fd_))
errno = EFAULT;
return -1;
}
if (fd_ == zmq::retired_fd) {
errno = EBADF;
return -1; return -1;
}
return ((zmq::socket_poller_t *) poller_)->modify_fd (fd_, events_); return ((zmq::socket_poller_t *) poller_)->modify_fd (fd_, events_);
} }
...@@ -1265,15 +1283,9 @@ int zmq_poller_modify_fd (void *poller_, int fd_, short events_) ...@@ -1265,15 +1283,9 @@ int zmq_poller_modify_fd (void *poller_, int fd_, short events_)
int zmq_poller_remove (void *poller_, void *s_) int zmq_poller_remove (void *poller_, void *s_)
{ {
if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) { if (-1 == check_poller_registration_args (poller_, s_))
errno = EFAULT;
return -1; return -1;
}
if (!s_ || !((zmq::socket_base_t *) s_)->check_tag ()) {
errno = ENOTSOCK;
return -1;
}
zmq::socket_base_t *socket = (zmq::socket_base_t *) s_; zmq::socket_base_t *socket = (zmq::socket_base_t *) s_;
return ((zmq::socket_poller_t *) poller_)->remove (socket); return ((zmq::socket_poller_t *) poller_)->remove (socket);
...@@ -1285,32 +1297,19 @@ int zmq_poller_remove_fd (void *poller_, SOCKET fd_) ...@@ -1285,32 +1297,19 @@ int zmq_poller_remove_fd (void *poller_, SOCKET fd_)
int zmq_poller_remove_fd (void *poller_, int fd_) int zmq_poller_remove_fd (void *poller_, int fd_)
#endif #endif
{ {
if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) { if (-1 == check_poller_fd_registration_args (poller_, fd_))
errno = EFAULT;
return -1;
}
if (fd_ == zmq::retired_fd) {
errno = EBADF;
return -1; return -1;
}
return ((zmq::socket_poller_t *) poller_)->remove_fd (fd_); return ((zmq::socket_poller_t *) poller_)->remove_fd (fd_);
} }
int zmq_poller_wait (void *poller_, zmq_poller_event_t *event_, long timeout_) int zmq_poller_wait (void *poller_, zmq_poller_event_t *event_, long timeout_)
{ {
if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) {
errno = EFAULT;
return -1;
}
if (!event_) {
errno = EFAULT;
return -1;
}
int rc = zmq_poller_wait_all (poller_, event_, 1, timeout_); int rc = zmq_poller_wait_all (poller_, event_, 1, timeout_);
if (rc < 0) { if (rc < 0 && event_) {
// TODO this is not portable... zmq_poller_event_t contains pointers,
// for which nullptr does not need to be represented by all-zeroes
memset (event_, 0, sizeof (zmq_poller_event_t)); memset (event_, 0, sizeof (zmq_poller_event_t));
} }
// wait_all returns number of events, but we return 0 for any success // wait_all returns number of events, but we return 0 for any success
...@@ -1322,10 +1321,9 @@ int zmq_poller_wait_all (void *poller_, ...@@ -1322,10 +1321,9 @@ int zmq_poller_wait_all (void *poller_,
int n_events, int n_events,
long timeout_) long timeout_)
{ {
if (!poller_ || !((zmq::socket_poller_t *) poller_)->check_tag ()) { if (-1 == check_poller (poller_))
errno = EFAULT;
return -1; return -1;
}
if (!events_) { if (!events_) {
errno = EFAULT; errno = EFAULT;
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment