From 4c8fb05cb4d6ddcd67299008db54af423f86fd05 Mon Sep 17 00:00:00 2001 From: Oliver Hartkopp Date: Sat, 1 Jul 2017 19:01:01 +0200 Subject: [PATCH] can-utils: remove strict-aliasing compiler warning GitHub user 'crossband' raised an issue regarding the strict-aliasing compiler warning in his specific setup: https://github.com/linux-can/can-utils/issues/42 In fact memcpy() and memset() are a better solution than the former pointer magics, so remove the issues and the compiler warning flag too. Reported-by: crossband (https://github.com/crossband) Signed-off-by: Oliver Hartkopp --- Makefile | 3 +-- candump.c | 4 ++-- cangen.c | 8 +++++--- cansniffer.c | 28 +++++++++++++++------------- slcanpty.c | 2 +- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index ee94340..67ddf8c 100644 --- a/Makefile +++ b/Makefile @@ -43,8 +43,7 @@ PREFIX ?= /usr/local MAKEFLAGS = -k -CFLAGS = -O2 -Wall -Wno-parentheses \ - -fno-strict-aliasing +CFLAGS = -O2 -Wall -Wno-parentheses CPPFLAGS += -Iinclude \ -D_FILE_OFFSET_BITS=64 \ diff --git a/candump.c b/candump.c index f328c66..d707bd9 100644 --- a/candump.c +++ b/candump.c @@ -689,9 +689,9 @@ int main(int argc, char **argv) cmsg && (cmsg->cmsg_level == SOL_SOCKET); cmsg = CMSG_NXTHDR(&msg,cmsg)) { if (cmsg->cmsg_type == SO_TIMESTAMP) - tv = *(struct timeval *)CMSG_DATA(cmsg); + memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv)); else if (cmsg->cmsg_type == SO_RXQ_OVFL) - dropcnt[i] = *(__u32 *)CMSG_DATA(cmsg); + memcpy(&dropcnt[i], CMSG_DATA(cmsg), sizeof(__u32)); } /* check for (unlikely) dropped frames on this specific socket */ diff --git a/cangen.c b/cangen.c index bd22b7d..769fac2 100644 --- a/cangen.c +++ b/cangen.c @@ -150,6 +150,7 @@ int main(int argc, char **argv) int mtu, maxdlen; uint64_t incdata = 0; int incdlc = 0; + unsigned long rnd; unsigned char fixdata[CANFD_MAX_DLEN]; int opt; @@ -400,9 +401,10 @@ int main(int argc, char **argv) if (data_mode == MODE_RANDOM) { - /* that's what the 64 bit alignment of data[] is for ... :) */ - *(unsigned long*)(&frame.data[0]) = random(); - *(unsigned long*)(&frame.data[4]) = random(); + rnd = random(); + memcpy(&frame.data[0], &rnd, 4); + rnd = random(); + memcpy(&frame.data[4], &rnd, 4); /* omit extra random number generation for CAN FD */ if (canfd && frame.len > 8) { diff --git a/cansniffer.c b/cansniffer.c index 39b3e2b..7b0a3fa 100644 --- a/cansniffer.c +++ b/cansniffer.c @@ -64,8 +64,6 @@ #include "terminal.h" -#define U64_DATA(p) (*(unsigned long long*)(p)->data) - #define SETFNAME "sniffset." #define ANYDEV "any" @@ -370,7 +368,9 @@ void rx_setup (int fd, int id){ txmsg.msg_head.ival2.tv_sec = 0; txmsg.msg_head.ival2.tv_usec = 0; txmsg.msg_head.nframes = 1; - U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL; + + /* set all bits to be relevant */ + memset(&txmsg.frame.data, 0xFF, 8); if (filter_id_only) txmsg.msg_head.flags |= RX_FILTER_ID; @@ -478,7 +478,7 @@ int handle_keyb(int fd){ case '*' : for (i=0; i < 2048; i++) - U64_DATA(&sniftab[i].notch) = (__u64) 0; + memset(&sniftab[i].notch.data, 0, 8); break; default: @@ -492,7 +492,7 @@ int handle_keyb(int fd){ int handle_bcm(int fd, long currcms){ - int nbytes, id; + int nbytes, id, i; struct { struct bcm_msg_head msg_head; @@ -518,8 +518,9 @@ int handle_bcm(int fd, long currcms){ } sniftab[id].current = bmsg.frame; - U64_DATA(&sniftab[id].marker) |= - U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last); + for (i=0; i < 8; i++) + sniftab[id].marker.data[i] |= sniftab[id].current.data[i] ^ sniftab[id].last.data[i]; + sniftab[id].timeout = (timeout)?(currcms + timeout):0; if (is_clr(id, DISPLAY)) @@ -533,7 +534,7 @@ int handle_bcm(int fd, long currcms){ int handle_timeo(int fd, long currcms){ - int i; + int i, j; int force_redraw = 0; static unsigned int frame_count; @@ -547,8 +548,10 @@ int handle_timeo(int fd, long currcms){ } if (notch) { - for (i=0; i < 2048; i++) - U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker); + for (i=0; i < 2048; i++) { + for (j=0; j < 8; j++) + sniftab[i].notch.data[j] |= sniftab[i].marker.data[j]; + } notch = 0; } @@ -569,7 +572,7 @@ int handle_timeo(int fd, long currcms){ } else if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) { - U64_DATA(&sniftab[i].marker) = (__u64) 0; + memset(&sniftab[i].marker.data, 0, 8); print_snifline(i); sniftab[i].hold = 0; /* disable update by hold */ } @@ -670,8 +673,7 @@ void print_snifline(int id){ putchar('\n'); - U64_DATA(&sniftab[id].marker) = (__u64) 0; - + memset(&sniftab[id].marker.data, 0, 8); }; diff --git a/slcanpty.c b/slcanpty.c index 5cf14ef..431ca68 100644 --- a/slcanpty.c +++ b/slcanpty.c @@ -246,7 +246,7 @@ rx_restart: else ptr = 9; /* dlc position Tiiiiiiiid */ - *(unsigned long long *) (&frame.data) = 0ULL; /* clear data[] */ + memset(&frame.data, 0, 8); /* clear data[] */ if ((cmd | 0x20) == 'r' && buf[ptr] != '0') {