Miroslav Lichvar a4f692
commit 25dcf01e340d85bcdbe7b3c24eac7fe1ce7ea0c2
Miroslav Lichvar a4f692
Author: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar a4f692
Date:   Wed Mar 10 17:05:55 2021 +0100
Miroslav Lichvar a4f692
Miroslav Lichvar a4f692
    Avoid unaligned pointers to packed members.
Miroslav Lichvar a4f692
    
Miroslav Lichvar a4f692
    This fixes "taking address of packed member ... may result in an
Miroslav Lichvar a4f692
    unaligned pointer value [-Waddress-of-packed-member]" warnings from gcc.
Miroslav Lichvar a4f692
    
Miroslav Lichvar a4f692
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar a4f692
Miroslav Lichvar a4f692
diff --git a/clock.c b/clock.c
Miroslav Lichvar a4f692
index 7005636..f88df58 100644
Miroslav Lichvar a4f692
--- a/clock.c
Miroslav Lichvar a4f692
+++ b/clock.c
Miroslav Lichvar a4f692
@@ -350,6 +350,7 @@ static int clock_management_fill_response(struct clock *c, struct port *p,
Miroslav Lichvar a4f692
 	struct time_status_np *tsn;
Miroslav Lichvar a4f692
 	struct tlv_extra *extra;
Miroslav Lichvar a4f692
 	struct PTPText *text;
Miroslav Lichvar a4f692
+	uint16_t duration;
Miroslav Lichvar a4f692
 	int datalen = 0;
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
 	extra = tlv_extra_alloc();
Miroslav Lichvar a4f692
@@ -452,7 +453,8 @@ static int clock_management_fill_response(struct clock *c, struct port *p,
Miroslav Lichvar a4f692
 			break;
Miroslav Lichvar a4f692
 		}
Miroslav Lichvar a4f692
 		sen = (struct subscribe_events_np *)tlv->data;
Miroslav Lichvar a4f692
-		clock_get_subscription(c, req, sen->bitmask, &sen->duration);
Miroslav Lichvar a4f692
+		clock_get_subscription(c, req, sen->bitmask, &duration);
Miroslav Lichvar a4f692
+		memcpy(&sen->duration, &duration, sizeof(sen->duration));
Miroslav Lichvar a4f692
 		datalen = sizeof(*sen);
Miroslav Lichvar a4f692
 		break;
Miroslav Lichvar a4f692
 	case TLV_SYNCHRONIZATION_UNCERTAIN_NP:
Miroslav Lichvar a4f692
diff --git a/msg.c b/msg.c
Miroslav Lichvar a4f692
index c4516ad..dcb397c 100644
Miroslav Lichvar a4f692
--- a/msg.c
Miroslav Lichvar a4f692
+++ b/msg.c
Miroslav Lichvar a4f692
@@ -19,6 +19,7 @@
Miroslav Lichvar a4f692
 #include <arpa/inet.h>
Miroslav Lichvar a4f692
 #include <errno.h>
Miroslav Lichvar a4f692
 #include <malloc.h>
Miroslav Lichvar a4f692
+#include <stdlib.h>
Miroslav Lichvar a4f692
 #include <string.h>
Miroslav Lichvar a4f692
 #include <time.h>
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
@@ -36,8 +37,8 @@ int assume_two_step = 0;
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
 struct message_storage {
Miroslav Lichvar a4f692
 	unsigned char reserved[MSG_HEADROOM];
Miroslav Lichvar a4f692
-	struct ptp_message msg;
Miroslav Lichvar a4f692
-} PACKED;
Miroslav Lichvar a4f692
+	struct ptp_message msg __attribute__((aligned (8)));
Miroslav Lichvar a4f692
+};
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
 static TAILQ_HEAD(msg_pool, ptp_message) msg_pool = TAILQ_HEAD_INITIALIZER(msg_pool);
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
diff --git a/tlv.c b/tlv.c
Miroslav Lichvar a4f692
index 879bb7e..98ef6e1 100644
Miroslav Lichvar a4f692
--- a/tlv.c
Miroslav Lichvar a4f692
+++ b/tlv.c
Miroslav Lichvar a4f692
@@ -67,7 +67,7 @@ static void timestamp_net2host(struct Timestamp *t)
Miroslav Lichvar a4f692
 	NTOHL(t->nanoseconds);
Miroslav Lichvar a4f692
 }
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
-static uint16_t flip16(uint16_t *p)
Miroslav Lichvar a4f692
+static uint16_t flip16(void *p)
Miroslav Lichvar a4f692
 {
Miroslav Lichvar a4f692
 	uint16_t v;
Miroslav Lichvar a4f692
 	memcpy(&v, p, sizeof(v));
Miroslav Lichvar a4f692
@@ -76,7 +76,7 @@ static uint16_t flip16(uint16_t *p)
Miroslav Lichvar a4f692
 	return v;
Miroslav Lichvar a4f692
 }
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
-static int64_t host2net64_unaligned(int64_t *p)
Miroslav Lichvar a4f692
+static int64_t host2net64_unaligned(void *p)
Miroslav Lichvar a4f692
 {
Miroslav Lichvar a4f692
 	int64_t v;
Miroslav Lichvar a4f692
 	memcpy(&v, p, sizeof(v));
Miroslav Lichvar a4f692
@@ -85,7 +85,7 @@ static int64_t host2net64_unaligned(int64_t *p)
Miroslav Lichvar a4f692
 	return v;
Miroslav Lichvar a4f692
 }
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
-static int64_t net2host64_unaligned(int64_t *p)
Miroslav Lichvar a4f692
+static int64_t net2host64_unaligned(void *p)
Miroslav Lichvar a4f692
 {
Miroslav Lichvar a4f692
 	int64_t v;
Miroslav Lichvar a4f692
 	memcpy(&v, p, sizeof(v));
Miroslav Lichvar a4f692
diff --git a/util.h b/util.h
Miroslav Lichvar a4f692
index 41e33d4..739c8fd 100644
Miroslav Lichvar a4f692
--- a/util.h
Miroslav Lichvar a4f692
+++ b/util.h
Miroslav Lichvar a4f692
@@ -57,7 +57,7 @@ const char *ts_str(enum timestamp_type ts);
Miroslav Lichvar a4f692
  */
Miroslav Lichvar a4f692
 int addreq(enum transport_type type, struct address *a, struct address *b);
Miroslav Lichvar a4f692
 
Miroslav Lichvar a4f692
-static inline uint16_t align16(uint16_t *p)
Miroslav Lichvar a4f692
+static inline uint16_t align16(void *p)
Miroslav Lichvar a4f692
 {
Miroslav Lichvar a4f692
 	uint16_t v;
Miroslav Lichvar a4f692
 	memcpy(&v, p, sizeof(v));