fix exception propagation on packet decompression

This commit is contained in:
Lion Kortlepel 2024-09-19 16:58:04 +02:00
parent 530d605bc1
commit 73f494041a
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
4 changed files with 35 additions and 3 deletions

View File

@ -268,3 +268,9 @@ std::vector<uint8_t> DeComp(std::span<const uint8_t> input);
std::string GetPlatformAgnosticErrorString();
#define S_DSN SU_RAW
class InvalidDataError : std::runtime_error {
public:
InvalidDataError() : std::runtime_error("Invalid data") {
}
};

View File

@ -418,7 +418,11 @@ std::vector<uint8_t> DeComp(std::span<const uint8_t> input) {
output_size = output_buffer.size();
} else if (res != Z_OK) {
beammp_error("zlib uncompress() failed: " + std::to_string(res));
throw std::runtime_error("zlib uncompress() failed");
if (res == Z_DATA_ERROR) {
throw InvalidDataError {};
} else {
throw std::runtime_error("zlib uncompress() failed");
}
} else if (res == Z_OK) {
break;
}

View File

@ -546,7 +546,17 @@ std::vector<uint8_t> TNetwork::TCPRcv(TClient& c) {
constexpr std::string_view ABG = "ABG:";
if (Data.size() >= ABG.size() && std::equal(Data.begin(), Data.begin() + ABG.size(), ABG.begin(), ABG.end())) {
Data.erase(Data.begin(), Data.begin() + ABG.size());
return DeComp(Data);
try {
return DeComp(Data);
} catch (const InvalidDataError& ) {
beammp_errorf("Failed to decompress packet from a client. The receive failed and the client may be disconnected as a result");
// return empty -> error
return std::vector<uint8_t>();
} catch (const std::runtime_error& e) {
beammp_errorf("Failed to decompress packet from a client: {}. The server may be out of RAM! The receive failed and the client may be disconnected as a result", e.what());
// return empty -> error
return std::vector<uint8_t>();
}
} else {
return Data;
}

View File

@ -165,7 +165,19 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
constexpr std::string_view ABG = "ABG:";
if (Packet.size() >= ABG.size() && std::equal(Packet.begin(), Packet.begin() + ABG.size(), ABG.begin(), ABG.end())) {
Packet.erase(Packet.begin(), Packet.begin() + ABG.size());
Packet = DeComp(Packet);
try {
Packet = DeComp(Packet);
} catch (const InvalidDataError& ) {
auto LockedClient = Client.lock();
beammp_errorf("Failed to decompress packet from client {}. The client sent invalid data and will now be disconnected.", LockedClient->GetID());
Network.ClientKick(*LockedClient, "Sent invalid compressed packet (this is likely a bug on your end)");
return;
} catch (const std::runtime_error& e) {
auto LockedClient = Client.lock();
beammp_errorf("Failed to decompress packet from client {}: {}. The server might be out of RAM! The client will now be disconnected.", LockedClient->GetID(), e.what());
Network.ClientKick(*LockedClient, "Decompression failed (likely a server-side problem)");
return;
}
}
if (Packet.empty()) {
return;