From 451c9121514c18ea85393080ab8ac5cf89ac0eb9 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 13 Apr 2011 10:48:29 +0200 Subject: [PATCH] Increased the warning level - included -pedantic, -Wshadow, -Wcast-qual and -Wconversion warnings - fixed some warnings regarding lost of precision from conversions - asserted that intermediate products from arithmetic operations fall within certain range --- CMakeLists.txt | 4 +++- src/host/boostpinger.cpp | 6 +++++- src/icmp/icmpmessagepayload.cpp | 8 +++++++- src/icmp/ipv4header.cpp | 10 ++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 124e950..d2a2cb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,9 @@ set(CMAKE_BUILD_TYPE Debug) # compiler: add definitions and arguments to the compiler add_definitions( - -Werror -Wall -Wextra -Weffc++ -O2 -DVERSION_STRING=${VERSION} + -Wall -Wextra -Weffc++ -Wshadow -Wcast-qual -Wconversion -pedantic + -Werror -O2 + -DVERSION_STRING=${VERSION} -DPROJECT_NAME=\"${PROJECT_NAME}\" ) diff --git a/src/host/boostpinger.cpp b/src/host/boostpinger.cpp index c8fcb1f..9f7306d 100644 --- a/src/host/boostpinger.cpp +++ b/src/host/boostpinger.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include #include "icmp/icmpchecksumcalculator.h" #include "icmp/icmpdata.h" @@ -18,6 +20,7 @@ using boost::asio::const_buffers_1; using boost::asio::io_service; using boost::asio::ip::address; using boost::asio::ip::icmp; +using boost::date_time::time_resolution_traits_adapted64_impl; using boost::posix_time::microsec_clock; using boost::posix_time::ptime; using boost::posix_time::seconds; @@ -281,7 +284,8 @@ void BoostPinger::print_echo_reply( uint16_t sequence_number = icmp_hdr.get_sequence_number(); int ttl = ipv4_hdr.get_time_to_live(); ptime now = microsec_clock::universal_time(); - int elapsed_time = (now - TimeSent).total_milliseconds(); + time_resolution_traits_adapted64_impl::int_type elapsed_time = + (now - TimeSent).total_milliseconds(); cout << bytes_received << " bytes " << "from " << remote_address diff --git a/src/icmp/icmpmessagepayload.cpp b/src/icmp/icmpmessagepayload.cpp index e6e8391..dbe0b17 100644 --- a/src/icmp/icmpmessagepayload.cpp +++ b/src/icmp/icmpmessagepayload.cpp @@ -1,5 +1,7 @@ #include "icmp/icmpmessagepayload.h" +#include + #include using namespace std; @@ -37,7 +39,11 @@ uint16_t IcmpMessagePayload::decode( const int right_byte ) const { - return ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; + int value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; + + BOOST_ASSERT( ( 0 <= value ) && ( value <= numeric_limits::max() ) ); + + return static_cast ( value ); } void IcmpMessagePayload::encode( diff --git a/src/icmp/ipv4header.cpp b/src/icmp/ipv4header.cpp index 1e9ddf4..205b364 100644 --- a/src/icmp/ipv4header.cpp +++ b/src/icmp/ipv4header.cpp @@ -1,5 +1,7 @@ #include "icmp/ipv4header.h" +#include + using namespace std; using boost::asio::ip::address_v4; @@ -21,7 +23,7 @@ uint8_t Ipv4Header::get_version() const uint16_t Ipv4Header::get_header_length() const { - return ( Payload[ 0 ] & 0xF ) * 4; + return static_cast ( (Payload[ 0 ] & 0xF) * 4 ); } uint8_t Ipv4Header::get_type_of_service() const @@ -124,5 +126,9 @@ istream &operator>>( uint16_t Ipv4Header::decode( int left_byte, int right_byte ) const { - return ( (Payload[ left_byte ] << 8) + Payload[ right_byte ] ); + int value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ]; + + BOOST_ASSERT( ( 0 <= value ) && ( value <= numeric_limits::max() ) ); + + return static_cast ( value ); } -- 1.7.1