safe parse uint
This commit is contained in:
1 parent
5c0c0c525f
commit
cd4e303259
1 file changed
+33
-18
+33
-18
@@ -48,6 +48,17 @@ namespace Void::TeamSpeak::Protocol
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.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)
|
||||
{
|
||||
@@ -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<uint16_t>(std::stoul(it->second)) ==
|
||||
clientId_.load()) {
|
||||
cleanup();
|
||||
}
|
||||
if (it != parsed.params.end()) {
|
||||
if (auto clid = safeParseU64(it->second);
|
||||
clid && static_cast<uint16_t>(*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<int>(*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<uint16_t>(std::stoul(it->second));
|
||||
}
|
||||
clientId_ = id;
|
||||
uint16_t id = 0;
|
||||
if (it != params.end()) {
|
||||
if (auto v = safeParseU64(it->second)) {
|
||||
id = static_cast<uint16_t>(*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<uint64_t>(std::stoull(ci->second));
|
||||
}
|
||||
if (auto v = safeParseU64(ci->second)) {
|
||||
channelMap_[ni->second] = *v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user