tcpheader: Fix BOOST_ASSERT failure in calculate_checksum
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Sat, 27 Dec 2025 17:28:09 +0000 (18:28 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Sat, 27 Dec 2025 17:28:09 +0000 (18:28 +0100)
The assertion (sizeof(current_size) / 2) >= sizeof(size) was failing on
64-bit systems because:
- current_size was int64_t (8 bytes), so sizeof(current_size)/2 = 4
- size was size_t (8 bytes), so sizeof(size) = 8
- 4 >= 8 is false

Fix by:
- Using size_t for current_size to match the size parameter type
- Simplifying the assertion to check that current_size can hold size
- Simplifying the size decrement since both are now size_t

Test: test_tcpheader/calculate_tcp_checksum_ipv4 now passes

src/tcp/tcpheader.cpp

index 5eedee5..bef7888 100644 (file)
@@ -551,14 +551,14 @@ uint16_t TcpHeader::calculate_checksum(
 ) const
 {
     uint64_t cksum = 0;
-    int64_t current_size = static_cast<size_t>( size );
+    size_t current_size = size;
 
-    BOOST_ASSERT( (sizeof(current_size) / 2) >= sizeof(size) );
+    BOOST_ASSERT( sizeof(current_size) >= sizeof(size) );
 
     while ( current_size > 1 )
     {
         cksum += *word_array++;
-        current_size -= static_cast<int64_t>( sizeof(word_array[ 0 ]) );
+        current_size -= sizeof(word_array[ 0 ]);
     }
     if ( current_size > 0 )
     {