Skip to content

Commit 7ebd1fa

Browse files
committed
Add exception handling to prevent player hacking network string
1 parent 00712c5 commit 7ebd1fa

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

src/modes/cutscene_world.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ void CutsceneWorld::update(int ticks)
231231
{
232232
// this way of calculating time and dt is more in line with what
233233
// irrlicht does and provides better synchronisation
234-
double prev_time = m_time;
235234
double now = StkTime::getRealTime();
236235
m_time = now - m_time_at_second_reset;
237236
}

src/network/network_string.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ friend class Crypto;
7474
*/
7575
std::string getString(int len) const
7676
{
77+
if (m_current_offset > (int)m_buffer.size() ||
78+
m_current_offset + len > (int)m_buffer.size())
79+
throw std::out_of_range("getString out of range.");
80+
7781
std::string a(m_buffer.begin() + (m_current_offset ),
7882
m_buffer.begin() + (m_current_offset + len));
7983
m_current_offset += len;
@@ -101,7 +105,7 @@ friend class Crypto;
101105
{
102106
result <<= 8; // offset one byte
103107
// add the data to result
104-
result += m_buffer[offset - a];
108+
result += m_buffer.at(offset - a);
105109
}
106110
return result;
107111
} // get(int pos)
@@ -110,7 +114,7 @@ friend class Crypto;
110114
template<typename T>
111115
T get() const
112116
{
113-
return m_buffer[m_current_offset++];
117+
return m_buffer.at(m_current_offset++);
114118
} // get
115119

116120
public:
@@ -424,8 +428,7 @@ class NetworkString : public BareNetworkString
424428
/** Returns the protocol type of this message. */
425429
ProtocolType getProtocolType() const
426430
{
427-
assert(!m_buffer.empty());
428-
return (ProtocolType)(m_buffer[0] & ~PROTOCOL_SYNCHRONOUS);
431+
return (ProtocolType)(m_buffer.at(0) & ~PROTOCOL_SYNCHRONOUS);
429432
} // getProtocolType
430433

431434
// ------------------------------------------------------------------------

src/network/protocol_manager.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,15 @@ bool ProtocolManager::sendEvent(Event* event)
378378
bool can_be_deleted = false;
379379
if (event->getType() == EVENT_TYPE_MESSAGE)
380380
{
381-
OneProtocolType &opt = m_all_protocols[event->data().getProtocolType()];
381+
OneProtocolType &opt =
382+
m_all_protocols.at(event->data().getProtocolType());
382383
can_be_deleted = opt.notifyEvent(event);
383384
}
384385
else // connect or disconnect event --> test all protocols
385386
{
386387
for (unsigned int i = 0; i < m_all_protocols.size(); i++)
387388
{
388-
can_be_deleted |= m_all_protocols[i].notifyEvent(event);
389+
can_be_deleted |= m_all_protocols.at(i).notifyEvent(event);
389390
}
390391
}
391392
return can_be_deleted || StkTime::getTimeSinceEpoch() - event->getArrivalTime()
@@ -432,7 +433,16 @@ void ProtocolManager::update(int ticks)
432433
while (i != m_sync_events_to_process.getData().end())
433434
{
434435
m_sync_events_to_process.unlock();
435-
bool can_be_deleted = sendEvent(*i);
436+
bool can_be_deleted = true;
437+
try
438+
{
439+
can_be_deleted = sendEvent(*i);
440+
}
441+
catch (std::exception& e)
442+
{
443+
Log::error("ProtocolManager", "Synchronous event error: %s",
444+
e.what());
445+
}
436446
m_sync_events_to_process.lock();
437447
if (can_be_deleted)
438448
{
@@ -478,7 +488,16 @@ void ProtocolManager::asynchronousUpdate()
478488
m_async_events_to_process.unlock();
479489

480490
m_all_protocols[(*i)->getType()].lock();
481-
bool result = sendEvent(*i);
491+
bool result = true;
492+
try
493+
{
494+
result = sendEvent(*i);
495+
}
496+
catch (std::exception& e)
497+
{
498+
Log::error("ProtocolManager", "Asynchronous event error: %s",
499+
e.what());
500+
}
482501
m_all_protocols[(*i)->getType()].unlock();
483502

484503
m_async_events_to_process.lock();

src/network/protocols/game_events_protocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ bool GameEventsProtocol::notifyEvent(Event* event)
5454
case GE_RESET_BALL:
5555
{
5656
if (!sw)
57-
throw("No soccer world");
57+
throw std::invalid_argument("No soccer world");
5858
sw->handleResetBallFromServer(data);
5959
break;
6060
}
6161
case GE_PLAYER_GOAL:
6262
{
6363
if (!sw)
64-
throw("No soccer world");
64+
throw std::invalid_argument("No soccer world");
6565
sw->handlePlayerGoalFromServer(data);
6666
break;
6767
}

src/network/stk_host.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,15 @@ void STKHost::mainLoop()
720720
auto sl = LobbyProtocol::get<ServerLobby>();
721721
if (direct_socket && sl && sl->waitingForPlayers())
722722
{
723-
handleDirectSocketRequest(direct_socket, sl);
723+
try
724+
{
725+
handleDirectSocketRequest(direct_socket, sl);
726+
}
727+
catch (std::exception& e)
728+
{
729+
Log::warn("STKHost", "Direct socket error: %s",
730+
e.what());
731+
}
724732
} // if discovery host
725733

726734
if (is_server)

src/states_screens/race_gui_overworld.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ void RaceGUIOverworld::renderGlobal(float dt)
189189
if (race_manager->getIfEmptyScreenSpaceExists() &&
190190
!GUIEngine::ModalDialog::isADialogActive())
191191
{
192-
const float sqrt_num_players =
193-
sqrtf((float)race_manager->getNumLocalPlayers());
194-
const int rows = (int)ceil(sqrt_num_players);
195-
const int cols = (int)round(sqrt_num_players);
196192
static video::SColor black = video::SColor(255,0,0,0);
197193
GL32_draw2DRectangle(black, irr_driver->getSplitscreenWindow(
198194
race_manager->getNumLocalPlayers()));

0 commit comments

Comments
 (0)