Skip to content

Commit a79a7f4

Browse files
committed
Problem: zmq_poller_[add/modify] accept invalid events arguments silently
Solution: check and return an error on invalid arguments. Fixes zeromq#3088
1 parent f3b69c5 commit a79a7f4

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/zmq.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,13 @@ static int check_poller (void *const poller_)
11971197
return 0;
11981198
}
11991199

1200+
static int check_events (const short events_)
1201+
{
1202+
return (events_ & ~(ZMQ_POLLIN | ZMQ_POLLOUT | ZMQ_POLLERR | ZMQ_POLLPRI))
1203+
? -1
1204+
: 0;
1205+
}
1206+
12001207
static int check_poller_registration_args (void *const poller_, void *const s_)
12011208
{
12021209
if (-1 == check_poller (poller_))
@@ -1231,7 +1238,8 @@ static int check_poller_fd_registration_args (void *const poller_,
12311238

12321239
int zmq_poller_add (void *poller_, void *s_, void *user_data_, short events_)
12331240
{
1234-
if (-1 == check_poller_registration_args (poller_, s_))
1241+
if (-1 == check_poller_registration_args (poller_, s_)
1242+
|| -1 == check_events (events_))
12351243
return -1;
12361244

12371245
zmq::socket_base_t *socket = (zmq::socket_base_t *) s_;
@@ -1249,7 +1257,8 @@ int zmq_poller_add_fd (void *poller_,
12491257
int zmq_poller_add_fd (void *poller_, int fd_, void *user_data_, short events_)
12501258
#endif
12511259
{
1252-
if (-1 == check_poller_fd_registration_args (poller_, fd_))
1260+
if (-1 == check_poller_fd_registration_args (poller_, fd_)
1261+
|| -1 == check_events (events_))
12531262
return -1;
12541263

12551264
return ((zmq::socket_poller_t *) poller_)
@@ -1259,7 +1268,8 @@ int zmq_poller_add_fd (void *poller_, int fd_, void *user_data_, short events_)
12591268

12601269
int zmq_poller_modify (void *poller_, void *s_, short events_)
12611270
{
1262-
if (-1 == check_poller_registration_args (poller_, s_))
1271+
if (-1 == check_poller_registration_args (poller_, s_)
1272+
|| -1 == check_events (events_))
12631273
return -1;
12641274

12651275
zmq::socket_base_t *socket = (zmq::socket_base_t *) s_;
@@ -1274,7 +1284,8 @@ int zmq_poller_modify_fd (void *poller_, SOCKET fd_, short events_)
12741284
int zmq_poller_modify_fd (void *poller_, int fd_, short events_)
12751285
#endif
12761286
{
1277-
if (-1 == check_poller_fd_registration_args (poller_, fd_))
1287+
if (-1 == check_poller_fd_registration_args (poller_, fd_)
1288+
|| -1 == check_events (events_))
12781289
return -1;
12791290

12801291
return ((zmq::socket_poller_t *) poller_)->modify_fd (fd_, events_);

tests/test_poller.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "testutil_unity.hpp"
3232

3333
#include <unity.h>
34+
#include <limits.h>
3435

3536
void setUp ()
3637
{
@@ -393,6 +394,42 @@ void call_poller_modify_fd_unregistered_fails (void *poller,
393394
TEST_ASSERT_SUCCESS_ERRNO (close (plain_socket));
394395
}
395396

397+
void call_poller_add_invalid_events_fails (void *poller, void *zeromq_socket)
398+
{
399+
TEST_ASSERT_FAILURE_ERRNO (
400+
EINVAL, zmq_poller_add (poller, zeromq_socket, NULL, SHRT_MAX));
401+
}
402+
403+
void call_poller_modify_invalid_events_fails (void *poller, void *zeromq_socket)
404+
{
405+
TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_add (poller, zeromq_socket, NULL, 0));
406+
407+
TEST_ASSERT_FAILURE_ERRNO (
408+
EINVAL, zmq_poller_modify (poller, zeromq_socket, SHRT_MAX));
409+
}
410+
411+
void call_poller_add_fd_invalid_events_fails (void *poller,
412+
void * /*zeromq_socket*/)
413+
{
414+
fd_t plain_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
415+
TEST_ASSERT_FAILURE_ERRNO (
416+
EINVAL, zmq_poller_add_fd (poller, plain_socket, NULL, SHRT_MAX));
417+
418+
TEST_ASSERT_SUCCESS_ERRNO (close (plain_socket));
419+
}
420+
421+
void call_poller_modify_fd_invalid_events_fails (void *poller,
422+
void * /*zeromq_socket*/)
423+
{
424+
fd_t plain_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
425+
TEST_ASSERT_SUCCESS_ERRNO (
426+
zmq_poller_add_fd (poller, plain_socket, NULL, 0));
427+
TEST_ASSERT_FAILURE_ERRNO (
428+
EINVAL, zmq_poller_modify_fd (poller, plain_socket, SHRT_MAX));
429+
430+
TEST_ASSERT_SUCCESS_ERRNO (close (plain_socket));
431+
}
432+
396433
TEST_CASE_FUNC_PARAM (call_poller_add_twice_fails, test_with_empty_poller)
397434
TEST_CASE_FUNC_PARAM (call_poller_remove_unregistered_fails,
398435
test_with_empty_poller)
@@ -406,6 +443,15 @@ TEST_CASE_FUNC_PARAM (call_poller_remove_fd_unregistered_fails,
406443
TEST_CASE_FUNC_PARAM (call_poller_modify_fd_unregistered_fails,
407444
test_with_empty_poller)
408445

446+
TEST_CASE_FUNC_PARAM (call_poller_add_invalid_events_fails,
447+
test_with_empty_poller)
448+
TEST_CASE_FUNC_PARAM (call_poller_modify_invalid_events_fails,
449+
test_with_empty_poller)
450+
TEST_CASE_FUNC_PARAM (call_poller_add_fd_invalid_events_fails,
451+
test_with_empty_poller)
452+
TEST_CASE_FUNC_PARAM (call_poller_modify_fd_invalid_events_fails,
453+
test_with_empty_poller)
454+
409455
void call_poller_wait_empty_with_timeout_fails (void *poller, void * /*socket*/)
410456
{
411457
zmq_poller_event_t event;
@@ -618,6 +664,10 @@ int main (void)
618664
RUN_TEST (test_call_poller_add_fd_twice_fails);
619665
RUN_TEST (test_call_poller_remove_fd_unregistered_fails);
620666
RUN_TEST (test_call_poller_modify_fd_unregistered_fails);
667+
RUN_TEST (test_call_poller_add_invalid_events_fails);
668+
RUN_TEST (test_call_poller_modify_invalid_events_fails);
669+
RUN_TEST (test_call_poller_add_fd_invalid_events_fails);
670+
RUN_TEST (test_call_poller_modify_fd_invalid_events_fails);
621671

622672
RUN_TEST (test_call_poller_wait_empty_with_timeout_fails);
623673
RUN_TEST (test_call_poller_wait_empty_without_timeout_fails);

0 commit comments

Comments
 (0)