Skip to content

Commit 7f69e33

Browse files
authored
Fix exception propagation on packet decompression (#365)
2 parents f08dfc0 + 5319c28 commit 7f69e33

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

include/Common.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Application final {
127127
static inline std::mutex mShutdownHandlersMutex {};
128128
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
129129

130-
static inline Version mVersion { 3, 5, 0 };
130+
static inline Version mVersion { 3, 5, 1 };
131131
};
132132

133133
void SplitString(std::string const& str, const char delim, std::vector<std::string>& out);
@@ -268,3 +268,9 @@ std::vector<uint8_t> DeComp(std::span<const uint8_t> input);
268268

269269
std::string GetPlatformAgnosticErrorString();
270270
#define S_DSN SU_RAW
271+
272+
class InvalidDataError : std::runtime_error {
273+
public:
274+
InvalidDataError() : std::runtime_error("Invalid data") {
275+
}
276+
};

src/Common.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,11 @@ std::vector<uint8_t> DeComp(std::span<const uint8_t> input) {
418418
output_size = output_buffer.size();
419419
} else if (res != Z_OK) {
420420
beammp_error("zlib uncompress() failed: " + std::to_string(res));
421-
throw std::runtime_error("zlib uncompress() failed");
421+
if (res == Z_DATA_ERROR) {
422+
throw InvalidDataError {};
423+
} else {
424+
throw std::runtime_error("zlib uncompress() failed");
425+
}
422426
} else if (res == Z_OK) {
423427
break;
424428
}

src/TNetwork.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,17 @@ std::vector<uint8_t> TNetwork::TCPRcv(TClient& c) {
546546
constexpr std::string_view ABG = "ABG:";
547547
if (Data.size() >= ABG.size() && std::equal(Data.begin(), Data.begin() + ABG.size(), ABG.begin(), ABG.end())) {
548548
Data.erase(Data.begin(), Data.begin() + ABG.size());
549-
return DeComp(Data);
549+
try {
550+
return DeComp(Data);
551+
} catch (const InvalidDataError& ) {
552+
beammp_errorf("Failed to decompress packet from a client. The receive failed and the client may be disconnected as a result");
553+
// return empty -> error
554+
return std::vector<uint8_t>();
555+
} catch (const std::runtime_error& e) {
556+
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());
557+
// return empty -> error
558+
return std::vector<uint8_t>();
559+
}
550560
} else {
551561
return Data;
552562
}

src/TServer.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,19 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
165165
constexpr std::string_view ABG = "ABG:";
166166
if (Packet.size() >= ABG.size() && std::equal(Packet.begin(), Packet.begin() + ABG.size(), ABG.begin(), ABG.end())) {
167167
Packet.erase(Packet.begin(), Packet.begin() + ABG.size());
168-
Packet = DeComp(Packet);
168+
try {
169+
Packet = DeComp(Packet);
170+
} catch (const InvalidDataError& ) {
171+
auto LockedClient = Client.lock();
172+
beammp_errorf("Failed to decompress packet from client {}. The client sent invalid data and will now be disconnected.", LockedClient->GetID());
173+
Network.ClientKick(*LockedClient, "Sent invalid compressed packet (this is likely a bug on your end)");
174+
return;
175+
} catch (const std::runtime_error& e) {
176+
auto LockedClient = Client.lock();
177+
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());
178+
Network.ClientKick(*LockedClient, "Decompression failed (likely a server-side problem)");
179+
return;
180+
}
169181
}
170182
if (Packet.empty()) {
171183
return;

0 commit comments

Comments
 (0)