From da3d144a59c13a09c68d2d7f34de529a003eee4b Mon Sep 17 00:00:00 2001 From: jilles Date: Tue, 3 Jul 2018 19:09:46 +0000 Subject: [PATCH] rup: Fix -Wcast-align warnings Fix possible strict aliasing issue (if time_t is the same size as int but not int but for example long) which also resulted in a false positive warning on systems with 64-bit time_t. Pointer casts are bad; we can just copy the time_t. Elsewhere, avoid casting char * to int * by using memcpy(). Reviewed by: eadler Differential Revision: https://reviews.freebsd.org/D16075 --- usr.bin/rup/Makefile | 2 -- usr.bin/rup/rup.c | 27 ++++++++------------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/usr.bin/rup/Makefile b/usr.bin/rup/Makefile index 4df0528284f8..cd3ab112fa81 100644 --- a/usr.bin/rup/Makefile +++ b/usr.bin/rup/Makefile @@ -4,6 +4,4 @@ PROG= rup LIBADD= rpcsvc -NO_WCAST_ALIGN= # Size is explicitly handled - .include diff --git a/usr.bin/rup/rup.c b/usr.bin/rup/rup.c index c0cd5f8243ae..aae3639b64d4 100644 --- a/usr.bin/rup/rup.c +++ b/usr.bin/rup/rup.c @@ -120,26 +120,15 @@ rstat_reply(statstime *host_stat, struct sockaddr_in *raddrp) printf("%-*s\t", HOST_WIDTH, host); - if (sizeof(time_t) == sizeof(host_stat->curtime.tv_sec)) { - tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec); - host_time = *tmp_time; + tmp_time_t = host_stat->curtime.tv_sec; + tmp_time = localtime(&tmp_time_t); + host_time = *tmp_time; - host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec; + host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec; - tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec); - host_uptime = *tmp_time; - } - else { /* non-32-bit time_t */ - tmp_time_t = host_stat->curtime.tv_sec; - tmp_time = localtime(&tmp_time_t); - host_time = *tmp_time; - - host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec; - - tmp_time_t = host_stat->curtime.tv_sec; - tmp_time = gmtime(&tmp_time_t); - host_uptime = *tmp_time; - } + tmp_time_t = host_stat->curtime.tv_sec; + tmp_time = gmtime(&tmp_time_t); + host_uptime = *tmp_time; #define updays (host_stat->curtime.tv_sec / 86400) if (host_uptime.tm_yday != 0) @@ -205,7 +194,7 @@ onehost(char *host) return(-1); } - addr.sin_addr.s_addr = *(int *)hp->h_addr; + memcpy(&addr.sin_addr.s_addr, hp->h_addr, sizeof(int)); rstat_reply(&host_stat, &addr); clnt_destroy(rstat_clnt); return (0);