Fixed crash in SocketSelector::add when passing an invalid socket

This commit is contained in:
Laurent Gomila 2012-06-26 21:55:45 +02:00
parent 5706111088
commit aa534a0936
2 changed files with 10 additions and 5 deletions

View File

@ -70,6 +70,7 @@ public :
/// This function keeps a weak reference to the socket,
/// so you have to make sure that the socket is not destroyed
/// while it is stored in the selector.
/// This function does nothing if the socket is not valid.
///
/// \param socket Reference to the socket to add
///
@ -186,7 +187,7 @@ private :
/// A selector doesn't store its own copies of the sockets
/// (socket classes are not copyable anyway), it simply keeps
/// a reference to the original sockets that you pass to the
/// Add function. Therefore, you can't use the selector as a
/// "add" function. Therefore, you can't use the selector as a
/// socket container, you must store them oustide and make sure
/// that they are alive as long as they are used in the selector.
///

View File

@ -73,11 +73,15 @@ SocketSelector::~SocketSelector()
////////////////////////////////////////////////////////////
void SocketSelector::add(Socket& socket)
{
FD_SET(socket.getHandle(), &m_impl->AllSockets);
SocketHandle handle = socket.getHandle();
if (handle != SocketImpl::invalidSocket())
{
FD_SET(handle, &m_impl->AllSockets);
int size = static_cast<int>(socket.getHandle());
if (size > m_impl->MaxSocket)
m_impl->MaxSocket = size;
int size = static_cast<int>(handle);
if (size > m_impl->MaxSocket)
m_impl->MaxSocket = size;
}
}