From aa534a0936c44694b9dfb153c7d0874ec4e15615 Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Tue, 26 Jun 2012 21:55:45 +0200 Subject: [PATCH] Fixed crash in SocketSelector::add when passing an invalid socket --- include/SFML/Network/SocketSelector.hpp | 3 ++- src/SFML/Network/SocketSelector.cpp | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/SFML/Network/SocketSelector.hpp b/include/SFML/Network/SocketSelector.hpp index a6713cbd2..5dd03a53f 100644 --- a/include/SFML/Network/SocketSelector.hpp +++ b/include/SFML/Network/SocketSelector.hpp @@ -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. /// diff --git a/src/SFML/Network/SocketSelector.cpp b/src/SFML/Network/SocketSelector.cpp index ace5d1a88..3a23c1027 100644 --- a/src/SFML/Network/SocketSelector.cpp +++ b/src/SFML/Network/SocketSelector.cpp @@ -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(socket.getHandle()); - if (size > m_impl->MaxSocket) - m_impl->MaxSocket = size; + int size = static_cast(handle); + if (size > m_impl->MaxSocket) + m_impl->MaxSocket = size; + } }