Re: [PATCH 1/1] Fix first day of the week calculation on big endian, 64 bit systems

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Feb 10, 2010 at 03:28:17PM +0000, Joseph Jezak wrote:
> This patch fixes commit dcb54fafb128ab41772ae217afc6a7612e2cc446 which is broken
> on big endian, 64 bit machines. The data is stored in the upper 4 bytes of the
> pointer on these machines, but the code was reading the lower 4 bytes. This
> resulted in the first day of the week being reported incorrectly.

 Good catch. Thanks!

>  	{
> -		int wfd = (int)(intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);
> +		char *wfd_ptr = nl_langinfo(_NL_TIME_WEEK_1STDAY);
> +		int *wfd_int_ptr = (int *)(&wfd_ptr);

 At first glance it seems usable, but this cast is against strict-aliasing
 rules (try gcc -Wall).

 I have reverted the commit dcb54fafb128ab41772ae217afc6a7612e2cc446,
 it seems that the original solution (based on union) is portable. See
 the patch below.

    Karel


>From aebb9522b78ded9cc00475b2dfc98813a0cbf202 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@xxxxxxxxxx>
Date: Thu, 11 Feb 2010 16:29:05 +0100
Subject: [PATCH] cal: fix first day of the week calculation on BE systems

This reverts commit dcb54fafb128ab41772ae217afc6a7612e2cc446,
"cal: remove gcc-ism from nl_langinfo() call".

The code:

	int wfd = (int)(intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);

does not work on big-endian machines. The original solution based on
union is better.

Note that the "type punning" is not gcc-ism any more, it's allowed
by C99 (6.5.2.3).

Reported-by: Joseph Jezak <josejx@xxxxxxxxxx>
Signed-off-by: Karel Zak <kzak@xxxxxxxxxx>
---
 misc-utils/cal.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 5444cea..5eb14b5 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -312,7 +312,11 @@ main(int argc, char **argv) {
 	 POSIX:  19971201 + 7 -1 = 0
 	 */
 	{
-		int wfd = (int)(intptr_t) nl_langinfo(_NL_TIME_WEEK_1STDAY);
+		int wfd;
+		union { unsigned int word; char *string; } val;
+		val.string = nl_langinfo(_NL_TIME_WEEK_1STDAY);
+
+		wfd = val.word;
 		wfd = day_in_week(wfd % 100, (wfd / 100) % 100, wfd / (100 * 100));
 		weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % 7;
 	}
-- 
1.6.6

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux