1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Merge pull request #3628 from IvanSavenko/fix_network_crash

Fix network crash
This commit is contained in:
Ivan Savenko
2024-02-19 15:51:35 +02:00
committed by GitHub
3 changed files with 8 additions and 7 deletions

View File

@@ -143,13 +143,6 @@ LONG WINAPI onUnhandledException(EXCEPTION_POINTERS* exception)
HANDLE dfile = CreateFileA(mname, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0); HANDLE dfile = CreateFileA(mname, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
logGlobal->error("Crash info will be put in %s", mname); logGlobal->error("Crash info will be put in %s", mname);
// flush loggers
std::string padding(1000, '@');
logGlobal->error(padding);
logAi->error(padding);
logNetwork->error(padding);
auto dumpType = MiniDumpWithDataSegs; auto dumpType = MiniDumpWithDataSegs;
if(settings["general"]["extraDump"].Bool()) if(settings["general"]["extraDump"].Bool())

View File

@@ -49,6 +49,12 @@ void NetworkConnection::onHeaderReceived(const boost::system::error_code & ecHea
return; return;
} }
if (messageSize == 0)
{
listener.onDisconnected(shared_from_this(), "Zero-sized packet!");
return;
}
boost::asio::async_read(*socket, boost::asio::async_read(*socket,
readBuffer, readBuffer,
boost::asio::transfer_exactly(messageSize), boost::asio::transfer_exactly(messageSize),
@@ -82,6 +88,7 @@ void NetworkConnection::sendPacket(const std::vector<std::byte> & message)
// create array with single element - boost::asio::buffer can be constructed from containers, but not from plain integer // create array with single element - boost::asio::buffer can be constructed from containers, but not from plain integer
std::array<uint32_t, 1> messageSize{static_cast<uint32_t>(message.size())}; std::array<uint32_t, 1> messageSize{static_cast<uint32_t>(message.size())};
boost::mutex::scoped_lock lock(writeMutex);
boost::asio::write(*socket, boost::asio::buffer(messageSize), ec ); boost::asio::write(*socket, boost::asio::buffer(messageSize), ec );
boost::asio::write(*socket, boost::asio::buffer(message), ec ); boost::asio::write(*socket, boost::asio::buffer(message), ec );

View File

@@ -19,6 +19,7 @@ class NetworkConnection : public INetworkConnection, public std::enable_shared_f
static const int messageMaxSize = 64 * 1024 * 1024; // arbitrary size to prevent potential massive allocation if we receive garbage input static const int messageMaxSize = 64 * 1024 * 1024; // arbitrary size to prevent potential massive allocation if we receive garbage input
std::shared_ptr<NetworkSocket> socket; std::shared_ptr<NetworkSocket> socket;
boost::mutex writeMutex;
NetworkBuffer readBuffer; NetworkBuffer readBuffer;
INetworkConnectionListener & listener; INetworkConnectionListener & listener;