diff options
author | Sadie Powell <sadie@witchery.services> | 2024-03-11 13:53:05 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2024-03-11 19:17:29 +0000 |
commit | 29e7674e56bf2b829bba22def2760d034a76e788 (patch) | |
tree | f40049ba995b03dd7c510d88f9f19db2d2e65a2e /src | |
parent | e2df7d4d01f8fdb41c49ce8efc462cab005e7d5c (diff) |
Replace convertTo/stringify with non-throwing alternatives.
Having these throw is terrible for ergonomics and there are loads
of places where the exception was either silently ignored or not
handled at all. Having a function which returns an optional and
another that returns a default works a lot better imo.
Diffstat (limited to 'src')
-rw-r--r-- | src/config.cpp | 16 | ||||
-rw-r--r-- | src/init.cpp | 6 | ||||
-rw-r--r-- | src/misc.cpp | 80 | ||||
-rw-r--r-- | src/modes.cpp | 27 | ||||
-rw-r--r-- | src/modulemanager.cpp | 2 | ||||
-rw-r--r-- | src/nickcore.cpp | 2 | ||||
-rw-r--r-- | src/protocol.cpp | 2 | ||||
-rw-r--r-- | src/regchannel.cpp | 11 | ||||
-rw-r--r-- | src/socketengines/epoll.cpp | 2 | ||||
-rw-r--r-- | src/socketengines/poll.cpp | 4 | ||||
-rw-r--r-- | src/sockets.cpp | 8 |
11 files changed, 69 insertions, 91 deletions
diff --git a/src/config.cpp b/src/config.cpp index ca36bea7d..6a8380464 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -824,12 +824,12 @@ void Conf::LoadConf(File &file) if (block_stack.empty() || itemname.empty()) { file.Close(); - throw ConfigException("Unexpected quoted string: " + file.GetName() + ":" + stringify(linenumber)); + throw ConfigException("Unexpected quoted string: " + file.GetName() + ":" + Anope::ToString(linenumber)); } if (in_word || !wordbuffer.empty()) { file.Close(); - throw ConfigException("Unexpected quoted string (prior unhandled words): " + file.GetName() + ":" + stringify(linenumber)); + throw ConfigException("Unexpected quoted string (prior unhandled words): " + file.GetName() + ":" + Anope::ToString(linenumber)); } in_quote = in_word = true; } @@ -838,13 +838,13 @@ void Conf::LoadConf(File &file) if (block_stack.empty()) { file.Close(); - throw ConfigException("Config item outside of section (or stray '='): " + file.GetName() + ":" + stringify(linenumber)); + throw ConfigException("Config item outside of section (or stray '='): " + file.GetName() + ":" + Anope::ToString(linenumber)); } if (!itemname.empty() || wordbuffer.empty()) { file.Close(); - throw ConfigException("Stray '=' sign or item without value: " + file.GetName() + ":" + stringify(linenumber)); + throw ConfigException("Stray '=' sign or item without value: " + file.GetName() + ":" + Anope::ToString(linenumber)); } in_word = false; @@ -891,7 +891,7 @@ void Conf::LoadConf(File &file) if (!in_word && !wordbuffer.empty()) { file.Close(); - throw ConfigException("Unexpected word: " + file.GetName() + ":" + stringify(linenumber)); + throw ConfigException("Unexpected word: " + file.GetName() + ":" + Anope::ToString(linenumber)); } wordbuffer += ch; in_word = true; @@ -918,7 +918,7 @@ void Conf::LoadConf(File &file) if (block_stack.empty()) { file.Close(); - throw ConfigException("Stray ';' outside of block: " + file.GetName() + ":" + stringify(linenumber)); + throw ConfigException("Stray ';' outside of block: " + file.GetName() + ":" + Anope::ToString(linenumber)); } Block *b = block_stack.top(); @@ -949,7 +949,7 @@ void Conf::LoadConf(File &file) if (block_stack.empty()) { file.Close(); - throw ConfigException("Stray '}': " + file.GetName() + ":" + stringify(linenumber)); + throw ConfigException("Stray '}': " + file.GetName() + ":" + Anope::ToString(linenumber)); } block_stack.pop(); @@ -969,7 +969,7 @@ void Conf::LoadConf(File &file) if (!block_stack.empty()) { if (block_stack.top()) - throw ConfigException("Unterminated block at end of file: " + file.GetName() + ". Block was opened on line " + stringify(block_stack.top()->linenum)); + throw ConfigException("Unterminated block at end of file: " + file.GetName() + ". Block was opened on line " + Anope::ToString(block_stack.top()->linenum)); else throw ConfigException("Unterminated commented block at end of file: " + file.GetName()); } diff --git a/src/init.cpp b/src/init.cpp index 969263191..c6cb62428 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -148,10 +148,10 @@ void Anope::HandleSignal() case SIGINT: #ifndef _WIN32 Log() << "Received " << strsignal(Signal) << " signal (" << Signal << "), exiting."; - Anope::QuitReason = Anope::string("Services terminating via signal ") + strsignal(Signal) + " (" + stringify(Signal) + ")"; + Anope::QuitReason = Anope::string("Services terminating via signal ") + strsignal(Signal) + " (" + Anope::ToString(Signal) + ")"; #else Log() << "Received signal " << Signal << ", exiting."; - Anope::QuitReason = Anope::string("Services terminating via signal ") + stringify(Signal); + Anope::QuitReason = Anope::string("Services terminating via signal ") + Anope::ToString(Signal); #endif Anope::Quitting = true; Anope::SaveDatabases(); @@ -355,7 +355,7 @@ bool Anope::Init(int ac, char **av) { if (!arg.empty()) { - int level = arg.is_number_only() ? convertTo<int>(arg) : -1; + auto level = Anope::Convert<int>(arg, -1); if (level > 0) Anope::Debug = level; else diff --git a/src/misc.cpp b/src/misc.cpp index f7ada8f60..a64de450e 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -45,13 +45,12 @@ NumberList::NumberList(const Anope::string &list, bool descending) : desc(descen if (t == Anope::string::npos) { - try + if (auto num = Anope::TryConvert<unsigned>(token, &error)) { - unsigned num = convertTo<unsigned>(token, error, false); if (error.empty()) - numbers.insert(num); + numbers.insert(num.value()); } - catch (const ConvertException &) + else { error = "1"; } @@ -68,15 +67,17 @@ NumberList::NumberList(const Anope::string &list, bool descending) : desc(descen else { Anope::string error2; - try + auto n1 = Anope::TryConvert<unsigned>(token.substr(0, t), &error); + auto n2 = Anope::TryConvert<unsigned>(token.substr(t + 1), &error); + if (n1.has_value() && n2.has_value()) { - unsigned num1 = convertTo<unsigned>(token.substr(0, t), error, false); - unsigned num2 = convertTo<unsigned>(token.substr(t + 1), error2, false); + auto num1 = n1.value(); + auto num2 = n2.value(); if (error.empty() && error2.empty()) for (unsigned i = num1; i <= num2; ++i) numbers.insert(i); } - catch (const ConvertException &) + else { error = "1"; } @@ -271,37 +272,28 @@ time_t Anope::DoTime(const Anope::string &s) if (s.empty()) return 0; - int amount = 0; Anope::string end; - - try + auto amount = Anope::Convert<int>(s, -1, &end); + if (!end.empty()) { - amount = convertTo<int>(s, end, false); - if (!end.empty()) + switch (end[0]) { - switch (end[0]) - { - case 's': - return amount; - case 'm': - return amount * 60; - case 'h': - return amount * 3600; - case 'd': - return amount * 86400; - case 'w': - return amount * 86400 * 7; - case 'y': - return amount * 86400 * 365; - default: - break; - } + case 's': + return amount; + case 'm': + return amount * 60; + case 'h': + return amount * 3600; + case 'd': + return amount * 86400; + case 'w': + return amount * 86400 * 7; + case 'y': + return amount * 86400 * 365; + default: + break; } } - catch (const ConvertException &) - { - amount = -1; - } return amount; } @@ -316,32 +308,32 @@ Anope::string Anope::Duration(time_t t, const NickCore *nc) time_t seconds = (t) % 60; if (!years && !days && !hours && !minutes) - return stringify(seconds) + " " + (seconds != 1 ? Language::Translate(nc, _("seconds")) : Language::Translate(nc, _("second"))); + return Anope::ToString(seconds) + " " + (seconds != 1 ? Language::Translate(nc, _("seconds")) : Language::Translate(nc, _("second"))); else { bool need_comma = false; Anope::string buffer; if (years) { - buffer = stringify(years) + " " + (years != 1 ? Language::Translate(nc, _("years")) : Language::Translate(nc, _("year"))); + buffer = Anope::ToString(years) + " " + (years != 1 ? Language::Translate(nc, _("years")) : Language::Translate(nc, _("year"))); need_comma = true; } if (days) { buffer += need_comma ? ", " : ""; - buffer += stringify(days) + " " + (days != 1 ? Language::Translate(nc, _("days")) : Language::Translate(nc, _("day"))); + buffer += Anope::ToString(days) + " " + (days != 1 ? Language::Translate(nc, _("days")) : Language::Translate(nc, _("day"))); need_comma = true; } if (hours) { buffer += need_comma ? ", " : ""; - buffer += stringify(hours) + " " + (hours != 1 ? Language::Translate(nc, _("hours")) : Language::Translate(nc, _("hour"))); + buffer += Anope::ToString(hours) + " " + (hours != 1 ? Language::Translate(nc, _("hours")) : Language::Translate(nc, _("hour"))); need_comma = true; } if (minutes) { buffer += need_comma ? ", " : ""; - buffer += stringify(minutes) + " " + (minutes != 1 ? Language::Translate(nc, _("minutes")) : Language::Translate(nc, _("minute"))); + buffer += Anope::ToString(minutes) + " " + (minutes != 1 ? Language::Translate(nc, _("minutes")) : Language::Translate(nc, _("minute"))); } return buffer; } @@ -596,23 +588,23 @@ Anope::string Anope::LastError() Anope::string Anope::Version() { #ifdef VERSION_GIT - return stringify(VERSION_MAJOR) + "." + stringify(VERSION_MINOR) + "." + stringify(VERSION_PATCH) + VERSION_EXTRA + " (" + VERSION_GIT + ")"; + return Anope::ToString(VERSION_MAJOR) + "." + Anope::ToString(VERSION_MINOR) + "." + Anope::ToString(VERSION_PATCH) + VERSION_EXTRA + " (" + VERSION_GIT + ")"; #else - return stringify(VERSION_MAJOR) + "." + stringify(VERSION_MINOR) + "." + stringify(VERSION_PATCH) + VERSION_EXTRA; + return Anope::ToString(VERSION_MAJOR) + "." + Anope::ToString(VERSION_MINOR) + "." + Anope::ToString(VERSION_PATCH) + VERSION_EXTRA; #endif } Anope::string Anope::VersionShort() { - return stringify(VERSION_MAJOR) + "." + stringify(VERSION_MINOR) + "." + stringify(VERSION_PATCH); + return Anope::ToString(VERSION_MAJOR) + "." + Anope::ToString(VERSION_MINOR) + "." + Anope::ToString(VERSION_PATCH); } Anope::string Anope::VersionBuildString() { #ifdef REPRODUCIBLE_BUILD - Anope::string s = "build #" + stringify(BUILD); + Anope::string s = "build #" + Anope::ToString(BUILD); #else - Anope::string s = "build #" + stringify(BUILD) + ", compiled " + Anope::compiled; + Anope::string s = "build #" + Anope::ToString(BUILD) + ", compiled " + Anope::compiled; #endif Anope::string flags; diff --git a/src/modes.cpp b/src/modes.cpp index 9d344e153..8b2534268 100644 --- a/src/modes.cpp +++ b/src/modes.cpp @@ -398,7 +398,7 @@ bool ModeManager::AddUserMode(UserMode *um) if (um->name.empty()) { - um->name = stringify(++GenericUserModes); + um->name = Anope::ToString(++GenericUserModes); Log() << "ModeManager: Added generic support for user mode " << um->mchar; } @@ -425,7 +425,7 @@ bool ModeManager::AddChannelMode(ChannelMode *cm) if (cm->name.empty()) { - cm->name = stringify(++GenericChannelModes); + cm->name = Anope::ToString(++GenericChannelModes); Log() << "ModeManager: Added generic support for channel mode " << cm->mchar; } @@ -783,22 +783,15 @@ Entry::Entry(const Anope::string &m, const Anope::string &fh) : name(m), mask(fh &cidr_range = this->host.substr(sl + 1); sockaddrs addr(cidr_ip); - - try + auto range = Anope::TryConvert<unsigned short>(cidr_range); + if (addr.valid() && range.has_value()) { - if (addr.valid() && cidr_range.is_pos_number_only()) - { - this->cidr_len = convertTo<unsigned short>(cidr_range); - - /* If we got here, cidr_len is a valid number. */ - - this->host = cidr_ip; - this->family = addr.family(); + this->cidr_len = range.value(); + this->host = cidr_ip; + this->family = addr.family(); - Log(LOG_DEBUG) << "Ban " << mask << " has cidr " << this->cidr_len; - } + Log(LOG_DEBUG) << "Ban " << mask << " has cidr " << this->cidr_len; } - catch (const ConvertException &) { } } } @@ -822,11 +815,11 @@ Anope::string Entry::GetNUHMask() const { case AF_INET: if (cidr_len <= 32) - c = "/" + stringify(cidr_len); + c = "/" + Anope::ToString(cidr_len); break; case AF_INET6: if (cidr_len <= 128) - c = "/" + stringify(cidr_len); + c = "/" + Anope::ToString(cidr_len); break; } diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp index 2da87c96f..c350f8efd 100644 --- a/src/modulemanager.cpp +++ b/src/modulemanager.cpp @@ -348,7 +348,7 @@ void ModuleManager::RequireVersion(int major, int minor, int patch) } } - throw ModuleException("This module requires version " + stringify(major) + "." + stringify(minor) + "." + stringify(patch) + " - this is " + Anope::VersionShort()); + throw ModuleException("This module requires version " + Anope::ToString(major) + "." + Anope::ToString(minor) + "." + Anope::ToString(patch) + " - this is " + Anope::VersionShort()); } ModuleReturn ModuleManager::DeleteModule(Module *m) diff --git a/src/nickcore.cpp b/src/nickcore.cpp index ae9e15334..5629b00fd 100644 --- a/src/nickcore.cpp +++ b/src/nickcore.cpp @@ -213,7 +213,7 @@ uint64_t NickCore::GetId() return 0; } - Anope::string secretid = this->display + "\0" + stringify(na->time_registered); + Anope::string secretid = this->display + "\0" + Anope::ToString(na->time_registered); // Generate the account id. This should almost always only have one // iteration but in the rare case that we generate a duplicate id we try diff --git a/src/protocol.cpp b/src/protocol.cpp index 6fb7d3398..4893173af 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -157,7 +157,7 @@ void IRCDProto::SendCTCPInternal(const MessageSource &source, const Anope::strin void IRCDProto::SendNumericInternal(int numeric, const Anope::string &dest, const std::vector<Anope::string> ¶ms) { - Anope::string n = stringify(numeric); + Anope::string n = Anope::ToString(numeric); if (numeric < 10) n = "0" + n; if (numeric < 100) diff --git a/src/regchannel.cpp b/src/regchannel.cpp index 404f55aae..6aaf02901 100644 --- a/src/regchannel.cpp +++ b/src/regchannel.cpp @@ -195,7 +195,7 @@ void ChannelInfo::Serialize(Serialize::Data &data) const { Anope::string levels_buffer; for (const auto &[name, level] : this->levels) - levels_buffer += name + " " + stringify(level) + " "; + levels_buffer += name + " " + Anope::ToString(level) + " "; data["levels"] << levels_buffer; } if (this->bi) @@ -238,11 +238,10 @@ Serializable *ChannelInfo::Unserialize(Serializable *obj, Serialize::Data &data) std::vector<Anope::string> v; spacesepstream(slevels).GetTokens(v); for (unsigned i = 0; i + 1 < v.size(); i += 2) - try - { - ci->levels[v[i]] = convertTo<int16_t>(v[i + 1]); - } - catch (const ConvertException &) { } + { + if (auto level = Anope::TryConvert<int16_t>(v[i + 1])) + ci->levels[v[i]] = level.value(); + } } BotInfo *bi = BotInfo::Find(sbi, true); if (*ci->bi != bi) diff --git a/src/socketengines/epoll.cpp b/src/socketengines/epoll.cpp index e67745096..eb70de630 100644 --- a/src/socketengines/epoll.cpp +++ b/src/socketengines/epoll.cpp @@ -67,7 +67,7 @@ void SocketEngine::Change(Socket *s, bool set, SocketFlag flag) return; if (epoll_ctl(EngineHandle, mod, ev.data.fd, &ev) == -1) - throw SocketException("Unable to epoll_ctl() fd " + stringify(ev.data.fd) + " to epoll: " + Anope::LastError()); + throw SocketException("Unable to epoll_ctl() fd " + Anope::ToString(ev.data.fd) + " to epoll: " + Anope::LastError()); } void SocketEngine::Process() diff --git a/src/socketengines/poll.cpp b/src/socketengines/poll.cpp index e9a81933b..feeac5967 100644 --- a/src/socketengines/poll.cpp +++ b/src/socketengines/poll.cpp @@ -71,7 +71,7 @@ void SocketEngine::Change(Socket *s, bool set, SocketFlag flag) { std::map<int, unsigned>::iterator pos = socket_positions.find(s->GetFD()); if (pos == socket_positions.end()) - throw SocketException("Unable to remove fd " + stringify(s->GetFD()) + " from poll, it does not exist?"); + throw SocketException("Unable to remove fd " + Anope::ToString(s->GetFD()) + " from poll, it does not exist?"); if (pos->second != events.size() - 1) { @@ -90,7 +90,7 @@ void SocketEngine::Change(Socket *s, bool set, SocketFlag flag) { std::map<int, unsigned>::iterator pos = socket_positions.find(s->GetFD()); if (pos == socket_positions.end()) - throw SocketException("Unable to modify fd " + stringify(s->GetFD()) + " in poll, it does not exist?"); + throw SocketException("Unable to modify fd " + Anope::ToString(s->GetFD()) + " in poll, it does not exist?"); pollfd &ev = events[pos->second]; ev.events = (s->flags[SF_READABLE] ? POLLIN : 0) | (s->flags[SF_WRITABLE] ? POLLOUT : 0); diff --git a/src/sockets.cpp b/src/sockets.cpp index fc2940a59..6d6718547 100644 --- a/src/sockets.cpp +++ b/src/sockets.cpp @@ -282,13 +282,7 @@ cidr::cidr(const Anope::string &ip) Anope::string cidr_range = ip.substr(sl + 1); this->cidr_ip = real_ip; - this->cidr_len = ipv6 ? 128 : 32; - try - { - if (cidr_range.is_pos_number_only()) - this->cidr_len = convertTo<unsigned int>(cidr_range); - } - catch (const ConvertException &) { } + this->cidr_len = Anope::Convert<unsigned int>(cidr_range, ipv6 ? 128 : 32); this->addr.pton(ipv6 ? AF_INET6 : AF_INET, real_ip); } } |