]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Cast dport and sport to u_int before shifting them.
authorGuy Harris <[email protected]>
Thu, 24 May 2018 19:11:09 +0000 (12:11 -0700)
committerGuy Harris <[email protected]>
Thu, 24 May 2018 19:11:09 +0000 (12:11 -0700)
The result of the expression is ultimately going to be put into a u_int;
cast them to u_int so that we'll be shifting unsigned values left rather
than int values, to avoid undefined behavior.

This should fix GitHub issue #681.

print-tcp.c

index 6e4815c05e6e412e3b3124cc8437241dc39577bc..d1c2c98403b6c41bb77ba97843fe547354d85c28 100644 (file)
@@ -275,11 +275,11 @@ tcp_print(netdissect_options *ndo,
                         if (rev) {
                                 UNALIGNED_MEMCPY(&tha.src, dst, sizeof(ip6->ip6_dst));
                                 UNALIGNED_MEMCPY(&tha.dst, src, sizeof(ip6->ip6_src));
-                                tha.port = dport << 16 | sport;
+                                tha.port = ((u_int)dport) << 16 | sport;
                         } else {
                                 UNALIGNED_MEMCPY(&tha.dst, dst, sizeof(ip6->ip6_dst));
                                 UNALIGNED_MEMCPY(&tha.src, src, sizeof(ip6->ip6_src));
-                                tha.port = sport << 16 | dport;
+                                tha.port = ((u_int)sport) << 16 | dport;
                         }
 
                         for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
@@ -331,13 +331,13 @@ tcp_print(netdissect_options *ndo,
                                                  sizeof(ip->ip_dst));
                                 UNALIGNED_MEMCPY(&tha.dst, ip->ip_src,
                                                  sizeof(ip->ip_src));
-                                tha.port = dport << 16 | sport;
+                                tha.port = ((u_int)dport) << 16 | sport;
                         } else {
                                 UNALIGNED_MEMCPY(&tha.dst, ip->ip_dst,
                                                  sizeof(ip->ip_dst));
                                 UNALIGNED_MEMCPY(&tha.src, ip->ip_src,
                                                  sizeof(ip->ip_src));
-                                tha.port = sport << 16 | dport;
+                                tha.port = ((u_int)sport) << 16 | dport;
                         }
 
                         for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];