diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2021-05-25 18:49:16 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2021-05-26 08:08:12 +0200 |
commit | 581f12c0bdfb718379c16bef01d71ee9e45a5b5a (patch) | |
tree | 92e364e59ab581364d1d1dcb6c4c3d32cd0ea665 /sal | |
parent | d4f5299fd2806d8f5dcd467742effeaa0dee8863 (diff) |
Replace a use of select with poll
...to avoid undefined behavior with file descriptors >= FD_SETSIZE (cf.
0a126b4c661d65860fd2de92f8cc49bdb65a957c "Deprecate osl_demultiplexSocketEvents
et al")
Change-Id: I168072b50efc5d50ee3767d0b56be4a8ffa12924
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116119
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'sal')
-rw-r--r-- | sal/osl/unx/socket.cxx | 41 |
1 files changed, 15 insertions, 26 deletions
diff --git a/sal/osl/unx/socket.cxx b/sal/osl/unx/socket.cxx index 2eafd39394cb..376d6f1412ce 100644 --- a/sal/osl/unx/socket.cxx +++ b/sal/osl/unx/socket.cxx @@ -264,6 +264,14 @@ static sal_Int32 osl_psz_getServicePort ( static void osl_psz_getLastSocketErrorDescription ( oslSocket Socket, char* pBuffer, sal_uInt32 BufferSize); +namespace { + +int convertToMs(TimeValue const * value) { + return value->Seconds * 1000 + value->Nanosec / 1000000; //TODO: overflow +} + +} + static oslSocket createSocketImpl() { oslSocket pSocket; @@ -1328,10 +1336,7 @@ oslSocketResult SAL_CALL osl_connectSocketTo(oslSocket pSocket, oslSocketAddr pAddr, const TimeValue* pTimeout) { - fd_set WriteSet; - fd_set ExcptSet; int ReadyHandles; - struct timeval tv; SAL_WARN_IF( !pSocket, "sal.osl", "undefined socket" ); @@ -1387,30 +1392,16 @@ oslSocketResult SAL_CALL osl_connectSocketTo(oslSocket pSocket, return osl_Socket_Error; } - /* prepare select set for socket */ - FD_ZERO(&WriteSet); - FD_ZERO(&ExcptSet); - FD_SET(pSocket->m_Socket, &WriteSet); - FD_SET(pSocket->m_Socket, &ExcptSet); - - /* prepare timeout */ - if (pTimeout) - { - /* divide milliseconds into seconds and microseconds */ - tv.tv_sec= pTimeout->Seconds; - tv.tv_usec= pTimeout->Nanosec / 1000; - } + /* prepare poll set for socket */ + pollfd Set = {pSocket->m_Socket, POLLPRI | POLLOUT, 0}; - /* select */ - ReadyHandles= select(pSocket->m_Socket+1, - nullptr, - PTR_FD_SET(WriteSet), - PTR_FD_SET(ExcptSet), - pTimeout ? &tv : nullptr); + /* poll */ + ReadyHandles= poll(&Set, 1, + pTimeout ? convertToMs(pTimeout) : -1); if (ReadyHandles > 0) /* connected */ { - if ( FD_ISSET(pSocket->m_Socket, &WriteSet ) ) + if ( (Set.revents & POLLOUT) != 0 ) { int nErrorCode = 0; socklen_t nErrorSize = sizeof( nErrorCode ); @@ -1789,9 +1780,7 @@ static bool socket_poll ( timeout = -1; if (pTimeout) { - /* Convert to [ms] */ - timeout = pTimeout->Seconds * 1000; - timeout += pTimeout->Nanosec / (1000 * 1000); + timeout = convertToMs(pTimeout); } result = poll (&fds, 1, timeout); |