Opened 9 years ago

Closed 9 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@…

Description

(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 by Antony Polukhin, 9 years ago

Cc: antoshkka@… added
Type: BugsPatches

To fix that error, just replace

    if (new_socket.get() >= 0)

with

    if (new_socket.get() == SOCKET_ERROR)

comment:2 by alb, 9 years ago

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.

in reply to:  1 comment:3 by Kevin.Vlack@…, 9 years ago

Replying to apolukhin:

To fix that error, just replace

    if (new_socket.get() >= 0)

with

    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 by chris_kohlhoff, 9 years ago

Resolution: fixed
Status: newclosed

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

Note: See TracTickets for help on using tickets.