From d38951db8738d1cd7bf511decbee590e29a78fa8 Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Wed, 15 Feb 2012 13:03:40 -0600 Subject: [PATCH 1/6] Return EFAULT if required arguments are null --- src/zmq.cpp | 65 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/src/zmq.cpp b/src/zmq.cpp index 6dba8ac3..a0ee5655 100644 --- a/src/zmq.cpp +++ b/src/zmq.cpp @@ -300,6 +300,10 @@ int zmq_sendmsg (void *s_, zmq_msg_t *msg_, int flags_) errno = ENOTSOCK; return -1; } + if (!msg_) { + errno = EFAULT; + return -1; + } zmq::socket_base_t *s = (zmq::socket_base_t *) s_; if(s->thread_safe()) s->lock(); int result = inner_sendmsg (s, msg_, flags_); @@ -393,6 +397,10 @@ int zmq_recvmsg (void *s_, zmq_msg_t *msg_, int flags_) errno = ENOTSOCK; return -1; } + if (!msg_) { + errno = EFAULT; + return -1; + } zmq::socket_base_t *s = (zmq::socket_base_t *) s_; if(s->thread_safe()) s->lock(); int result = inner_recvmsg(s, msg_, flags_); @@ -502,68 +510,95 @@ int zmq_recvmmsg (void *s_, iovec *a_, size_t *count_, int flags_) int zmq_msg_init (zmq_msg_t *msg_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->init (); } int zmq_msg_init_size (zmq_msg_t *msg_, size_t size_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->init_size (size_); } int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_, zmq_free_fn *ffn_, void *hint_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->init_data (data_, size_, ffn_, hint_); } int zmq_msg_close (zmq_msg_t *msg_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->close (); } int zmq_msg_move (zmq_msg_t *dest_, zmq_msg_t *src_) { + if (!dest_ || !src_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) dest_)->move (*(zmq::msg_t*) src_); } int zmq_msg_copy (zmq_msg_t *dest_, zmq_msg_t *src_) { + if (!dest_ || !src_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) dest_)->copy (*(zmq::msg_t*) src_); } void *zmq_msg_data (zmq_msg_t *msg_) { + if (!msg_) { + errno = EFAULT; + return NULL; + } return ((zmq::msg_t*) msg_)->data (); } size_t zmq_msg_size (zmq_msg_t *msg_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->size (); } int zmq_getmsgopt (zmq_msg_t *msg_, int option_, void *optval_, size_t *optvallen_) { - switch (option_) { - case ZMQ_MORE: - if (*optvallen_ < sizeof (int)) { - errno = EINVAL; - return -1; - } - *((int*) optval_) = - (((zmq::msg_t*) msg_)->flags () & zmq::msg_t::more) ? 1 : 0; - *optvallen_ = sizeof (int); - return 0; - default: - errno = EINVAL; + if (!msg_) { + errno = EFAULT; return -1; } + return (((zmq::msg_t*) msg_)->flags () & zmq::msg_t::more)? 1: 0; } // Polling. 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 (unlikely (nitems_ < 0)) { errno = EINVAL; @@ -582,12 +617,6 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_) return usleep (timeout_ * 1000); #endif } - - if (!items_) { - errno = EFAULT; - return -1; - } - zmq::clock_t clock; uint64_t now = 0; uint64_t end = 0; From 6b2ec366b1b96da14260000939946b12a7b085c6 Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Wed, 15 Feb 2012 15:17:01 -0600 Subject: [PATCH 2/6] Changed return type of zmq_msg_size to ssize_t to allow error return --- include/zmq.h | 2 +- src/zmq.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/zmq.h b/include/zmq.h index 23c6bee0..f2f36c2b 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -165,7 +165,7 @@ ZMQ_EXPORT int zmq_msg_close (zmq_msg_t *msg); ZMQ_EXPORT int zmq_msg_move (zmq_msg_t *dest, zmq_msg_t *src); ZMQ_EXPORT int zmq_msg_copy (zmq_msg_t *dest, zmq_msg_t *src); ZMQ_EXPORT void *zmq_msg_data (zmq_msg_t *msg); -ZMQ_EXPORT size_t zmq_msg_size (zmq_msg_t *msg); +ZMQ_EXPORT ssize_t zmq_msg_size (zmq_msg_t *msg); ZMQ_EXPORT int zmq_getmsgopt (zmq_msg_t *msg, int option, void *optval, size_t *optvallen); diff --git a/src/zmq.cpp b/src/zmq.cpp index a0ee5655..3d3e70a1 100644 --- a/src/zmq.cpp +++ b/src/zmq.cpp @@ -572,7 +572,7 @@ void *zmq_msg_data (zmq_msg_t *msg_) return ((zmq::msg_t*) msg_)->data (); } -size_t zmq_msg_size (zmq_msg_t *msg_) +ssize_t zmq_msg_size (zmq_msg_t *msg_) { if (!msg_) { errno = EFAULT; From 07b49ffb9c30af3c8a4e486dc4225784f4a1dda8 Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Thu, 16 Feb 2012 12:04:25 -0600 Subject: [PATCH 3/6] Fixed up all references to zmq_msg_size --- doc/zmq_msg_size.txt | 16 ++++++++++++---- include/zmq.h | 1 + perf/inproc_lat.cpp | 4 ++-- perf/inproc_thr.cpp | 4 ++-- perf/local_lat.cpp | 4 ++-- perf/local_thr.cpp | 4 ++-- perf/remote_lat.cpp | 4 ++-- 7 files changed, 23 insertions(+), 14 deletions(-) diff --git a/doc/zmq_msg_size.txt b/doc/zmq_msg_size.txt index 354ca284..75f5f1ce 100644 --- a/doc/zmq_msg_size.txt +++ b/doc/zmq_msg_size.txt @@ -9,7 +9,7 @@ zmq_msg_size - retrieve message content size in bytes SYNOPSIS -------- -*size_t zmq_msg_size (zmq_msg_t '*msg');* +*ssize_t zmq_msg_size (zmq_msg_t '*msg');* DESCRIPTION @@ -29,7 +29,15 @@ message content in bytes. ERRORS ------ -No errors are defined. +The _zmq_msg_size()_ function shall return a positive integer (0 or higher) +if successful. Otherwise it shall return `-1` and set 'errno' to one of the +values defined below. + + +ERRORS +------ +*EFAULT*:: +The provided 'msg' was NULL. SEE ALSO @@ -44,5 +52,5 @@ linkzmq:zmq[7] AUTHORS ------- -This 0MQ manual page was written by Martin Sustrik and -Martin Lucina . +This 0MQ manual page was written by Martin Sustrik , +Martin Lucina , and Pieter Hintjens . diff --git a/include/zmq.h b/include/zmq.h index f2f36c2b..ab9e3f76 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -29,6 +29,7 @@ extern "C" { #include #include +#include #if defined _WIN32 #include #endif diff --git a/perf/inproc_lat.cpp b/perf/inproc_lat.cpp index 5b6a830c..e19b2366 100644 --- a/perf/inproc_lat.cpp +++ b/perf/inproc_lat.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -35,7 +35,7 @@ #include #endif -static size_t message_size; +static ssize_t message_size; static int roundtrip_count; #if defined ZMQ_HAVE_WINDOWS diff --git a/perf/inproc_thr.cpp b/perf/inproc_thr.cpp index b4cadfcc..b4fe8024 100644 --- a/perf/inproc_thr.cpp +++ b/perf/inproc_thr.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -36,7 +36,7 @@ #endif static int message_count; -static size_t message_size; +static ssize_t message_size; #if defined ZMQ_HAVE_WINDOWS static unsigned int __stdcall worker (void *ctx_) diff --git a/perf/local_lat.cpp b/perf/local_lat.cpp index 714b8c0f..9811fa3b 100644 --- a/perf/local_lat.cpp +++ b/perf/local_lat.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -28,7 +28,7 @@ int main (int argc, char *argv []) { const char *bind_to; int roundtrip_count; - size_t message_size; + ssize_t message_size; void *ctx; void *s; int rc; diff --git a/perf/local_thr.cpp b/perf/local_thr.cpp index 5c495d83..451c3b88 100644 --- a/perf/local_thr.cpp +++ b/perf/local_thr.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -28,7 +28,7 @@ int main (int argc, char *argv []) { const char *bind_to; int message_count; - size_t message_size; + ssize_t message_size; void *ctx; void *s; int rc; diff --git a/perf/remote_lat.cpp b/perf/remote_lat.cpp index 9eb76b0b..07979985 100644 --- a/perf/remote_lat.cpp +++ b/perf/remote_lat.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -29,7 +29,7 @@ int main (int argc, char *argv []) { const char *connect_to; int roundtrip_count; - size_t message_size; + ssize_t message_size; void *ctx; void *s; int rc; From dc09da456936e84e68e220a8c950e1abc2ebbd0b Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Wed, 15 Feb 2012 13:03:40 -0600 Subject: [PATCH 4/6] Return EFAULT if required arguments are null --- src/zmq.cpp | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/zmq.cpp b/src/zmq.cpp index 7058f61e..f60a42b8 100644 --- a/src/zmq.cpp +++ b/src/zmq.cpp @@ -488,17 +488,29 @@ int zmq_recvmmsg (void *s_, iovec *a_, size_t *count_, int flags_) int zmq_msg_init (zmq_msg_t *msg_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->init (); } int zmq_msg_init_size (zmq_msg_t *msg_, size_t size_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->init_size (size_); } int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_, zmq_free_fn *ffn_, void *hint_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->init_data (data_, size_, ffn_, hint_); } @@ -530,26 +542,46 @@ int zmq_msg_recv (zmq_msg_t *msg_, void *s_, int flags_) int zmq_msg_close (zmq_msg_t *msg_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->close (); } int zmq_msg_move (zmq_msg_t *dest_, zmq_msg_t *src_) { + if (!dest_ || !src_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) dest_)->move (*(zmq::msg_t*) src_); } int zmq_msg_copy (zmq_msg_t *dest_, zmq_msg_t *src_) { + if (!dest_ || !src_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) dest_)->copy (*(zmq::msg_t*) src_); } void *zmq_msg_data (zmq_msg_t *msg_) { + if (!msg_) { + errno = EFAULT; + return NULL; + } return ((zmq::msg_t*) msg_)->data (); } size_t zmq_msg_size (zmq_msg_t *msg_) { + if (!msg_) { + errno = EFAULT; + return -1; + } return ((zmq::msg_t*) msg_)->size (); } @@ -601,6 +633,10 @@ int zmq_msg_set (zmq_msg_t *msg_, int option_, const void *optval_, 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 (unlikely (nitems_ < 0)) { errno = EINVAL; @@ -619,12 +655,6 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_) return usleep (timeout_ * 1000); #endif } - - if (!items_) { - errno = EFAULT; - return -1; - } - zmq::clock_t clock; uint64_t now = 0; uint64_t end = 0; From 02b81d42ce2c3b6fabcfe1bcdc6fa8bceed9762a Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Wed, 15 Feb 2012 15:17:01 -0600 Subject: [PATCH 5/6] Changed return type of zmq_msg_size to ssize_t to allow error return --- include/zmq.h | 4 ++-- src/zmq.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/zmq.h b/include/zmq.h index 9b86786b..d6d44682 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -167,14 +167,14 @@ ZMQ_EXPORT int zmq_msg_close (zmq_msg_t *msg); ZMQ_EXPORT int zmq_msg_move (zmq_msg_t *dest, zmq_msg_t *src); ZMQ_EXPORT int zmq_msg_copy (zmq_msg_t *dest, zmq_msg_t *src); ZMQ_EXPORT void *zmq_msg_data (zmq_msg_t *msg); -ZMQ_EXPORT size_t zmq_msg_size (zmq_msg_t *msg); + +ZMQ_EXPORT ssize_t zmq_msg_size (zmq_msg_t *msg); ZMQ_EXPORT int zmq_msg_more (zmq_msg_t *msg); ZMQ_EXPORT int zmq_msg_get (zmq_msg_t *msg, int option, void *optval, size_t *optvallen); ZMQ_EXPORT int zmq_msg_set (zmq_msg_t *msg, int option, const void *optval, size_t *optvallen); - /******************************************************************************/ /* 0MQ infrastructure (a.k.a. context) initialisation & termination. */ /******************************************************************************/ diff --git a/src/zmq.cpp b/src/zmq.cpp index f60a42b8..98da24ed 100644 --- a/src/zmq.cpp +++ b/src/zmq.cpp @@ -576,7 +576,7 @@ void *zmq_msg_data (zmq_msg_t *msg_) return ((zmq::msg_t*) msg_)->data (); } -size_t zmq_msg_size (zmq_msg_t *msg_) +ssize_t zmq_msg_size (zmq_msg_t *msg_) { if (!msg_) { errno = EFAULT; From 0efb49f12fdec061b267a7526cbaa8d149d8c254 Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Thu, 16 Feb 2012 12:04:25 -0600 Subject: [PATCH 6/6] Fixed up all references to zmq_msg_size --- doc/zmq_msg_size.txt | 16 ++++++++++++---- include/zmq.h | 1 + perf/inproc_lat.cpp | 4 ++-- perf/inproc_thr.cpp | 4 ++-- perf/local_lat.cpp | 4 ++-- perf/local_thr.cpp | 4 ++-- perf/remote_lat.cpp | 4 ++-- 7 files changed, 23 insertions(+), 14 deletions(-) diff --git a/doc/zmq_msg_size.txt b/doc/zmq_msg_size.txt index 354ca284..75f5f1ce 100644 --- a/doc/zmq_msg_size.txt +++ b/doc/zmq_msg_size.txt @@ -9,7 +9,7 @@ zmq_msg_size - retrieve message content size in bytes SYNOPSIS -------- -*size_t zmq_msg_size (zmq_msg_t '*msg');* +*ssize_t zmq_msg_size (zmq_msg_t '*msg');* DESCRIPTION @@ -29,7 +29,15 @@ message content in bytes. ERRORS ------ -No errors are defined. +The _zmq_msg_size()_ function shall return a positive integer (0 or higher) +if successful. Otherwise it shall return `-1` and set 'errno' to one of the +values defined below. + + +ERRORS +------ +*EFAULT*:: +The provided 'msg' was NULL. SEE ALSO @@ -44,5 +52,5 @@ linkzmq:zmq[7] AUTHORS ------- -This 0MQ manual page was written by Martin Sustrik and -Martin Lucina . +This 0MQ manual page was written by Martin Sustrik , +Martin Lucina , and Pieter Hintjens . diff --git a/include/zmq.h b/include/zmq.h index d6d44682..a42576d7 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -29,6 +29,7 @@ extern "C" { #include #include +#include #if defined _WIN32 #include #endif diff --git a/perf/inproc_lat.cpp b/perf/inproc_lat.cpp index 5b6a830c..e19b2366 100644 --- a/perf/inproc_lat.cpp +++ b/perf/inproc_lat.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -35,7 +35,7 @@ #include #endif -static size_t message_size; +static ssize_t message_size; static int roundtrip_count; #if defined ZMQ_HAVE_WINDOWS diff --git a/perf/inproc_thr.cpp b/perf/inproc_thr.cpp index b4cadfcc..b4fe8024 100644 --- a/perf/inproc_thr.cpp +++ b/perf/inproc_thr.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -36,7 +36,7 @@ #endif static int message_count; -static size_t message_size; +static ssize_t message_size; #if defined ZMQ_HAVE_WINDOWS static unsigned int __stdcall worker (void *ctx_) diff --git a/perf/local_lat.cpp b/perf/local_lat.cpp index 714b8c0f..9811fa3b 100644 --- a/perf/local_lat.cpp +++ b/perf/local_lat.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -28,7 +28,7 @@ int main (int argc, char *argv []) { const char *bind_to; int roundtrip_count; - size_t message_size; + ssize_t message_size; void *ctx; void *s; int rc; diff --git a/perf/local_thr.cpp b/perf/local_thr.cpp index 5c495d83..451c3b88 100644 --- a/perf/local_thr.cpp +++ b/perf/local_thr.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -28,7 +28,7 @@ int main (int argc, char *argv []) { const char *bind_to; int message_count; - size_t message_size; + ssize_t message_size; void *ctx; void *s; int rc; diff --git a/perf/remote_lat.cpp b/perf/remote_lat.cpp index 9eb76b0b..07979985 100644 --- a/perf/remote_lat.cpp +++ b/perf/remote_lat.cpp @@ -1,6 +1,6 @@ /* + Copyright (c) 2007-2012 iMatix Corporation Copyright (c) 2009-2011 250bpm s.r.o. - Copyright (c) 2007-2009 iMatix Corporation Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -29,7 +29,7 @@ int main (int argc, char *argv []) { const char *connect_to; int roundtrip_count; - size_t message_size; + ssize_t message_size; void *ctx; void *s; int rc;