server: catch invalid utf8 exceptions

issue reported by coverity
This commit is contained in:
Chris Beck 2014-11-17 19:43:50 -05:00
parent d45d8ad890
commit 1bc5aa91fb
2 changed files with 46 additions and 1 deletions

View file

@ -372,7 +372,16 @@ static lg::log_domain log_server("server");
return true;
}
default_ban_times::const_iterator time_itor = ban_times_.find(duration);
if (utf8::lowercase(duration) == "permanent" || duration == "0") {
std::string dur_lower;
try {
dur_lower = utf8::lowercase(duration);
} catch ( utf8::invalid_utf8_exception & e ) {
ERR_SERVER << "While parsing ban command duration string, caught an invalid utf8 exception: " << e.what() << std::endl;
return false;
}
if (dur_lower == "permanent" || duration == "0") {
*time = 0;
} else if (ban_times_.find(duration) != ban_times_.end()) {
*time += time_itor->second;

View file

@ -1054,6 +1054,8 @@ void server::process_login(const network::connection sock,
for (std::vector<std::string>::const_iterator d_it = disallowed_names_.begin();
d_it != disallowed_names_.end(); ++d_it)
{
try {
if (utils::wildcard_string_match(utf8::lowercase(username),
utf8::lowercase(*d_it)))
{
@ -1061,6 +1063,10 @@ void server::process_login(const network::connection sock,
MP_NAME_RESERVED_ERROR);
return;
}
} catch ( utf8::invalid_utf8_exception & e ) {
ERR_SERVER << "While checking a username vs a list of disallowed names, caught an invalid utf8 exception: " << e.what() << std::endl;
}
}
// If this is a request for password reminder
@ -1378,6 +1384,9 @@ std::string server::process_command(std::string query, std::string issuer_name)
}
const std::string::iterator i = std::find(query.begin(), query.end(), ' ');
try {
const std::string command = utf8::lowercase(std::string(query.begin(), i));
std::string parameters = (i == query.end() ? "" : std::string(i + 1, query.end()));
utils::strip(parameters);
@ -1393,10 +1402,18 @@ std::string server::process_command(std::string query, std::string issuer_name)
} catch (boost::bad_function_call & ex) {
ERR_SERVER << "While handling a command '" << command << "', caught a boost::bad_function_call exception.\n";
ERR_SERVER << ex.what() << std::endl;
out << "An internal server error occurred (boost::bad_function_call) while executing '" << command << "'\n";
}
}
return out.str();
} catch ( utf8::invalid_utf8_exception & e ) {
std::string msg = "While handling a command, caught an invalid utf8 exception: ";
msg += e.what();
ERR_SERVER << msg << std::endl;
return (msg + '\n');
}
}
// Shutdown, restart and sample commands can only be issued via the socket.
@ -1500,11 +1517,17 @@ void server::netstats_handler(const std::string& /*issuer_name*/, const std::str
<< stats.npending_sends << "\nBytes in buffers: "
<< stats.nbytes_pending_sends << "\n";
try {
if (utf8::lowercase(parameters) == "all") {
*out << network::get_bandwidth_stats_all();
} else {
*out << network::get_bandwidth_stats(); // stats from previuos hour
}
} catch ( utf8::invalid_utf8_exception & e ) {
ERR_SERVER << "While handling a netstats command, caught an invalid utf8 exception: " << e.what() << std::endl;
}
}
void server::adminmsg_handler(const std::string& issuer_name, const std::string& /*query*/, std::string& parameters, std::ostringstream *out) {
@ -1672,6 +1695,9 @@ void server::clones_handler(const std::string& /*issuer_name*/, const std::strin
void server::bans_handler(const std::string& /*issuer_name*/, const std::string& /*query*/, std::string& parameters, std::ostringstream *out) {
assert(out != NULL);
try
{
if (parameters.empty()) {
ban_manager_.list_bans(*out);
} else if (utf8::lowercase(parameters) == "deleted") {
@ -1682,6 +1708,10 @@ void server::bans_handler(const std::string& /*issuer_name*/, const std::string&
} else {
ban_manager_.list_bans(*out, utils::strip(parameters));
}
} catch ( utf8::invalid_utf8_exception & e ) {
ERR_SERVER << "While handling bans, caught an invalid utf8 exception: " << e.what() << std::endl;
}
}
void server::ban_handler(const std::string& issuer_name, const std::string& /*query*/, std::string& parameters, std::ostringstream *out) {
@ -2015,12 +2045,18 @@ void server::searchlog_handler(const std::string& /*issuer_name*/, const std::st
void server::dul_handler(const std::string& /*issuer_name*/, const std::string& /*query*/, std::string& parameters, std::ostringstream *out) {
assert(out != NULL);
try {
if (parameters == "") {
*out << "Unregistered login is " << (deny_unregistered_login_ ? "disallowed" : "allowed") << ".";
} else {
deny_unregistered_login_ = (utf8::lowercase(parameters) == "yes");
*out << "Unregistered login is now " << (deny_unregistered_login_ ? "disallowed" : "allowed") << ".";
}
} catch ( utf8::invalid_utf8_exception & e ) {
ERR_SERVER << "While handling dul (deny unregistered logins), caught an invalid utf8 exception: " << e.what() << std::endl;
}
}
void server::process_nickserv(const network::connection sock, simple_wml::node& data) {