PC-Lint warnings fixed:
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Thu, 22 Dec 2011 09:42:24 +0000 (07:42 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Thu, 22 Dec 2011 09:46:19 +0000 (07:46 -0200)
- 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.

src/tcp/tcpheader.cpp
src/tcp/tcpheader.h

index a1984d7..55aae2b 100644 (file)
@@ -26,6 +26,7 @@ on this file might be covered by the GNU General Public License.
 #include <algorithm>
 
 #include <boost/assert.hpp>
+#include <boost/static_assert.hpp>
 
 #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<bool>( Payload[13] & 0x80 );
 }
 
 bool TcpHeader::get_ecn_echo() const
 {
-    return ( Payload[13] & 0x40 );
+    return static_cast<bool>( Payload[13] & 0x40 );
 }
 
 bool TcpHeader::get_urgent() const
 {
-    return ( Payload[13] & 0x20 );
+    return static_cast<bool>( Payload[13] & 0x20 );
 }
 
 bool TcpHeader::get_acknowledgment() const
 {
-    return ( Payload[13] & 0x10 );
+    return static_cast<bool>( Payload[13] & 0x10 );
 }
 
 bool TcpHeader::get_push() const
 {
-    return ( Payload[13] & 0x08 );
+    return static_cast<bool>( Payload[13] & 0x08 );
 }
 
 bool TcpHeader::get_reset() const
 {
-    return ( Payload[13] & 0x04 );
+    return static_cast<bool>( Payload[13] & 0x04 );
 }
 
 bool TcpHeader::get_synchronize() const
 {
-    return ( Payload[13] & 0x02 );
+    return static_cast<bool>( Payload[13] & 0x02 );
 }
 
 bool TcpHeader::get_finish() const
 {
-    return ( Payload[13] & 0x01 );
+    return static_cast<bool>( 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_t>( 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<int64_t>( sizeof(word_array[ 0 ]) );
     }
-    if ( size )
+    if ( current_size > 0 )
     {
-        cksum += *(uint8_t*) word_array;
+        cksum += *(reinterpret_cast<const uint8_t *>( word_array ));
     }
 
     cksum = (cksum >> 16) + (cksum & 0xffff);
     cksum += (cksum >> 16);
 
-    return (uint16_t) (~cksum);
+    return static_cast<uint16_t>( ~cksum );
 }
index 7387d05..330c7f7 100644 (file)
@@ -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;
 };