From ee1f1af0091d9bdffa0e5ce1783da925b3cd7e56 Mon Sep 17 00:00:00 2001
From: Martin Lucina <mato@kotelna.sk>
Date: Sat, 7 Aug 2010 21:04:30 +0200
Subject: zmq_poll(): Fix some corner cases

Trying to optimize out the case where items_[i]. events is 0 would
result in a bogus pollfds[i]. Similarly in the select()-based impl,
while not strictly necessary it's better to get ZMQ_FD even if
events is 0 since that detects ETERM and friends.
---
 src/zmq.cpp | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

(limited to 'src')

diff --git a/src/zmq.cpp b/src/zmq.cpp
index 6ec8fbe..1a74f86 100644
--- a/src/zmq.cpp
+++ b/src/zmq.cpp
@@ -382,14 +382,14 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
 
         //  If the poll item is a 0MQ socket, we poll on the file descriptor
         //  retrieved by the ZMQ_FD socket option.
-        if (items_ [i].socket && items_ [i].events) {
+        if (items_ [i].socket) {
             size_t zmq_fd_size = sizeof (zmq::fd_t);
             if (zmq_getsockopt (items_ [i].socket, ZMQ_FD, &pollfds [i].fd,
                 &zmq_fd_size) == -1) {
                 free (pollfds);
                 return -1;
             }
-            pollfds [i].events = POLLIN;
+            pollfds [i].events = items_ [i].events ? POLLIN : 0;
         }
         //  Else, the poll item is a raw file descriptor. Just convert the
         //  events to normal POLLIN/POLLOUT for poll ().
@@ -474,15 +474,17 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
 
         //  If the poll item is a 0MQ socket we are interested in input on the
         //  notification file descriptor retrieved by the ZMQ_FD socket option.
-        if (items_ [i].socket && items_ [i].events) {
+        if (items_ [i].socket) {
             size_t zmq_fd_size = sizeof (zmq::fd_t);
             zmq::fd_t notify_fd;
             if (zmq_getsockopt (items_ [i].socket, ZMQ_FD, &notify_fd,
                 &zmq_fd_size) == -1)
                 return -1;
-            FD_SET (notify_fd, &pollset_in);
-            if (maxfd < notify_fd)
-                maxfd = notify_fd;
+            if (items_ [i].events) {
+                FD_SET (notify_fd, &pollset_in);
+                if (maxfd < notify_fd)
+                    maxfd = notify_fd;
+            }
         }
         //  Else, the poll item is a raw file descriptor. Convert the poll item
         //  events to the appropriate fd_sets.
-- 
cgit v1.2.3