Commit 48423c9e authored by Pieter Hintjens's avatar Pieter Hintjens

Merge pull request #242 from niXman/master

more checks for parameters of API functions
parents 256fa5d2 e646ce43
...@@ -235,6 +235,10 @@ int zmq_setsockopt (void *s_, int option_, const void *optval_, ...@@ -235,6 +235,10 @@ int zmq_setsockopt (void *s_, int option_, const void *optval_,
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
if (optvallen_ && !(optval_)) {
errno = EINVAL;
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_; zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
if(s->thread_safe()) s->lock(); if(s->thread_safe()) s->lock();
int result = s->setsockopt (option_, optval_, optvallen_); int result = s->setsockopt (option_, optval_, optvallen_);
...@@ -248,6 +252,10 @@ int zmq_getsockopt (void *s_, int option_, void *optval_, size_t *optvallen_) ...@@ -248,6 +252,10 @@ int zmq_getsockopt (void *s_, int option_, void *optval_, size_t *optvallen_)
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
if (optvallen_ && *optvallen_ && !(optval_)) {
errno = EINVAL;
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_; zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
if(s->thread_safe()) s->lock(); if(s->thread_safe()) s->lock();
int result = s->getsockopt (option_, optval_, optvallen_); int result = s->getsockopt (option_, optval_, optvallen_);
...@@ -285,6 +293,14 @@ int zmq_connect (void *s_, const char *addr_) ...@@ -285,6 +293,14 @@ int zmq_connect (void *s_, const char *addr_)
static int inner_sendmsg (zmq::socket_base_t *s_, zmq_msg_t *msg_, int flags_) static int inner_sendmsg (zmq::socket_base_t *s_, zmq_msg_t *msg_, int flags_)
{ {
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
return -1;
}
if (!msg_) {
errno = EINVAL;
return -1;
}
int sz = (int) zmq_msg_size (msg_); int sz = (int) zmq_msg_size (msg_);
int rc = s_->send ((zmq::msg_t*) msg_, flags_); int rc = s_->send ((zmq::msg_t*) msg_, flags_);
if (unlikely (rc < 0)) if (unlikely (rc < 0))
...@@ -298,6 +314,10 @@ int zmq_sendmsg (void *s_, zmq_msg_t *msg_, int flags_) ...@@ -298,6 +314,10 @@ int zmq_sendmsg (void *s_, zmq_msg_t *msg_, int flags_)
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
if (!msg_) {
errno = EINVAL;
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_; zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
if(s->thread_safe()) s->lock(); if(s->thread_safe()) s->lock();
int result = inner_sendmsg (s, msg_, flags_); int result = inner_sendmsg (s, msg_, flags_);
...@@ -311,6 +331,10 @@ int zmq_send (void *s_, const void *buf_, size_t len_, int flags_) ...@@ -311,6 +331,10 @@ int zmq_send (void *s_, const void *buf_, size_t len_, int flags_)
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
if (!buf_ || !len_) {
errno = EINVAL;
return -1;
}
zmq_msg_t msg; zmq_msg_t msg;
int rc = zmq_msg_init_size (&msg, len_); int rc = zmq_msg_init_size (&msg, len_);
if (rc != 0) if (rc != 0)
...@@ -346,6 +370,11 @@ int zmq_sendv (void *s_, iovec *a_, size_t count_, int flags_) ...@@ -346,6 +370,11 @@ int zmq_sendv (void *s_, iovec *a_, size_t count_, int flags_)
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
if (!a_) {
errno = EINVAL;
return -1;
}
int rc = 0; int rc = 0;
zmq_msg_t msg; zmq_msg_t msg;
zmq::socket_base_t *s = (zmq::socket_base_t *) s_; zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
...@@ -377,9 +406,14 @@ int zmq_sendv (void *s_, iovec *a_, size_t count_, int flags_) ...@@ -377,9 +406,14 @@ int zmq_sendv (void *s_, iovec *a_, size_t count_, int flags_)
// Receiving functions. // Receiving functions.
static int inner_recvmsg (zmq::socket_base_t *s_, zmq_msg_t *msg_, int flags_) int inner_recvmsg (void *s_, zmq_msg_t *msg_, int flags_)
{ {
int rc = s_->recv ((zmq::msg_t*) msg_, flags_); if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int rc = s->recv ((zmq::msg_t*) msg_, flags_);
if (unlikely (rc < 0)) if (unlikely (rc < 0))
return -1; return -1;
return (int) zmq_msg_size (msg_); return (int) zmq_msg_size (msg_);
...@@ -405,6 +439,11 @@ int zmq_recv (void *s_, void *buf_, size_t len_, int flags_) ...@@ -405,6 +439,11 @@ int zmq_recv (void *s_, void *buf_, size_t len_, int flags_)
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
if (!buf_ || !len_) {
errno = EINVAL;
return -1;
}
zmq_msg_t msg; zmq_msg_t msg;
int rc = zmq_msg_init (&msg); int rc = zmq_msg_init (&msg);
errno_assert (rc == 0); errno_assert (rc == 0);
...@@ -458,6 +497,11 @@ int zmq_recvmmsg (void *s_, iovec *a_, size_t *count_, int flags_) ...@@ -458,6 +497,11 @@ int zmq_recvmmsg (void *s_, iovec *a_, size_t *count_, int flags_)
errno = ENOTSOCK; errno = ENOTSOCK;
return -1; return -1;
} }
if (!a_ || !count_ || (a_ && !(*count_))) {
errno = EINVAL;
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_; zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
if(s->thread_safe()) s->lock(); if(s->thread_safe()) s->lock();
...@@ -482,7 +526,7 @@ int zmq_recvmmsg (void *s_, iovec *a_, size_t *count_, int flags_) ...@@ -482,7 +526,7 @@ int zmq_recvmmsg (void *s_, iovec *a_, size_t *count_, int flags_)
nread = -1; nread = -1;
break; break;
} }
++*count_; ++(*count_);
++nread; ++nread;
// Cheat: acquire zmq_msg buffer. // Cheat: acquire zmq_msg buffer.
...@@ -500,48 +544,84 @@ int zmq_recvmmsg (void *s_, iovec *a_, size_t *count_, int flags_) ...@@ -500,48 +544,84 @@ int zmq_recvmmsg (void *s_, iovec *a_, size_t *count_, int flags_)
int zmq_msg_init (zmq_msg_t *msg_) int zmq_msg_init (zmq_msg_t *msg_)
{ {
if (!msg_) {
errno = EINVAL;
return -1;
}
return ((zmq::msg_t*) msg_)->init (); return ((zmq::msg_t*) msg_)->init ();
} }
int zmq_msg_init_size (zmq_msg_t *msg_, size_t size_) int zmq_msg_init_size (zmq_msg_t *msg_, size_t size_)
{ {
if (!msg_ || !size_) {
errno = EINVAL;
return -1;
}
return ((zmq::msg_t*) msg_)->init_size (size_); return ((zmq::msg_t*) msg_)->init_size (size_);
} }
int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_, int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_,
zmq_free_fn *ffn_, void *hint_) zmq_free_fn *ffn_, void *hint_)
{ {
if (!msg_ || !data_ || !size_) {
errno = EINVAL;
return -1;
}
return ((zmq::msg_t*) msg_)->init_data (data_, size_, ffn_, hint_); return ((zmq::msg_t*) msg_)->init_data (data_, size_, ffn_, hint_);
} }
int zmq_msg_close (zmq_msg_t *msg_) int zmq_msg_close (zmq_msg_t *msg_)
{ {
if (!msg_) {
errno = EINVAL;
return -1;
}
return ((zmq::msg_t*) msg_)->close (); return ((zmq::msg_t*) msg_)->close ();
} }
int zmq_msg_move (zmq_msg_t *dest_, zmq_msg_t *src_) int zmq_msg_move (zmq_msg_t *dest_, zmq_msg_t *src_)
{ {
if (!dest_ || !src_) {
errno = EINVAL;
return -1;
}
return ((zmq::msg_t*) dest_)->move (*(zmq::msg_t*) src_); return ((zmq::msg_t*) dest_)->move (*(zmq::msg_t*) src_);
} }
int zmq_msg_copy (zmq_msg_t *dest_, zmq_msg_t *src_) int zmq_msg_copy (zmq_msg_t *dest_, zmq_msg_t *src_)
{ {
if (!dest_ || !src_) {
errno = EINVAL;
return -1;
}
return ((zmq::msg_t*) dest_)->copy (*(zmq::msg_t*) src_); return ((zmq::msg_t*) dest_)->copy (*(zmq::msg_t*) src_);
} }
void *zmq_msg_data (zmq_msg_t *msg_) void *zmq_msg_data (zmq_msg_t *msg_)
{ {
if (!msg_) {
errno = EINVAL;
return NULL;
}
return ((zmq::msg_t*) msg_)->data (); return ((zmq::msg_t*) msg_)->data ();
} }
size_t zmq_msg_size (zmq_msg_t *msg_) size_t zmq_msg_size (zmq_msg_t *msg_)
{ {
if (!msg_) {
errno = EINVAL;
return -1;
}
return ((zmq::msg_t*) msg_)->size (); return ((zmq::msg_t*) msg_)->size ();
} }
int zmq_getmsgopt (zmq_msg_t *msg_, int option_, void *optval_, int zmq_getmsgopt (zmq_msg_t *msg_, int option_, void *optval_,
size_t *optvallen_) size_t *optvallen_)
{ {
if (!msg_ || !optval_ || !optvallen_) {
errno = EINVAL;
return -1;
}
switch (option_) { switch (option_) {
case ZMQ_MORE: case ZMQ_MORE:
if (*optvallen_ < sizeof (int)) { if (*optvallen_ < sizeof (int)) {
...@@ -562,6 +642,11 @@ int zmq_getmsgopt (zmq_msg_t *msg_, int option_, void *optval_, ...@@ -562,6 +642,11 @@ int zmq_getmsgopt (zmq_msg_t *msg_, int option_, void *optval_,
int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_) int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
{ {
if (!items_) {
errno = EFAULT;
return -1;
}
#if defined ZMQ_POLL_BASED_ON_POLL #if defined ZMQ_POLL_BASED_ON_POLL
if (unlikely (nitems_ < 0)) { if (unlikely (nitems_ < 0)) {
errno = EINVAL; errno = EINVAL;
...@@ -581,11 +666,6 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_) ...@@ -581,11 +666,6 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
#endif #endif
} }
if (!items_) {
errno = EFAULT;
return -1;
}
zmq::clock_t clock; zmq::clock_t clock;
uint64_t now = 0; uint64_t now = 0;
uint64_t end = 0; uint64_t end = 0;
......
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