Opened 6 years ago

Closed 5 years ago

#8752 closed Patches (fixed)

Bad check for invalid socket in boost::asio::detail::win_iocp_socket_service<Protocol>::accept()

Reported by: Kevin.Vlack@… Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.53.0 Severity: Problem
Keywords: Cc: antoshkka@…


(See line 434 in boost/asio/detail/win_iocp_socket_service.hpp).

At the end of the function boost::asio::detail::win_iocp_socket_service<Protocol>::accept() there is a check for an valid SOCKET handle by checking if the result is >= 0.

In recent Winsock implementations the SOCKET is typedef'd as an unsigned type (UINT_PTR), so this check for >= 0 will succeed even for invalid sockets.

If the preceding accept() operation fails and returns an invalid socket, the consequence of using it in subsequent operations is an ugly anonymous exception.

Change History (4)

comment:1 Changed 5 years ago by Antony Polukhin

Cc: antoshkka@… added
Type: BugsPatches

To fix that error, just replace

    if (new_socket.get() >= 0)


    if (new_socket.get() == SOCKET_ERROR)

comment:2 Changed 5 years ago by alb

Or better platform independent constant boost::asio::detail::socket_error_retval. But SOCKET_ERROR is OK too since win_iocp_socket_service.cpp is windows-only.

comment:3 in reply to:  1 Changed 5 years ago by Kevin.Vlack@…

Replying to apolukhin:

To fix that error, just replace

    if (new_socket.get() >= 0)


    if (new_socket.get() == SOCKET_ERROR)

Since that line checks if the socket is valid*, I believe that should be:

if (new_socket.get() != SOCKET_ERROR)

comment:4 Changed 5 years ago by chris_kohlhoff

Resolution: fixed
Status: newclosed

Fixed on trunk in [85745] and [85755]. Merged to release in [85838].

Note: See TracTickets for help on using tickets.