Skip to content

Commit

Permalink
Clamp client-sent movement speed control (#15721)
Browse files Browse the repository at this point in the history
Results in the `movement_x` and `movement_y` fields of `player:get_player_control()` being safe to use
(otherwise users would need to compute the length as `(x^2 + y^2)^0.5` and clamp that to 1 themselves).
  • Loading branch information
appgurueu authored Feb 4, 2025
1 parent b2a6c3b commit a73e715
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/network/serverpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "util/srp.h"
#include "clientdynamicinfo.h"

#include <algorithm>

void Server::handleCommand_Deprecated(NetworkPacket* pkt)
{
infostream << "Server: " << toServerCommandTable[pkt->getCommand()].name
Expand Down Expand Up @@ -468,7 +470,11 @@ void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
*pkt >> bits;

if (pkt->getRemainingBytes() >= 8) {
*pkt >> player->control.movement_speed;
f32 movement_speed;
*pkt >> movement_speed;
if (movement_speed != movement_speed) // NaN
movement_speed = 0.0f;
player->control.movement_speed = std::clamp(movement_speed, 0.0f, 1.0f);
*pkt >> player->control.movement_direction;
} else {
player->control.movement_speed = 0.0f;
Expand Down

0 comments on commit a73e715

Please sign in to comment.