Avoid any potential int overflow

This commit is contained in:
louiz’
2017-05-09 15:46:20 +02:00
parent c046d2ea98
commit f7e4adb10b
3 changed files with 13 additions and 5 deletions
+5 -1
View File
@@ -200,7 +200,11 @@ int Poller::poll(const std::chrono::milliseconds& timeout)
// Unblock all signals, only during the epoll_pwait call // Unblock all signals, only during the epoll_pwait call
sigset_t empty_signal_set{}; sigset_t empty_signal_set{};
sigemptyset(&empty_signal_set); sigemptyset(&empty_signal_set);
const int nb_events = ::epoll_pwait(this->epfd, revents, max_events, timeout.count(),
int real_timeout = std::numeric_limits<int>::max();
if (timeout.count() < real_timeout) // Just avoid any potential int overflow
real_timeout = static_cast<int>(timeout.count());
const int nb_events = ::epoll_pwait(this->epfd, revents, max_events, real_timeout,
&empty_signal_set); &empty_signal_set);
if (nb_events == -1) if (nb_events == -1)
{ {
+3 -2
View File
@@ -227,9 +227,10 @@ void TCPSocketHandler::consume_in_buffer(const std::size_t size)
} }
#ifdef BOTAN_FOUND #ifdef BOTAN_FOUND
void TCPSocketHandler::start_tls(const std::string& address, const std::string& port) void TCPSocketHandler::start_tls(const std::string& address, const std::string& port_string)
{ {
Botan::TLS::Server_Information server_info(address, "irc", std::stoul(port)); auto port = std::min(std::stoul(port_string), static_cast<unsigned long>(std::numeric_limits<uint16_t>::max()));
Botan::TLS::Server_Information server_info(address, "irc", static_cast<uint16_t>(port));
auto policy_directory = Config::get("policy_directory", utils::dirname(Config::get_filename())); auto policy_directory = Config::get("policy_directory", utils::dirname(Config::get_filename()));
if (!policy_directory.empty() && policy_directory[policy_directory.size()-1] != '/') if (!policy_directory.empty() && policy_directory[policy_directory.size()-1] != '/')
policy_directory += '/'; policy_directory += '/';
+5 -2
View File
@@ -112,17 +112,20 @@ void XmppComponent::on_connection_close(const std::string& error)
void XmppComponent::parse_in_buffer(const size_t size) void XmppComponent::parse_in_buffer(const size_t size)
{ {
// in_buf.size, or size, cannot be bigger than our read-size (4096) so it’s safe
// to cast.
if (!this->in_buf.empty()) if (!this->in_buf.empty())
{ // This may happen if the parser could not allocate enough space for { // This may happen if the parser could not allocate enough space for
// us. We try to feed it the data that was read into our in_buf // us. We try to feed it the data that was read into our in_buf
// instead. If this fails again we are in trouble. // instead. If this fails again we are in trouble.
this->parser.feed(this->in_buf.data(), this->in_buf.size(), false); this->parser.feed(this->in_buf.data(), static_cast<int>(this->in_buf.size()), false);
this->in_buf.clear(); this->in_buf.clear();
} }
else else
{ // Just tell the parser to parse the data that was placed into the { // Just tell the parser to parse the data that was placed into the
// buffer it provided to us with GetBuffer // buffer it provided to us with GetBuffer
this->parser.parse(size, false); this->parser.parse(static_cast<int>(size), false);
} }
} }