From: Guilherme Maciel Ferreira Date: Thu, 22 Dec 2011 09:42:24 +0000 (-0200) Subject: PC-Lint warnings fixed: X-Git-Tag: v1.3~27 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=aaaa3b6243154d3dc690b388f23005c256402098;p=pingcheck PC-Lint warnings fixed: - Info 1786: Implicit conversion to Boolean (return) (int to bool); - Info 1705: static class members may be accessed by the scoping operator; - Info 713: Loss of precision (arg. no. 2) (unsigned int to int); - Info 737: Loss of sign in promotion from int to unsigned int in statement 'size -= sizeof(word_array[ 0 ])'; - Info 713: Loss of precision (assignment) (unsigned int to int) in statement 'size -= sizeof(word_array[ 0 ])'; - Info 1773: Attempt to cast away const (or volatile) in statement 'cksum += *(uint8_t*) word_array'; - Replaced all C-style casts by C++ style casts. --- diff --git a/src/tcp/tcpheader.cpp b/src/tcp/tcpheader.cpp index a1984d7..55aae2b 100644 --- a/src/tcp/tcpheader.cpp +++ b/src/tcp/tcpheader.cpp @@ -26,6 +26,7 @@ on this file might be covered by the GNU General Public License. #include #include +#include #include "ip/pseudoipv4header.h" #include "ip/pseudoipv6header.h" @@ -72,42 +73,42 @@ uint8_t TcpHeader::get_header_length() const bool TcpHeader::get_congestion_window_reduced() const { - return ( Payload[13] & 0x80 ); + return static_cast( Payload[13] & 0x80 ); } bool TcpHeader::get_ecn_echo() const { - return ( Payload[13] & 0x40 ); + return static_cast( Payload[13] & 0x40 ); } bool TcpHeader::get_urgent() const { - return ( Payload[13] & 0x20 ); + return static_cast( Payload[13] & 0x20 ); } bool TcpHeader::get_acknowledgment() const { - return ( Payload[13] & 0x10 ); + return static_cast( Payload[13] & 0x10 ); } bool TcpHeader::get_push() const { - return ( Payload[13] & 0x08 ); + return static_cast( Payload[13] & 0x08 ); } bool TcpHeader::get_reset() const { - return ( Payload[13] & 0x04 ); + return static_cast( Payload[13] & 0x04 ); } bool TcpHeader::get_synchronize() const { - return ( Payload[13] & 0x02 ); + return static_cast( Payload[13] & 0x02 ); } bool TcpHeader::get_finish() const { - return ( Payload[13] & 0x01 ); + return static_cast( Payload[13] & 0x01 ); } uint16_t TcpHeader::get_window_size() const @@ -132,8 +133,10 @@ uint16_t TcpHeader::calculate_tcp_checksum( ); PseudoIpv4Header pseudo_header; - BOOST_ASSERT( src_addr.size() == sizeof(pseudo_header.source_address) ); - BOOST_ASSERT( dest_addr.size() == sizeof(pseudo_header.destination_address) ); + + BOOST_ASSERT( address_v4::bytes_type::size() == sizeof(pseudo_header.source_address) ); + BOOST_ASSERT( address_v4::bytes_type::size() == sizeof(pseudo_header.destination_address) ); + // Byte ordering (i.e. endianness) is handle by Boost.Asio copy( src_addr.begin(), src_addr.end(), pseudo_header.source_address ); copy( dest_addr.begin(), dest_addr.end(), pseudo_header.destination_address ); @@ -180,8 +183,10 @@ uint16_t TcpHeader::calculate_tcp_checksum( ); PseudoIpv6Header pseudo_header; - BOOST_ASSERT( src_addr.size() == sizeof(pseudo_header.source_address) ); - BOOST_ASSERT( dest_addr.size() == sizeof(pseudo_header.destination_address) ); + + BOOST_ASSERT( address_v6::bytes_type::size() == sizeof(pseudo_header.source_address) ); + BOOST_ASSERT( address_v6::bytes_type::size() == sizeof(pseudo_header.destination_address) ); + copy( src_addr.begin(), src_addr.end(), pseudo_header.source_address ); copy( dest_addr.begin(), dest_addr.end(), pseudo_header.destination_address ); fill_n( pseudo_header.zero_byte, 3, 0 ); @@ -310,21 +315,28 @@ ostream& operator<<( ostream& os, const TcpHeader& header ) return header.Payload.write( os ); } -uint16_t TcpHeader::calculate_checksum( const uint16_t *word_array, int size ) const +uint16_t TcpHeader::calculate_checksum( + const uint16_t *word_array, + const size_t size +) const { - unsigned long cksum = 0; - while ( size > 1 ) + uint64_t cksum = 0; + int64_t current_size = static_cast( size ); + + BOOST_STATIC_ASSERT( (sizeof(current_size) / 2) >= sizeof(size) ); + + while ( current_size > 1 ) { cksum += *word_array++; - size -= sizeof(word_array[ 0 ]); + current_size -= static_cast( sizeof(word_array[ 0 ]) ); } - if ( size ) + if ( current_size > 0 ) { - cksum += *(uint8_t*) word_array; + cksum += *(reinterpret_cast( word_array )); } cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); - return (uint16_t) (~cksum); + return static_cast( ~cksum ); } diff --git a/src/tcp/tcpheader.h b/src/tcp/tcpheader.h index 7387d05..330c7f7 100644 --- a/src/tcp/tcpheader.h +++ b/src/tcp/tcpheader.h @@ -130,7 +130,10 @@ public: ); private: - uint16_t calculate_checksum( const uint16_t *word_array, int size ) const; + uint16_t calculate_checksum( + const uint16_t *word_array, + const std::size_t size + ) const; MessagePayload Payload; };