1
0
mirror of https://github.com/anope/anope.git synced 2026-07-09 20:43:13 +02:00

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.
This commit is contained in:
Sadie Powell
2024-03-11 13:53:05 +00:00
parent e2df7d4d01
commit 29e7674e56
76 changed files with 572 additions and 810 deletions
+8 -8
View File
@@ -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());
}
+3 -3
View File
@@ -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
+36 -44
View File
@@ -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;
+10 -17
View File
@@ -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);
this->cidr_len = range.value();
this->host = cidr_ip;
this->family = addr.family();
/* If we got here, cidr_len is a valid number. */
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;
}
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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> &params)
{
Anope::string n = stringify(numeric);
Anope::string n = Anope::ToString(numeric);
if (numeric < 10)
n = "0" + n;
if (numeric < 100)
+5 -6
View File
@@ -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)
+1 -1
View File
@@ -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()
+2 -2
View File
@@ -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);
+1 -7
View File
@@ -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);
}
}