Shared/Networking: Fix buffer overflow in Socket handling

Fix a buffer overflow caused by Boost trying to store too much data in a buffer too small.

(cherry picked from commit cdf6c88462)
This commit is contained in:
jackpoz
2014-12-27 00:10:01 +01:00
committed by Nayd
parent d51b2c04c8
commit 3cdc66bec6
2 changed files with 10 additions and 1 deletions

View File

@@ -81,6 +81,14 @@ public:
}
}
// Ensures there's "some" free space, make sure to call Normalize() before this
void EnsureFreeSpace()
{
// Double the size of the buffer if it's already full
if (GetRemainingSpace() == 0)
_storage.resize(_storage.size() * 2);
}
void Write(void const* data, std::size_t size)
{
if (size)

View File

@@ -92,7 +92,8 @@ public:
return;
_readBuffer.Normalize();
_socket.async_read_some(boost::asio::buffer(_readBuffer.GetWritePointer(), READ_BLOCK_SIZE),
_readBuffer.EnsureFreeSpace();
_socket.async_read_some(boost::asio::buffer(_readBuffer.GetWritePointer(), _readBuffer.GetRemainingSpace()),
std::bind(&Socket<T>::ReadHandlerInternal, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}