mirror of
https://github.com/zeromq/libzmq.git
synced 2025-01-14 01:37:56 +08:00
Problem: zmq_poller_*_fd functions do not check for invalid fd
Solution: add checks, added test cases
This commit is contained in:
parent
f9af5503b4
commit
2e4fc4faf0
12
src/zmq.cpp
12
src/zmq.cpp
@ -1269,6 +1269,10 @@ int zmq_poller_add_fd (void *poller_, int fd_, void *user_data_, short events_)
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
if (fd_ == zmq::retired_fd) {
|
||||
errno = ENOTSOCK;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ((zmq::socket_poller_t*)poller_)->add_fd (fd_, user_data_, events_);
|
||||
}
|
||||
@ -1301,6 +1305,10 @@ int zmq_poller_modify_fd (void *poller_, int fd_, short events_)
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
if (fd_ == zmq::retired_fd) {
|
||||
errno = ENOTSOCK;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ((zmq::socket_poller_t*)poller_)->modify_fd (fd_, events_);
|
||||
}
|
||||
@ -1332,6 +1340,10 @@ int zmq_poller_remove_fd (void *poller_, int fd_)
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
if (fd_ == zmq::retired_fd) {
|
||||
errno = ENOTSOCK;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ((zmq::socket_poller_t*)poller_)->remove_fd (fd_);
|
||||
}
|
||||
|
@ -29,6 +29,20 @@
|
||||
|
||||
#include "testutil.hpp"
|
||||
|
||||
// duplicated from fd.hpp
|
||||
#ifdef ZMQ_HAVE_WINDOWS
|
||||
#if defined _MSC_VER &&_MSC_VER <= 1400
|
||||
typedef UINT_PTR fd_t;
|
||||
enum {retired_fd = (fd_t)(~0)};
|
||||
#else
|
||||
typedef SOCKET fd_t;
|
||||
enum {retired_fd = (fd_t)INVALID_SOCKET};
|
||||
#endif
|
||||
#else
|
||||
typedef int fd_t;
|
||||
enum {retired_fd = -1};
|
||||
#endif
|
||||
|
||||
void test_null_poller_pointers (void *ctx)
|
||||
{
|
||||
int rc = zmq_poller_destroy (NULL);
|
||||
@ -55,11 +69,7 @@ void test_null_poller_pointers (void *ctx)
|
||||
rc = zmq_poller_remove (&null_poller, socket);
|
||||
assert (rc == -1 && errno == EFAULT);
|
||||
|
||||
#ifdef _WIN32
|
||||
SOCKET fd;
|
||||
#else
|
||||
int fd;
|
||||
#endif
|
||||
fd_t fd;
|
||||
size_t fd_size = sizeof fd;
|
||||
rc = zmq_getsockopt(socket, ZMQ_FD, &fd, &fd_size);
|
||||
assert (rc == 0);
|
||||
@ -97,6 +107,17 @@ void test_null_socket_pointers ()
|
||||
rc = zmq_poller_remove (poller, NULL);
|
||||
assert (rc == -1 && errno == ENOTSOCK);
|
||||
|
||||
fd_t null_socket_fd = retired_fd;
|
||||
|
||||
rc = zmq_poller_add_fd (poller, null_socket_fd, NULL, ZMQ_POLLIN);
|
||||
assert (rc == -1 && errno == ENOTSOCK);
|
||||
|
||||
rc = zmq_poller_modify_fd (poller, null_socket_fd, ZMQ_POLLIN);
|
||||
assert (rc == -1 && errno == ENOTSOCK);
|
||||
|
||||
rc = zmq_poller_remove_fd (poller, null_socket_fd);
|
||||
assert (rc == -1 && errno == ENOTSOCK);
|
||||
|
||||
rc = zmq_poller_destroy (&poller);
|
||||
assert (rc == 0);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user