safe parse uint

This commit is contained in:
faumaray committed 2026-07-27 19:24:57 +03:00
1 parent 5c0c0c525f
commit cd4e303259
1 file changed
+33 -18
+33 -18
View File
@@ -48,6 +48,17 @@ namespace Void::TeamSpeak::Protocol
std::chrono::steady_clock::now().time_since_epoch()) std::chrono::steady_clock::now().time_since_epoch())
.count(); .count();
} }
std::optional<uint64_t> safeParseU64(const std::string& s) {
if (s.empty()) return std::nullopt;
try {
size_t pos = 0;
uint64_t v = std::stoull(s, &pos);
if (pos != s.size()) return std::nullopt;
return v;
} catch (const std::exception&) {
return std::nullopt;
}
}
void putU16BE(std::vector<uint8_t>& b, size_t off, uint16_t v) void putU16BE(std::vector<uint8_t>& b, size_t off, uint16_t v)
{ {
@@ -949,22 +960,23 @@ namespace Void::TeamSpeak::Protocol
handleChannelListFinished(); handleChannelListFinished();
} else if (name == "notifyclientleftview") { } else if (name == "notifyclientleftview") {
auto it = parsed.params.find("clid"); auto it = parsed.params.find("clid");
if (it != parsed.params.end() && if (it != parsed.params.end()) {
static_cast<uint16_t>(std::stoul(it->second)) == if (auto clid = safeParseU64(it->second);
clientId_.load()) { clid && static_cast<uint16_t>(*clid) == clientId_.load()) {
cleanup(); cleanup();
} }
}
} else if (name == "notifytextmessage") { } else if (name == "notifytextmessage") {
if (cb_.onTextMessage) { if (cb_.onTextMessage) {
cb_.onTextMessage(parsed.params); cb_.onTextMessage(parsed.params);
} }
} else if (name == "error") { } else if (name == "error") {
int errId = 0; int errId = 0;
auto it = parsed.params.find("id"); auto it = parsed.params.find("id");
if (it != parsed.params.end()) { if (it != parsed.params.end()) {
errId = std::stoi(it->second); if (auto v = safeParseU64(it->second)) errId = static_cast<int>(*v);
} }
if (errId == 2568 || errId == 3329 || errId == 1796) { if (errId == 2568 || errId == 3329 || errId == 1796) {
std::string msg = "unknown error"; std::string msg = "unknown error";
auto m = parsed.params.find("msg"); auto m = parsed.params.find("msg");
if (m != parsed.params.end()) { if (m != parsed.params.end()) {
@@ -1104,11 +1116,13 @@ namespace Void::TeamSpeak::Protocol
void TsClient::handleInitServer(const CommandParams& params) void TsClient::handleInitServer(const CommandParams& params)
{ {
auto it = params.find("aclid"); auto it = params.find("aclid");
uint16_t id = 0; uint16_t id = 0;
if (it != params.end()) { if (it != params.end()) {
id = static_cast<uint16_t>(std::stoul(it->second)); if (auto v = safeParseU64(it->second)) {
} id = static_cast<uint16_t>(*v);
clientId_ = id; }
}
clientId_ = id;
state_ = ClientState::Connected; state_ = ClientState::Connected;
lastPing_ = nowMs(); lastPing_ = nowMs();
debug("connected, clientId=" + std::to_string(id)); debug("connected, clientId=" + std::to_string(id));
@@ -1127,9 +1141,10 @@ namespace Void::TeamSpeak::Protocol
auto ci = e.find("cid"); auto ci = e.find("cid");
auto ni = e.find("channel_name"); auto ni = e.find("channel_name");
if (ci != e.end() && ni != e.end()) { if (ci != e.end() && ni != e.end()) {
channelMap_[ni->second] = if (auto v = safeParseU64(ci->second)) {
static_cast<uint64_t>(std::stoull(ci->second)); channelMap_[ni->second] = *v;
} }
}
} }
} }