Netdev List
 help / color / mirror / Atom feed
From: Mike Frysinger <vapier@gentoo.org>
To: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Cc: netdev@vger.kernel.org
Subject: [PATCH 2/3] fix up strict-aliasing warnings
Date: Sat, 19 Feb 2011 12:51:28 -0500	[thread overview]
Message-ID: <1298137889-23969-2-git-send-email-vapier@gentoo.org> (raw)
In-Reply-To: <1298137889-23969-1-git-send-email-vapier@gentoo.org>

Current build of some tools results in gcc warning about strict-aliasing
violations.  So change those freaky casts to memcpy's.  When the pointer
types work out, gcc will optimize this away anyways.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
 ping6.c      |   13 +++++++++----
 tracepath.c  |    2 +-
 tracepath6.c |    2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/ping6.c b/ping6.c
index c5ff881..86f9216 100644
--- a/ping6.c
+++ b/ping6.c
@@ -1104,18 +1104,21 @@ int build_niquery(__u8 *_nih)
 {
 	struct ni_hdr *nih;
 	int cc;
+	__u16 this_nonce;
 
 	nih = (struct ni_hdr *)_nih;
 	nih->ni_cksum = 0;
 
-	CLR(ntohs((*(__u16*)(nih->ni_nonce))) % mx_dup_ck);
+	memcpy(&this_nonce, &nih->ni_nonce, sizeof(this_nonce));
+	CLR(ntohs(this_nonce) % mx_dup_ck);
 
 	nih->ni_type = ICMPV6_NI_QUERY;
 	cc = sizeof(*nih);
 	datalen = 0;
 
 	memcpy(nih->ni_nonce, ni_nonce, sizeof(nih->ni_nonce));
-	*(__u16*)(nih->ni_nonce) = htons(ntransmitted + 1);
+	this_nonce = htons(ntransmitted + 1);
+	memcpy(&nih->ni_nonce, &this_nonce, sizeof(this_nonce));
 
 	nih->ni_code = ni_subject_type;
 	nih->ni_qtype = htons(ni_query);
@@ -1331,7 +1334,7 @@ parse_reply(struct msghdr *msg, int cc, void *addr, struct timeval *tv)
 #endif
 			if (c->cmsg_len < CMSG_LEN(sizeof(int)))
 				continue;
-			hops = *(int*)CMSG_DATA(c);
+			memcpy(&hops, CMSG_DATA(c), sizeof(int));
 		}
 	}
 
@@ -1355,7 +1358,9 @@ parse_reply(struct msghdr *msg, int cc, void *addr, struct timeval *tv)
 			return 0;
 	} else if (icmph->icmp6_type == ICMPV6_NI_REPLY) {
 		struct ni_hdr *nih = (struct ni_hdr *)icmph;
-		__u16 seq = ntohs(*(__u16 *)nih->ni_nonce);
+		__u16 seq;
+		memcpy(&seq, &nih->ni_nonce, sizeof(seq));
+		seq = ntohs(seq);
 		if (memcmp(&nih->ni_nonce[2], &ni_nonce[2], sizeof(ni_nonce) - sizeof(__u16)))
 			return 1;
 		if (gather_statistics((__u8*)icmph, sizeof(*icmph), cc,
diff --git a/tracepath.c b/tracepath.c
index ca84a69..0a14b1b 100644
--- a/tracepath.c
+++ b/tracepath.c
@@ -142,7 +142,7 @@ restart:
 			if (cmsg->cmsg_type == IP_RECVERR) {
 				e = (struct sock_extended_err *) CMSG_DATA(cmsg);
 			} else if (cmsg->cmsg_type == IP_TTL) {
-				rethops = *(int*)CMSG_DATA(cmsg);
+				memcpy(&rethops, CMSG_DATA(cmsg), sizeof(int));
 			} else {
 				printf("cmsg:%d\n ", cmsg->cmsg_type);
 			}
diff --git a/tracepath6.c b/tracepath6.c
index 5c2db8f..77a3563 100644
--- a/tracepath6.c
+++ b/tracepath6.c
@@ -170,7 +170,7 @@ restart:
 #ifdef IPV6_2292HOPLIMIT
 			case IPV6_2292HOPLIMIT:
 #endif
-				rethops = *(int*)CMSG_DATA(cmsg);
+				memcpy(&rethops, CMSG_DATA(cmsg), sizeof(int));
 				break;
 			default:
 				printf("cmsg6:%d\n ", cmsg->cmsg_type);
-- 
1.7.4.1


  reply	other threads:[~2011-02-19 17:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-19 17:51 [PATCH 1/3] tracepath: re-use printf return in print_host Mike Frysinger
2011-02-19 17:51 ` Mike Frysinger [this message]
2011-02-19 17:51 ` [PATCH 3/3] tracepath: fix typo in -l error message Mike Frysinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1298137889-23969-2-git-send-email-vapier@gentoo.org \
    --to=vapier@gentoo.org \
    --cc=netdev@vger.kernel.org \
    --cc=yoshfuji@linux-ipv6.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox