]> 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]>
Mon, 15 Oct 2018 17:22:39 +0000 (10:22 -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.

(cherry picked from commit cde392ab16d1de6b88f6b70dca6ddd4b83d8b525)

print-tcp.c

index 316303769d641466495de9d18680c54207ff50ae..b80a2f264d456992a0ffd6c7b2bf68632fa65ffc 100644 (file)
@@ -267,11 +267,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];
@@ -318,11 +318,11 @@ tcp_print(netdissect_options *ndo,
                         if (rev) {
                                 UNALIGNED_MEMCPY(&tha.src, &ip->ip_dst, 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];