]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Revert print-tcp memcpy() changes, and use unaligned_memcpy() instead.
authorGuy Harris <[email protected]>
Thu, 16 Jan 2014 03:08:07 +0000 (19:08 -0800)
committerGuy Harris <[email protected]>
Thu, 16 Jan 2014 03:08:07 +0000 (19:08 -0800)
That should prevent optimizing the memcpy into code that assumes
alignment.

Add unaligned_memcmp(), and use it, as well.

interface.h
netdissect.h
print-tcp.c
util.c

index 8ed4a4a8f747ff2bc40150e20d8b82d86612ccaf..0ac231ce55810a466072314d5d4d7b167651d849 100644 (file)
@@ -159,6 +159,7 @@ extern void safeputchar(int);
 extern void safeputs(const char *, int);
 
 extern void unaligned_memcpy(void *, const void *, size_t);
+extern int unaligned_memcmp(void *, const void *, size_t);
 
 extern const char *isonsap_string(const u_char *, register u_int);
 extern const char *protoid_string(const u_char *);
index 0b194c95661753702e00fb94a6d1f53f529477c1..af535de3caff345283df3f2836d26baeb67a89d7 100644 (file)
@@ -271,6 +271,7 @@ extern void safeputchar(int);
 extern void safeputs(const char *, int);
 
 extern void unaligned_memcpy(void *, const void *, size_t);
+extern int unaligned_memcmp(void *, const void *, size_t);
 
 #define PLURAL_SUFFIX(n) \
        (((n) != 1) ? "s" : "")
index b63bc4ebf21fd53303ff74e0f5b32e2fd766bad4..658032e5faac7dfce6cf35125bbb6ebf72cba7f5 100644 (file)
@@ -305,31 +305,25 @@ tcp_print(register const u_char *bp, register u_int length,
 #endif /*INET6*/
                         register struct tcp_seq_hash *th;
                         struct tcp_seq_hash *tcp_seq_hash;
-                        const void *src, *dst;
+                        const struct in_addr *src, *dst;
                         struct tha tha;
 
                         tcp_seq_hash = tcp_seq_hash4;
-                       /*
-                        * We use "void *" to keep the compiler from
-                        * assuming the structures are aligned and
-                        * generating non-unaligned-safe code for
-                        * the copies.
-                        */
-                        src = (const void *)&ip->ip_src;
-                        dst = (const void *)&ip->ip_dst;
+                        src = &ip->ip_src;
+                        dst = &ip->ip_dst;
                         if (sport > dport)
                                 rev = 1;
                         else if (sport == dport) {
-                                if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
+                                if (unaligned_memcmp(src, dst, sizeof ip->ip_dst) > 0)
                                         rev = 1;
                         }
                         if (rev) {
-                                memcpy(&tha.src, dst, sizeof ip->ip_dst);
-                                memcpy(&tha.dst, src, sizeof ip->ip_src);
+                                unaligned_memcpy(&tha.src, dst, sizeof ip->ip_dst);
+                                unaligned_memcpy(&tha.dst, src, sizeof ip->ip_src);
                                 tha.port = dport << 16 | sport;
                         } else {
-                                memcpy(&tha.dst, dst, sizeof ip->ip_dst);
-                                memcpy(&tha.src, src, sizeof ip->ip_src);
+                                unaligned_memcpy(&tha.dst, dst, sizeof ip->ip_dst);
+                                unaligned_memcpy(&tha.src, src, sizeof ip->ip_src);
                                 tha.port = sport << 16 | dport;
                         }
 
diff --git a/util.c b/util.c
index f93ed43767340c8bd00643d6955fcec9ddef9a5f..f243390b2cf742d04df3b80c321c5bc03930e739 100644 (file)
--- a/util.c
+++ b/util.c
@@ -612,3 +612,11 @@ unaligned_memcpy(void *p, const void *q, size_t l)
 {
        memcpy(p, q, l);
 }
+
+/* As with memcpy(), so with memcmp(). */
+int
+unaligned_memcmp(void *p, const void *q, size_t l)
+{
+       return (memcmp(p, q, l));
+}
+