From 8531292e3e13f713188187e21565b81669c9e0c7 Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Mon, 1 Jan 2018 20:18:38 +0100 Subject: [PATCH] Fix strict aliasing violations Type-punning causes unaligned pointers, and those cause crashes on some processors, e.g. sparc64. deutex.c: In function 'COMhelp': deutex.c:1130:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] width1 = *((short *) &d->exec) + OPTINDENT; deutex.c:1131:13: warning: width2 = *((short *) &d->use); deutex.c: In function 'opt_widths': deutex.c:1236:17: warning: *((short *) ¤t_section->com) = (short) width2r; deutex.c:1237:17: warning: if (*((short *) ¤t_section->com) != width2r) deutex.c:1239:21: warning: *((short *) ¤t_section->com) = SHRT_MAX; deutex.c:1241:17: warning: *((short *) ¤t_section->exec) = (short) width1t; deutex.c:1242:17: warning: if (*((short *) ¤t_section->exec) != width1t) deutex.c:1244:21: warning: *((short *) ¤t_section->exec) = SHRT_MAX; deutex.c:1246:17: warning: *((short *) ¤t_section->use) = (short) width2t; deutex.c:1247:17: warning: if (*((short *) ¤t_section->use) != width2t) deutex.c:1249:21: warning: *((short *) ¤t_section->use) = SHRT_MAX; --- src/deutex.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/deutex.c b/src/deutex.c index 15ff8c3..f52608c 100644 --- a/src/deutex.c +++ b/src/deutex.c @@ -1124,11 +1124,14 @@ void COMhelp(int argc, const char *argv[]) /* Do a first pass on all the options for this section. Find out how wide the left and right columns need to be. */ if (d->type == SEC) { + uint16_t tmp; if (section++) putchar('\n'); printf("%s:\n", d->help); - width1 = *((short *) &d->exec) + OPTINDENT; - width2 = *((short *) &d->use); + memcpy(&tmp, &d->exec, sizeof(tmp)); + width1 = tmp + OPTINDENT; + memcpy(&tmp, &d->use, sizeof(tmp)); + width2 = tmp; if (width1 + 1 + width2 > TTYCOL) width1 = TTYCOL - width2 - COLSPACING; } @@ -1229,24 +1232,26 @@ static void opt_widths() - exec = maximum text width of the first column, - use = maximum text width of second column. */ if (current_section != NULL) { + uint16_t tmp; current_section->argc = (char) width1r; if (current_section->argc != width1r) current_section->argc = CHAR_MAX; /* Can't happen */ - *((short *) ¤t_section->com) = (short) width2r; - if (*((short *) ¤t_section->com) != width2r) + tmp = width2r; + if (tmp != width2r) /* Can't happen */ - *((short *) ¤t_section->com) = SHRT_MAX; + tmp = SHRT_MAX; + memcpy(¤t_section->com, &tmp, sizeof(tmp)); - *((short *) ¤t_section->exec) = (short) width1t; - if (*((short *) ¤t_section->exec) != width1t) - /* Can't happen */ - *((short *) ¤t_section->exec) = SHRT_MAX; + tmp = width1t; + if (tmp != width1t) + tmp = SHRT_MAX; + memcpy(¤t_section->exec, &tmp, sizeof(tmp)); - *((short *) ¤t_section->use) = (short) width2t; - if (*((short *) ¤t_section->use) != width2t) - /* Can't happen */ - *((short *) ¤t_section->use) = SHRT_MAX; + tmp = width2t; + if (tmp != width2t) + tmp = SHRT_MAX; + memcpy(¤t_section->use, &tmp, sizeof(tmp)); } } -- 2.15.1