From: Guilherme Maciel Ferreira Date: Tue, 5 Apr 2011 09:58:47 +0000 (+0200) Subject: Fixed more problems issued by PC-lint. X-Git-Tag: v1.0~97 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=a4049623b33292085a9e06364326ea6def40cd38;p=pingcheck Fixed more problems issued by PC-lint. --- diff --git a/src/config/configurationreader.cpp b/src/config/configurationreader.cpp index 9e5c2c8..a110e71 100644 --- a/src/config/configurationreader.cpp +++ b/src/config/configurationreader.cpp @@ -86,8 +86,8 @@ options_description ConfigurationReader::get_generic_options() const bool ConfigurationReader::is_generic_options( const variables_map &vm ) const { - bool is_help = vm.count( HelpCmdStr ); - bool is_version = vm.count( VersionCmdStr ); + bool is_help = ( vm.count( HelpCmdStr ) > 0 ); + bool is_version = ( vm.count( VersionCmdStr ) > 0 ); bool is_a_generic_option = is_help || is_version; return is_a_generic_option; @@ -178,7 +178,7 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) } // [host] name - int hosts_names_count = 0; + size_t hosts_names_count = 0; if ( vm.count( HostNameCmdStr ) ) { vector hosts_list; @@ -196,11 +196,11 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) hosts_names_count = hosts_names.size(); - BOOST_ASSERT( hosts_names_count >= host_down_limit ); + BOOST_ASSERT( hosts_names_count >= static_cast( host_down_limit ) ); } // [host] interval - int hosts_interval_count = 0; + size_t hosts_interval_count = 0; if ( vm.count( HostIntervalCmdStr ) ) { vector hosts_list = Config.get_hosts(); @@ -244,8 +244,7 @@ bool ConfigurationReader::process_command_line( visible.add( generic ).add( config ); positional_options_description p; - p.add( HostNameCmdStr.c_str(), -1 ); - //p.add( HostIntervalCmdStr.c_str(), -1 ); + (void) p.add( HostNameCmdStr.c_str(), -1 ); parsed_options parsed_opt = command_line_parser( argc, argv ). options( cmdline_options ).positional( p ).run(); @@ -254,7 +253,7 @@ bool ConfigurationReader::process_command_line( if ( is_generic_options( vm ) ) { - parse_generic_options( vm, visible ); + (void) parse_generic_options( vm, visible ); return false; } @@ -289,7 +288,7 @@ bool ConfigurationReader::process_configuration_file( ifstream ifs( config_file_name.c_str() ); if ( !ifs ) { - cerr << "Error: could not open " << config_file_name << " file\n" + cerr << "Error: could not open " << config_file_name << " file." << endl; return false; } diff --git a/src/dns/dnsresolver.cpp b/src/dns/dnsresolver.cpp index ee16967..b67d2e8 100644 --- a/src/dns/dnsresolver.cpp +++ b/src/dns/dnsresolver.cpp @@ -33,7 +33,7 @@ bool DnsResolver::resolve() { BOOST_ASSERT( !HostDnsAddress.empty() ); - cerr << "Resolved IP(s) for host : " << HostDnsAddress << endl; + cout << "Resolved IP(s) for host : " << HostDnsAddress << endl; try { @@ -50,7 +50,7 @@ bool DnsResolver::resolve() resolved_host.set_ip( dest_ip ); ResolvedHostAddressList.push_back( resolved_host ); - cerr << "- " << dest_ip << endl; + cout << "- " << dest_ip << endl; ++it_first; } @@ -72,7 +72,7 @@ bool DnsResolver::resolve() */ int DnsResolver::get_resolved_ip_count() const { - int resolved_ip_count = ResolvedHostAddressList.size(); + size_t resolved_ip_count = ResolvedHostAddressList.size(); BOOST_ASSERT( 1 <= resolved_ip_count ); @@ -85,14 +85,14 @@ int DnsResolver::get_resolved_ip_count() const */ string DnsResolver::get_next_ip() { - int list_size_before = ResolvedHostAddressList.size(); + size_t list_size_before = ResolvedHostAddressList.size(); HostAddress host_address = ResolvedHostAddressList.front(); ResolvedHostAddressList.pop_front(); string destination_ip = host_address.get_ip(); ResolvedHostAddressList.push_back( host_address ); - int list_size_after = ResolvedHostAddressList.size(); + size_t list_size_after = ResolvedHostAddressList.size(); BOOST_ASSERT( list_size_before == list_size_after ); diff --git a/src/icmp/checksumcalculator.h b/src/icmp/checksumcalculator.h index ec8d17f..fb5673f 100644 --- a/src/icmp/checksumcalculator.h +++ b/src/icmp/checksumcalculator.h @@ -24,7 +24,7 @@ public: uint8_t code, uint16_t identifier, uint16_t sequence_number - ); + ) const; private: Iterator BodyBegin; @@ -48,7 +48,7 @@ template uint8_t code, uint16_t identifier, uint16_t sequence_number - ) + ) const { uint32_t sum = (type << 8) + code + identifier + sequence_number; diff --git a/src/icmp/icmpchecksumcalculator.h b/src/icmp/icmpchecksumcalculator.h index 309f01c..80f32b2 100644 --- a/src/icmp/icmpchecksumcalculator.h +++ b/src/icmp/icmpchecksumcalculator.h @@ -8,6 +8,6 @@ // IcmpChecksumCalculator //----------------------------------------------------------------------------- -typedef ChecksumCalculator IcmpChecksumCalculator; +typedef ChecksumCalculator IcmpChecksumCalculator; #endif /* ICMPCHECKSUMCALCULATOR_H */ diff --git a/src/icmp/ipv4header.cpp b/src/icmp/ipv4header.cpp index 796500a..1a3a0ec 100644 --- a/src/icmp/ipv4header.cpp +++ b/src/icmp/ipv4header.cpp @@ -1,22 +1,25 @@ #include "icmp/ipv4header.h" +using namespace std; +using namespace boost::asio::ip; + //----------------------------------------------------------------------------- // Ipv4Header //----------------------------------------------------------------------------- Ipv4Header::Ipv4Header() { - std::fill( Rep, Rep + sizeof(Rep), 0 ); + fill( Rep, Rep + sizeof(Rep), 0 ); } uint8_t Ipv4Header::get_version() const { - return (Rep[ 0 ] >> 4) & 0xF; + return ( Rep[ 0 ] >> 4 ) & 0xF; } uint16_t Ipv4Header::get_header_length() const { - return (Rep[ 0 ] & 0xF) * 4; + return ( Rep[ 0 ] & 0xF ) * 4; } uint8_t Ipv4Header::get_type_of_service() const @@ -64,39 +67,47 @@ uint16_t Ipv4Header::get_header_checksum() const return decode( 10, 11 ); } -boost::asio::ip::address_v4 Ipv4Header::get_source_address() const +address_v4 Ipv4Header::get_source_address() const { - boost::asio::ip::address_v4::bytes_type bytes = { { + address_v4::bytes_type bytes = { { Rep[ 12 ], Rep[ 13 ], Rep[ 14 ], Rep[ 15 ] } }; - return boost::asio::ip::address_v4( bytes ); + return address_v4( bytes ); } -boost::asio::ip::address_v4 Ipv4Header::get_destination_address() const +address_v4 Ipv4Header::get_destination_address() const { - boost::asio::ip::address_v4::bytes_type bytes = { { + address_v4::bytes_type bytes = { { Rep[ 16 ], Rep[ 17 ], Rep[ 18 ], Rep[ 19 ] } }; - return boost::asio::ip::address_v4( bytes ); + return address_v4( bytes ); } -std::istream& operator>>( - std::istream &is, +istream &operator>>( + istream &is, Ipv4Header &header ) { - is.read( reinterpret_cast ( header.Rep ), 20 ); + (void) is.read( reinterpret_cast ( header.Rep ), 20 ); if ( header.get_version() != 4 ) - is.setstate( std::ios::failbit ); - std::streamsize options_length = header.get_header_length() - 20; - if ( options_length < 0 || options_length > 40 ) - is.setstate( std::ios::failbit ); + is.setstate( ios::failbit ); + streamsize options_length = (streamsize) header.get_header_length() - 20; + if ( options_length < 0 || 40 < options_length ) + { + is.setstate( ios::failbit ); + } else - is.read( reinterpret_cast ( header.Rep ) + 20, options_length ); + { + (void) is.read( + reinterpret_cast ( header.Rep ) + 20, + options_length + ); + } + return is; } diff --git a/src/icmp/ipv4header.h b/src/icmp/ipv4header.h index 7f23310..828442f 100644 --- a/src/icmp/ipv4header.h +++ b/src/icmp/ipv4header.h @@ -64,7 +64,7 @@ public: boost::asio::ip::address_v4 get_source_address() const; boost::asio::ip::address_v4 get_destination_address() const; - friend std::istream& operator>>( + friend std::istream &operator>>( std::istream &is, Ipv4Header &header ); diff --git a/src/main.cpp b/src/main.cpp index e3894d4..f7e8042 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -60,11 +61,15 @@ int main( int argc, char* argv[] ) // TODO ping_loop() BOOST_FOREACH( PingSchedulerItem scheduler, scheduler_list ) { - scheduler->start_pinging(); + bool started = scheduler->start_pinging(); + if ( !started ) + { + cerr << "Error: could not start pinger." << endl; + } } // Main loop to handle scheduled ping requests - io_serv.run(); + (void) io_serv.run(); } return 0; diff --git a/src/notify/linkstatusanalyzer.cpp b/src/notify/linkstatusanalyzer.cpp index ecc1f8f..3b8eb8a 100644 --- a/src/notify/linkstatusanalyzer.cpp +++ b/src/notify/linkstatusanalyzer.cpp @@ -71,13 +71,15 @@ void LinkStatusAnalyzer::add_host_up( const string &host_address ) { if ( HostsDownList.count( host_address ) > 0 ) { - HostsDownList.erase( host_address ); + size_t erased_host_count = HostsDownList.erase( host_address ); + + BOOST_ASSERT( erased_host_count == 1 ); } } void LinkStatusAnalyzer::add_host_down( const string &host_address ) { - HostsDownList.insert( host_address ); + (void) HostsDownList.insert( host_address ); } bool LinkStatusAnalyzer::exceeded_host_down_limit() const @@ -97,14 +99,17 @@ void LinkStatusAnalyzer::notify_link_up() BOOST_ASSERT( CurrentLinkStatus == LinkStatus_Up ); BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported ); - StatusNotifierCmd.set_token_value( + bool replaced = StatusNotifierCmd.set_token_value( StatusNotifierCommand::StatusToken, "up" ); - StatusNotifierCmd.execute(); + bool executed = StatusNotifierCmd.execute(); - CurrentNotificationStatus = NotificationStatus_Reported; + if ( replaced && executed ) + { + CurrentNotificationStatus = NotificationStatus_Reported; + } } } @@ -117,14 +122,17 @@ void LinkStatusAnalyzer::notify_link_down() BOOST_ASSERT( CurrentLinkStatus == LinkStatus_Down ); BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported ); - StatusNotifierCmd.set_token_value( + bool replaced = StatusNotifierCmd.set_token_value( StatusNotifierCommand::StatusToken, "down" ); - StatusNotifierCmd.execute(); + bool executed = StatusNotifierCmd.execute(); - CurrentNotificationStatus = NotificationStatus_Reported; + if ( replaced && executed ) + { + CurrentNotificationStatus = NotificationStatus_Reported; + } } } diff --git a/src/notify/statusnotifiercommand.cpp b/src/notify/statusnotifiercommand.cpp index d1c753a..dfd5898 100644 --- a/src/notify/statusnotifiercommand.cpp +++ b/src/notify/statusnotifiercommand.cpp @@ -45,7 +45,7 @@ bool StatusNotifierCommand::set_token_value( } const size_t token_size = token.length(); - CommandStr.replace( token_begin_pos, token_size, value ); + (void) CommandStr.replace( token_begin_pos, token_size, value ); // assert the token is no longer within the command string BOOST_ASSERT( CommandStr.find( token ) == string::npos ); diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index ca1c921..406ad66 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -73,7 +73,7 @@ void BoostPinger::start_pinger() start_send(); start_receive(); - IoService.run(); + (void) IoService.run(); } void BoostPinger::stop_pinger() @@ -101,7 +101,7 @@ IcmpPacket BoostPinger::create_echo_request( const uint16_t sequence_number ) const { - IcmpData icmp_data( "ping-message" ); + const IcmpData icmp_data( "ping-message" ); IcmpHeader::IcmpType type = IcmpHeader::IcmpType_EchoRequest; uint8_t code = 0; @@ -128,7 +128,13 @@ void BoostPinger::send_echo_request( const IcmpPacket &icmp_packet ) // Send the request. string dest_address_string = DestinationEndpoint.address().to_string(); BOOST_ASSERT( !dest_address_string.empty() ); - Socket.send_to( request_buffer.data(), DestinationEndpoint ); + + const_buffers_1 data = request_buffer.data(); + size_t bytes_sent = Socket.send_to( data, DestinationEndpoint ); + if ( bytes_sent != buffer_size( data ) ) + { + cerr << "Error: fail sending ping data." << endl; + } schedule_timeout_echo_reply(); } @@ -137,7 +143,7 @@ void BoostPinger::schedule_timeout_echo_reply() { // Wait up to N seconds for a reply. RepliesCount = 0; - Timer.expires_at( TimeSent + seconds( EchoReplyTimeoutInSec ) ); + (void) Timer.expires_at( TimeSent + seconds( EchoReplyTimeoutInSec ) ); Timer.async_wait( boost::bind( &BoostPinger::handle_timeout_echo_reply, this ) ); @@ -159,7 +165,7 @@ void BoostPinger::schedule_next_echo_request() { // Requests must be sent no less than one second apart. const int echo_request_interval_in_sec = 1; - Timer.expires_at( TimeSent + seconds( echo_request_interval_in_sec ) ); + (void) Timer.expires_at( TimeSent + seconds( echo_request_interval_in_sec ) ); Timer.async_wait( boost::bind( &BoostPinger::start_send, this ) ); } @@ -194,7 +200,7 @@ void BoostPinger::handle_receive_echo_reply( const size_t &bytes_transferred ) // If this is the first reply, interrupt the echo reply timeout. if ( RepliesCount == 0 ) { - Timer.cancel(); + (void) Timer.cancel(); } ++RepliesCount; diff --git a/src/ping/hoststatusanalyzer.cpp b/src/ping/hoststatusanalyzer.cpp index 9d763af..ca9f683 100644 --- a/src/ping/hoststatusanalyzer.cpp +++ b/src/ping/hoststatusanalyzer.cpp @@ -20,7 +20,7 @@ using namespace boost; HostStatusAnalyzer::HostStatusAnalyzer( const string &host_address, const int ping_fail_percentage_limit, - shared_ptr link_analyzer + const shared_ptr link_analyzer ) : HostAddress( host_address ), LinkAnalyzer( link_analyzer ), diff --git a/src/ping/hoststatusanalyzer.h b/src/ping/hoststatusanalyzer.h index d9e127f..b215319 100644 --- a/src/ping/hoststatusanalyzer.h +++ b/src/ping/hoststatusanalyzer.h @@ -22,7 +22,7 @@ public: HostStatusAnalyzer( const std::string &host_address, const int ping_fail_percentage_limit, - boost::shared_ptr link_analyzer + const boost::shared_ptr link_analyzer ); virtual ~HostStatusAnalyzer(); @@ -40,7 +40,7 @@ private: private: std::string HostAddress; - boost::shared_ptr LinkAnalyzer; + const boost::shared_ptr LinkAnalyzer; int PingFailPercentageLimit; int ResolvedIpCount; int PingsPerformedCount; diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index aae3204..47f68cf 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -78,22 +78,22 @@ void PingScheduler::setup_ping() schedule_next_ping(); } -bool PingScheduler::ping( const string &destination ) +bool PingScheduler::ping( const string &destination_ip ) const { - BOOST_ASSERT( !destination.empty() ); + BOOST_ASSERT( !destination_ip.empty() ); - io_service io_service; + io_service io_serv; int echo_reply_timeout_in_sec = 5; // TODO configurable: this is the timeout to WAIT FOR the ping before considering a timeout - BoostPinger pinger( io_service, echo_reply_timeout_in_sec ); + BoostPinger pinger( io_serv, echo_reply_timeout_in_sec ); - return pinger.ping( destination ); + return pinger.ping( destination_ip ); } void PingScheduler::schedule_next_ping() { BOOST_ASSERT( 0 < PingIntervalInSec ); - Timer.expires_from_now( seconds( PingIntervalInSec ) ); + (void) Timer.expires_from_now( seconds( PingIntervalInSec ) ); Timer.async_wait( bind( &PingScheduler::handle_next_ping, this ) ); } diff --git a/src/ping/pingscheduler.h b/src/ping/pingscheduler.h index d2486df..ef0b14f 100644 --- a/src/ping/pingscheduler.h +++ b/src/ping/pingscheduler.h @@ -37,7 +37,7 @@ public: private: bool resolve_ping_address(); void setup_ping(); - bool ping( const std::string &destination ); + bool ping( const std::string &destination_ip ) const; void schedule_next_ping(); void handle_next_ping(); void update_ping_statistics( const bool ping_success );