diff --git a/src/TsClient.cpp b/src/TsClient.cpp index 4334ccd..51a592d 100644 --- a/src/TsClient.cpp +++ b/src/TsClient.cpp @@ -48,6 +48,17 @@ namespace Void::TeamSpeak::Protocol std::chrono::steady_clock::now().time_since_epoch()) .count(); } + std::optional 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& b, size_t off, uint16_t v) { @@ -949,22 +960,23 @@ namespace Void::TeamSpeak::Protocol handleChannelListFinished(); } else if (name == "notifyclientleftview") { auto it = parsed.params.find("clid"); - if (it != parsed.params.end() && - static_cast(std::stoul(it->second)) == - clientId_.load()) { - cleanup(); - } + if (it != parsed.params.end()) { + if (auto clid = safeParseU64(it->second); + clid && static_cast(*clid) == clientId_.load()) { + cleanup(); + } + } } else if (name == "notifytextmessage") { if (cb_.onTextMessage) { cb_.onTextMessage(parsed.params); } } else if (name == "error") { int errId = 0; - auto it = parsed.params.find("id"); - if (it != parsed.params.end()) { - errId = std::stoi(it->second); - } - if (errId == 2568 || errId == 3329 || errId == 1796) { + auto it = parsed.params.find("id"); + if (it != parsed.params.end()) { + if (auto v = safeParseU64(it->second)) errId = static_cast(*v); + } + if (errId == 2568 || errId == 3329 || errId == 1796) { std::string msg = "unknown error"; auto m = parsed.params.find("msg"); if (m != parsed.params.end()) { @@ -1104,11 +1116,13 @@ namespace Void::TeamSpeak::Protocol void TsClient::handleInitServer(const CommandParams& params) { auto it = params.find("aclid"); - uint16_t id = 0; - if (it != params.end()) { - id = static_cast(std::stoul(it->second)); - } - clientId_ = id; + uint16_t id = 0; + if (it != params.end()) { + if (auto v = safeParseU64(it->second)) { + id = static_cast(*v); + } + } + clientId_ = id; state_ = ClientState::Connected; lastPing_ = nowMs(); debug("connected, clientId=" + std::to_string(id)); @@ -1127,9 +1141,10 @@ namespace Void::TeamSpeak::Protocol auto ci = e.find("cid"); auto ni = e.find("channel_name"); if (ci != e.end() && ni != e.end()) { - channelMap_[ni->second] = - static_cast(std::stoull(ci->second)); - } + if (auto v = safeParseU64(ci->second)) { + channelMap_[ni->second] = *v; + } + } } }