From 557b6f565d1c2f488be2d9fef67a1694c123c1b9 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Tue, 18 May 2010 11:39:27 +0200 Subject: [PATCH] Last linter optimizations. --- bpdyndnsd.lnt | 4 ++++ src/ip_addr_helper.cpp | 2 +- src/logger.cpp | 6 +++--- src/main.cpp | 10 +++++----- src/net_helper.cpp | 14 ++++++++++++++ src/service.cpp | 2 +- src/service_gnudip.cpp | 6 +++--- src/service_ods.cpp | 22 +++++++++++----------- src/serviceholder.cpp | 2 +- src/tcp_service.cpp | 18 +++++++++--------- src/tcp_service.h | 8 ++++---- src/util.cpp | 29 ++++++++++++++++------------- 12 files changed, 72 insertions(+), 51 deletions(-) diff --git a/bpdyndnsd.lnt b/bpdyndnsd.lnt index 0174dc3..cad6d39 100644 --- a/bpdyndnsd.lnt +++ b/bpdyndnsd.lnt @@ -8,8 +8,12 @@ // Disable bogus BOOST warnings -emacro(58,BOOST_ASSERT) -emacro(*, BOOST_FOREACH) +-esym(665, BOOST_FOREACH) -emacro(121, BOOST_CLASS_EXPORT_GUID) -esym(1024, boost::lexical_cast) +-esym(534, boost::program_options::options_description_easy_init::operator*) +-esym(534, boost::algorithm::split) +-esym(534, boost::program_options::options_description::add) // Don't show errors in (boost) library headers -wlib(0) diff --git a/src/ip_addr_helper.cpp b/src/ip_addr_helper.cpp index e0e8dd1..af88ee4 100644 --- a/src/ip_addr_helper.cpp +++ b/src/ip_addr_helper.cpp @@ -377,7 +377,7 @@ string IPAddrHelper::webcheck_ip() time_t current_time = time(NULL); // Test if webcheck is allowed due to webcheck_interval. - if ( (LastWebcheck + ((time_t)(WebcheckInterval*60))) >= current_time ) + if ( (LastWebcheck + ((time_t)WebcheckInterval*60)) >= current_time ) { // Webcheck not allowed, log it and return empty string. Log->print_webcheck_exceed_interval( LastWebcheck, (WebcheckInterval*60), current_time ); diff --git a/src/logger.cpp b/src/logger.cpp index e6f1f15..cd3d024 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1174,7 +1174,7 @@ void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode cu if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; - msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " "<< curl_err << endl; // lint !e 641 + msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */ log_error(msg.str()); } } @@ -1202,7 +1202,7 @@ void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) c if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; - msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " "<< curl_err << endl; // lint !e 641 + msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << endl; /*lint !e641 */ log_warning(msg.str(),level); } } @@ -1231,7 +1231,7 @@ void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, c if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; - msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " "<< curl_err << " " << curl_err_buff << endl; // lint !e 641 + msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " " << curl_err << " " << curl_err_buff << endl; /*lint !e641 */ log_warning(msg.str(),level); } } diff --git a/src/main.cpp b/src/main.cpp index 42a40b3..f26b482 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -221,16 +221,16 @@ int init_daemon_mode(bool daemon_mode) if ( kill(pid,SIGTERM) != 0 ) { updater->get_logger()->print_error_kill_child(pid); - shutdown_parent(false,-1); // lint !e 534 // keep pidfile + shutdown_parent(false,-1); /*lint !e534 */ } else { updater->get_logger()->print_child_killed(pid); - shutdown_parent(true,-1); // lint !e 534 // remove pidfile + shutdown_parent(true,-1); /*lint !e534 */ } } updater->get_logger()->print_runnig_as_daemon(pid); - shutdown_parent(false,0); // lint !e 534 // keep pidfile + shutdown_parent(false,0); /*lint !e534 */ } // child starts here } @@ -238,7 +238,7 @@ int init_daemon_mode(bool daemon_mode) { if ( write_pidfile(getpid()) != 0 ) { - shutdown(); // lint !e 534 + shutdown(); /*lint !e534 */ exit(-1); } } @@ -288,7 +288,7 @@ int main(int argc, char *argv[]) // Snore, snore... don't hug the cpu if we are in daemon_mode. if ( updater->get_config()->get_daemon_mode() == 1 ) - sleep(10); // lint !e 534 + sleep(10); /*lint !e534 */ }while ( updater->get_config()->get_daemon_mode() == 1 ); diff --git a/src/net_helper.cpp b/src/net_helper.cpp index 47a325f..f826823 100644 --- a/src/net_helper.cpp +++ b/src/net_helper.cpp @@ -60,6 +60,20 @@ int NetHelper::open_connection(const string& hostname, const string& port) const Log->print_network_error(out.str()); return -1; } + catch ( exception& e ) + { + ostringstream out; + out << "NetHelper::open_connection(): " << e.what() << " Host: " << hostname << " Port: " << port; + Log->print_network_error(out.str()); + return -1; + } + catch ( ... ) + { + ostringstream out; + out << "Unknown exception caught while trying to connect to Host: " << hostname << " Port: " << port; + Log->print_network_error(out.str()); + return -1; + } return 0; } diff --git a/src/service.cpp b/src/service.cpp index 519c655..0d0c590 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -210,7 +210,7 @@ bool Service::update_allowed(const time_t current_time) for (iter = LastUpdates.begin(); (iter != LastUpdates.end()) && ( i < MaxUpdatesWithinInterval ); iter++) { - if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + ((time_t)(UpdateInterval*60))) >= current_time ) ) + if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + ((time_t)UpdateInterval*60)) >= current_time ) ) { Log->print_update_not_allowed(current_time,*iter,MaxUpdatesWithinInterval,get_service_name()); return false; diff --git a/src/service_gnudip.cpp b/src/service_gnudip.cpp index a63a787..7e07d2a 100644 --- a/src/service_gnudip.cpp +++ b/src/service_gnudip.cpp @@ -108,7 +108,7 @@ map ServiceGnudip::parse_initial_request(const string& curl_data) // Get the salt out of received http data if ( boost::regex_search(curl_data,matches,expr_salt) ) { - response.insert(pair("salt",matches[1].str())); // lint !e 534 + response.insert(pair("salt",matches[1].str())); /*lint !e534 */ get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); } else @@ -121,7 +121,7 @@ map ServiceGnudip::parse_initial_request(const string& curl_data) // Get the time out of received http data if ( boost::regex_search(curl_data,matches,expr_time) ) { - response.insert(pair("time",matches[1].str())); // lint !e 534 + response.insert(pair("time",matches[1].str())); /*lint !e534 */ get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); } else @@ -134,7 +134,7 @@ map ServiceGnudip::parse_initial_request(const string& curl_data) // Get the sign out of received http data if ( boost::regex_search(curl_data,matches,expr_sign) ) { - response.insert(pair("sign",matches[1].str())); // lint !e 534 + response.insert(pair("sign",matches[1].str())); /*lint !e534 */ get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); } else diff --git a/src/service_ods.cpp b/src/service_ods.cpp index 7515e0f..ea86f5f 100644 --- a/src/service_ods.cpp +++ b/src/service_ods.cpp @@ -81,7 +81,7 @@ int ServiceOds::perform_update(const std::string& ip) string server_greeting = connection->receive_data(); if ( server_greeting.empty() ) { - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } @@ -90,7 +90,7 @@ int ServiceOds::perform_update(const std::string& ip) login << "LOGIN " << get_login() << " " << get_password() << endl; if (connection->send_data(login.str()) != 0) { - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } @@ -98,7 +98,7 @@ int ServiceOds::perform_update(const std::string& ip) string login_reply = connection->receive_data(); if ( login_reply.empty() ) { - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } else @@ -109,14 +109,14 @@ int ServiceOds::perform_update(const std::string& ip) { // Login failed get_logger()->print_service_not_authorized(UpdateServer,get_login(),get_password()); - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } else if ( status_code != "225" ) { // Undefined protocol error, Log login_reply get_logger()->print_undefined_protocol_error("ODS",login_reply); - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } } @@ -133,14 +133,14 @@ int ServiceOds::perform_update(const std::string& ip) // Send delete request if (connection->send_data(delete_request.str()) != 0) { - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } // Get delete reply delete_reply = connection->receive_data(); if ( delete_reply.empty() ) { - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } del_count++; @@ -153,7 +153,7 @@ int ServiceOds::perform_update(const std::string& ip) update_request << "ADDRR " << get_hostname() << " A " << ip << endl; if (connection->send_data(update_request.str()) != 0) { - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } @@ -161,7 +161,7 @@ int ServiceOds::perform_update(const std::string& ip) string update_reply = connection->receive_data(); if ( update_reply.empty() ) { - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } else @@ -171,12 +171,12 @@ int ServiceOds::perform_update(const std::string& ip) if ( status_code != "795" ) { get_logger()->print_undefined_protocol_error("ODS",update_reply); - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return -1; } } // Successfully updated host - connection->close_connection(); + connection->close_connection(); /*lint !e534 */ return 0; } diff --git a/src/serviceholder.cpp b/src/serviceholder.cpp index 9cde9fe..e4beb18 100644 --- a/src/serviceholder.cpp +++ b/src/serviceholder.cpp @@ -67,7 +67,7 @@ int Serviceholder::serialize_services() const BOOST_FOREACH(const Service::Ptr &service, OldServices) { - if ( ( service->get_last_updates().front() + ((time_t)(service->get_update_interval()*60) )) >= current_time ) // UpdateInterval timeout of service isn't expired. + if ( ( service->get_last_updates().front() + ((time_t)service->get_update_interval()*60) ) >= current_time ) // UpdateInterval timeout of service isn't expired. service_container->add_service(service); } diff --git a/src/tcp_service.cpp b/src/tcp_service.cpp index 32056f9..496534a 100644 --- a/src/tcp_service.cpp +++ b/src/tcp_service.cpp @@ -39,7 +39,7 @@ TCPService::~TCPService() * @param endpoint_iterator The enpoint iterator. * @return throws boost::syste::system_error if something went wrong. */ -void TCPService::connect(const std::string& _host, const std::string& _port) throw (boost::system::system_error) +void TCPService::connect(const std::string& _host, const std::string& _port) { // Init boost::system::error_code boost::system::error_code err_code; @@ -69,15 +69,15 @@ void TCPService::connect(const std::string& _host, const std::string& _port) thr { do { - Socket->lowest_layer().close(); - Socket->lowest_layer().connect(*endpoint_iterator++, err_code); + Socket->lowest_layer().close(); /*lint !e534 */ + Socket->lowest_layer().connect(*endpoint_iterator++, err_code); /*lint !e534 */ } while ( err_code && (endpoint_iterator != end_iter) ); } if ( err_code ) throw boost::system::system_error(err_code); // Perform SSL handshake - Socket->handshake(boost::asio::ssl::stream_base::client,err_code); + Socket->handshake(boost::asio::ssl::stream_base::client,err_code); /*lint !e534*/ if ( err_code ) throw boost::system::system_error(err_code); @@ -89,7 +89,7 @@ void TCPService::connect(const std::string& _host, const std::string& _port) thr * Will read all available data and return it as a string. * @return The data read from the socket. Throws boost::syste::system_error if something went wrong. */ -std::string TCPService::read_from_socket() throw (boost::system::system_error) +std::string TCPService::read_from_socket() { // Limit stream buffer to 1MB //unsigned int max_buff_size = 1024*1024; @@ -125,7 +125,7 @@ std::string TCPService::read_from_socket() throw (boost::system::system_error) * @param data The data which will be written to the socket. * @return Throws boost::syste::system_error if something went wrong. */ -void TCPService::write_to_socket(const string& data) throw (boost::system::system_error) +void TCPService::write_to_socket(const string& data) { // Get the data size which will be written size_t data_size = data.size(); @@ -153,17 +153,17 @@ void TCPService::write_to_socket(const string& data) throw (boost::system::syste * Will close the session. * @return 0 if all is fine, throws boost::syste::system_error if something went wrong. */ -void TCPService::close() throw (boost::system::system_error) +void TCPService::close() { boost::system::error_code error_code; // Shutdown send and receive channel on socket. This results in sending FIN flag. - Socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both,error_code); + Socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both,error_code); /*lint !e534*/ if ( error_code ) throw boost::system::system_error(error_code); // Closing the session. - Socket->lowest_layer().close( error_code ); + Socket->lowest_layer().close( error_code ); /*lint !e534*/ if ( error_code ) throw boost::system::system_error(error_code); diff --git a/src/tcp_service.h b/src/tcp_service.h index 27a29c1..70360d6 100644 --- a/src/tcp_service.h +++ b/src/tcp_service.h @@ -37,13 +37,13 @@ public: ~TCPService(); - virtual void connect(const std::string& _hostname, const std::string& _port) throw (boost::system::system_error); + virtual void connect(const std::string& _hostname, const std::string& _port); - std::string read_from_socket() throw (boost::system::system_error); + std::string read_from_socket(); - void write_to_socket(const std::string& data) throw (boost::system::system_error); + void write_to_socket(const std::string& data); - void close() throw (boost::system::system_error); + void close(); }; #endif diff --git a/src/util.cpp b/src/util.cpp index 77e8963..8b795fc 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -45,46 +45,46 @@ std::string compute_md5_digest(std::string data) // Now we can call init_ex. if ( EVP_DigestInit_ex(&mdctx, md, NULL) == 0 ) { - EVP_MD_CTX_cleanup(&mdctx); - EVP_cleanup(); + EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ + EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Could not set up digest context correctly"); } // Test if data is empty. if ( data.empty() ) { - EVP_MD_CTX_cleanup(&mdctx); - EVP_cleanup(); + EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ + EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Passed data is empty"); } // Hash the data. At this point data is not empty and &mdctx is initialized. if ( EVP_DigestUpdate(&mdctx, data.c_str(), data.size()) == 0 ) { - EVP_MD_CTX_cleanup(&mdctx); - EVP_cleanup(); + EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ + EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Could not hash data into digest context"); } // Retrieve the digest value from &mdctx and place it in md_value. if ( EVP_DigestFinal_ex(&mdctx, md_value, &md_len) == 0 ) { - EVP_MD_CTX_cleanup(&mdctx); - EVP_cleanup(); + EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ + EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Could not retrieve digest value"); } // Test if md_value is filled correctly and md_len is not zero. if ( (md_len == 0) || (EVP_MD_CTX_size(&mdctx) == 0) ) { - EVP_MD_CTX_cleanup(&mdctx); - EVP_cleanup(); + EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ + EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Retrieved invalid digest value"); } // Internal cleanup of the digest content. - EVP_MD_CTX_cleanup(&mdctx); - EVP_cleanup(); + EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ + EVP_cleanup(); /*lint !e534 */ // Convert md5 digest C string to hex. std::ostringstream oss_digest_md5_hex; @@ -124,6 +124,9 @@ std::string parse_status_code(std::string data, std::string delimiter) { std::list tokens; boost::algorithm::split(tokens,data,boost::is_any_of(delimiter)); + if ( tokens.empty() ) + return ""; return tokens.front(); } -}; + +} -- 1.7.1