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

try/catch-ified all instances of convertTo to keep from aborting when a user gives too large or too small a number

This commit is contained in:
Adam
2011-02-04 21:01:33 -05:00
parent faf5f3128f
commit 83556667fd
12 changed files with 236 additions and 144 deletions
+109 -63
View File
File diff suppressed because it is too large Load Diff
+21 -10
View File
@@ -169,7 +169,13 @@ class CommandCSAccess : public Command
ChannelInfo *ci = source.ci;
Anope::string mask = params[2];
int level = params[3].is_number_only() ? convertTo<int>(params[3]) : ACCESS_INVALID;
int level = ACCESS_INVALID;
try
{
level = convertTo<int>(params[3]);
}
catch (const ConvertException &) { }
ChanAccess *u_access = ci->GetAccess(u);
int16 u_level = u_access ? u_access->level : 0;
@@ -536,20 +542,25 @@ class CommandCSLevels : public Command
const Anope::string &what = params[2];
const Anope::string &lev = params[3];
Anope::string error;
int level = (lev.is_number_only() ? convertTo<int>(lev, error, false) : 0);
if (!lev.is_number_only())
error = "1";
int level;
if (lev.equals_ci("FOUNDER"))
{
level = ACCESS_FOUNDER;
error.clear();
else
{
level = 1;
try
{
level = convertTo<int>(lev);
}
catch (const ConvertException &)
{
this->OnSyntaxError(source, "SET");
return MOD_CONT;
}
}
if (!error.empty())
this->OnSyntaxError(source, "SET");
else if (level <= ACCESS_INVALID || level > ACCESS_FOUNDER)
if (level <= ACCESS_INVALID || level > ACCESS_FOUNDER)
source.Reply(_("Level must be between %d and %d inclusive."), ACCESS_INVALID + 1, ACCESS_FOUNDER - 1);
else
{
+10 -12
View File
@@ -41,24 +41,22 @@ public:
if (pattern[0] == '#')
{
Anope::string tmp = myStrGetToken(pattern.substr(1), '-', 0); /* Read FROM out */
if (tmp.empty() || !tmp.is_number_only())
Anope::string n1 = myStrGetToken(pattern.substr(1), '-', 0), /* Read FROM out */
n2 = myStrGetTokenRemainder(pattern, '-', 1);
try
{
from = convertTo<int>(n1);
to = convertTo<int>(n2);
}
catch (const ConvertException &)
{
source.Reply(LanguageString::LIST_INCORRECT_RANGE);
source.Reply(_("To search for channels starting with #, search for the channel\n"
"name without the #-sign prepended (\002anope\002 instead of \002#anope\002)."));
return MOD_CONT;
}
from = convertTo<int>(tmp);
tmp = myStrGetTokenRemainder(pattern, '-', 1); /* Read TO out */
if (tmp.empty() || !tmp.is_number_only())
{
source.Reply(LanguageString::LIST_INCORRECT_RANGE);
source.Reply(_("To search for channels starting with #, search for the channel\n"
"name without the #-sign prepended (\002anope\002 instead of \002#anope\002)."));
return MOD_CONT;
}
to = convertTo<int>(tmp);
pattern = "*";
}
+6 -8
View File
@@ -26,17 +26,15 @@ class CommandCSSetBanType : public Command
if (!ci)
throw CoreException("NULL ci in CommandCSSetBanType");
Anope::string end;
int16 bantype = convertTo<int16>(params[1], end, false);
if (!end.empty() || bantype < 0 || bantype > 3)
source.Reply(_("\002%s\002 is not a valid ban type."), params[1].c_str());
else
try
{
ci->bantype = bantype;
ci->bantype = convertTo<int16>(params[1]);
source.Reply(_("Ban type for channel %s is now #%d."), ci->name.c_str(), ci->bantype);
}
catch (const ConvertException &)
{
source.Reply(_("\002%s\002 is not a valid ban type."), params[1].c_str());
}
return MOD_CONT;
}
+6 -2
View File
@@ -45,8 +45,12 @@ class CommandHSList : public Command
source.Reply(LanguageString::LIST_INCORRECT_RANGE);
return MOD_CONT;
}
from = convertTo<int>(key.substr(1, tmp - 1));
to = convertTo<int>(key.substr(tmp + 1));
try
{
from = convertTo<int>(key.substr(1, tmp - 1));
to = convertTo<int>(key.substr(tmp + 1));
}
catch (const ConvertException &) { }
}
}
+8 -2
View File
@@ -35,7 +35,6 @@ class CommandNSAList : public Command
Anope::string nick;
NickAlias *na;
int min_level = 0;
int is_servadmin = u->Account()->IsServicesOper();
unsigned lev_param = 0;
@@ -60,6 +59,7 @@ class CommandNSAList : public Command
Anope::string lev = params.size() > lev_param ? params[lev_param] : "";
/* if a level was given, make sure it's an int for later */
int min_level = ACCESS_INVALID;
if (!lev.empty())
{
if (lev.equals_ci("FOUNDER"))
@@ -73,7 +73,13 @@ class CommandNSAList : public Command
else if (lev.equals_ci("VOP"))
min_level = ACCESS_VOP;
else
min_level = lev.is_number_only() ? convertTo<int>(lev) : ACCESS_INVALID;
{
try
{
min_level = convertTo<int>(lev);
}
catch (const ConvertException &) { }
}
}
if (!na)
+10 -20
View File
@@ -57,30 +57,20 @@ class CommandNSList : public Command
if (pattern[0] == '#')
{
Anope::string tmp = myStrGetToken(pattern.substr(1), '-', 0); /* Read FROM out */
if (tmp.empty())
Anope::string n1 = myStrGetToken(pattern.substr(1), '-', 0), /* Read FROM out */
n2 = myStrGetToken(pattern, '-', 1);
try
{
from = convertTo<int>(n1);
to = convertTo<int>(n2);
}
catch (const ConvertException &)
{
source.Reply(LanguageString::LIST_INCORRECT_RANGE);
return MOD_CONT;
}
if (!tmp.is_number_only())
{
source.Reply(LanguageString::LIST_INCORRECT_RANGE);
return MOD_CONT;
}
from = convertTo<int>(tmp);
tmp = myStrGetTokenRemainder(pattern, '-', 1); /* Read TO out */
if (tmp.empty())
{
source.Reply(LanguageString::LIST_INCORRECT_RANGE);
return MOD_CONT;
}
if (!tmp.is_number_only())
{
source.Reply(LanguageString::LIST_INCORRECT_RANGE);
return MOD_CONT;
}
to = convertTo<int>(tmp);
pattern = "*";
}
+9 -2
View File
@@ -63,7 +63,6 @@ class CommandOSDefcon : public Command
{
User *u = source.u;
const Anope::string &lvl = params[0];
int newLevel = 0;
if (lvl.empty())
{
@@ -71,12 +70,20 @@ class CommandOSDefcon : public Command
defcon_sendlvls(source);
return MOD_CONT;
}
newLevel = lvl.is_number_only() ? convertTo<int>(lvl) : 0;
int newLevel = 0;
try
{
newLevel = convertTo<int>(lvl);
}
catch (const ConvertException &) { }
if (newLevel < 1 || newLevel > 5)
{
this->OnSyntaxError(source, "");
return MOD_CONT;
}
Config->DefConLevel = newLevel;
FOREACH_MOD(I_OnDefconLevel, OnDefconLevel(newLevel));
+13 -8
View File
@@ -226,16 +226,21 @@ class NewsBase : public Command
}
if (!text.equals_ci("ALL"))
{
num = text.is_pos_number_only() ? convertTo<unsigned>(text) : 0;
if (num > 0 && del_newsitem(num, type))
try
{
source.Reply(msgs[MSG_DELETED], num);
for (unsigned i = 0, end = News.size(); i < end; ++i)
if (News[i]->type == type && News[i]->num > num)
--News[i]->num;
unsigned num = convertTo<unsigned>(text);
if (num > 0 && del_newsitem(num, type))
{
source.Reply(msgs[MSG_DELETED], num);
for (unsigned i = 0, end = News.size(); i < end; ++i)
if (News[i]->type == type && News[i]->num > num)
--News[i]->num;
return MOD_CONT;
}
}
else
source.Reply(msgs[MSG_DEL_NOT_FOUND], num);
catch (const ConvertException &) { }
source.Reply(msgs[MSG_DEL_NOT_FOUND], num);
}
else
{
+19 -4
View File
@@ -125,7 +125,12 @@ class CommandOSSession : public Command
{
Anope::string param = params[1];
unsigned mincount = param.is_pos_number_only() ? convertTo<unsigned>(param) : 0;
unsigned mincount = 0;
try
{
mincount = convertTo<unsigned>(param);
}
catch (const ConvertException &) { }
if (mincount <= 1)
source.Reply(_("Invalid threshold value. It must be a valid integer greater than 1."));
@@ -260,7 +265,12 @@ class CommandOSException : public Command
else if (expires > 0)
expires += Anope::CurTime;
int limit = !limitstr.empty() && limitstr.is_number_only() ? convertTo<int>(limitstr) : -1;
int limit = -1;
try
{
limit = convertTo<int>(limitstr);
}
catch (const ConvertException &) { }
if (limit < 0 || limit > static_cast<int>(Config->MaxSessionLimit))
{
@@ -334,8 +344,13 @@ class CommandOSException : public Command
return MOD_CONT;
}
n1 = n1str.is_pos_number_only() ? convertTo<int>(n1str) - 1 : -1;
n2 = n2str.is_pos_number_only() ? convertTo<int>(n2str) - 1 : -1;
n1 = n2 = -1;
try
{
n1 = convertTo<int>(n1str);
n2 = convertTo<int>(n2str);
}
catch (const ConvertException &) { }
if (n1 >= 0 && n1 < exceptions.size() && n2 >= 0 && n2 < exceptions.size() && n1 != n2)
{
+12 -7
View File
@@ -122,14 +122,19 @@ class CommandOSSet : public Command
debug = 0;
source.Reply(_("Services are now in non-debug mode."));
}
else if (setting.is_number_only() && convertTo<int>(setting) > 0)
{
debug = convertTo<int>(setting);
Log(LOG_ADMIN, u, this) << "DEBUG " << debug;
source.Reply(_("Services are now in debug mode (level %d)."), debug);
}
else
source.Reply(_("Setting for DEBUG must be \002\002, \002\002, or a positive number."));
{
try
{
debug = convertTo<int>(setting);
Log(LOG_ADMIN, u, this) << "DEBUG " << debug;
source.Reply(_("Services are now in debug mode (level %d)."), debug);
return MOD_CONT;
}
catch (const ConvertException &) { }
source.Reply(_("Setting for DEBUG must be \002ON\002, \002OFF\002, or a positive number."));
}
return MOD_CONT;
}
+13 -6
View File
@@ -69,15 +69,22 @@ class CommandEntryMessage : public Command
source.Reply(("Entry message \002%s\002 not found on channel \002%s\002."), message.c_str(), ci->name.c_str());
else if (ci->GetExtRegular("cs_entrymsg", messages))
{
unsigned i = convertTo<unsigned>(message);
if (i > 0 && i <= messages.size())
try
{
messages.erase(messages.begin() + i - 1);
ci->Extend("cs_entrymsg", new ExtensibleItemRegular<std::vector<EntryMsg> >(messages));
source.Reply(_("Entry message \2%i\2 for \2%s\2 deleted."), i, ci->name.c_str());
unsigned i = convertTo<unsigned>(message);
if (i <= messages.size())
{
messages.erase(messages.begin() + i - 1);
ci->Extend("cs_entrymsg", new ExtensibleItemRegular<std::vector<EntryMsg> >(messages));
source.Reply(_("Entry message \2%i\2 for \2%s\2 deleted."), i, ci->name.c_str());
}
else
throw ConvertException();
}
else
catch (const ConvertException &)
{
source.Reply(_("Entry message \2%s\2 not found on channel \2%s\2."), message.c_str(), ci->name.c_str());
}
}
else
source.Reply(_("Entry message list for \2%s\2 is empty."), ci->name.c_str());