From 6371df556ca7d5fa52947cb5fbe414bbfb5a0e4d Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Mon, 20 Feb 2012 07:54:04 -0700 Subject: [PATCH] yet anther attempt to work around the Mac 10.7 localtime() bug I now think the problem is likely to be realted to values that do not fit into a signed 32-bit integer. Check for the OS version and reject such integers. --- src/racket/sconfig.h | 5 ----- src/racket/src/fun.c | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/racket/sconfig.h b/src/racket/sconfig.h index b8b21bfb5b..b84942b028 100644 --- a/src/racket/sconfig.h +++ b/src/racket/sconfig.h @@ -768,11 +768,6 @@ #endif # define MZ_JIT_USE_MPROTECT -#if defined(__x86_64__) -/* work around a bug in localtime() in 10.6.8 */ -# define MIN_VALID_DATE_SECONDS -67768040609715600 -#endif - # define FLAGS_ALREADY_SET #endif diff --git a/src/racket/src/fun.c b/src/racket/src/fun.c index 77877f45f2..8c83515bf3 100644 --- a/src/racket/src/fun.c +++ b/src/racket/src/fun.c @@ -8320,10 +8320,44 @@ static int month_offsets[12] = { 0, 31, 59, 90, 243, 273, 304, 334 }; #endif -#ifdef MIN_VALID_DATE_SECONDS -# define VALID_TIME_RANGE(x) ((x) >= MIN_VALID_DATE_SECONDS) +#if (defined(OS_X) || defined(XONX)) && defined(__x86_64__) +/* work around a bug in localtime() in 10.6.8 */ +# include +# include +static int VALID_TIME_RANGE(UNBUNDLE_TIME_TYPE lnow) +{ + /* Fits in 32 bits? */ + int ilnow = (int)lnow; + if (lnow == (UNBUNDLE_TIME_TYPE)ilnow) + return 1; + + /* 10.7 or later? */ + { + int a[2]; + size_t len; + char *vers; + + a[0] = CTL_KERN; + a[1] = KERN_OSRELEASE; + sysctl(a, 2, NULL, &len, NULL, 0); + vers = (char *)scheme_malloc_atomic(len * sizeof(char)); + sysctl(a, 2, vers, &len, NULL, 0); + + if ((vers[0] == '1') && (vers[1] == '0') && (vers[2] == '.')) { + /* localtime() in 10.7.x (= 10.x at the kernel layer) doesn't seem + to work right with negative numbers that don't fit into 32 bits */ + return 0; + } + } + + return 1; +} #else -# define VALID_TIME_RANGE(x) 1 +# ifdef MIN_VALID_DATE_SECONDS +# define VALID_TIME_RANGE(x) ((x) >= MIN_VALID_DATE_SECONDS) +# else +# define VALID_TIME_RANGE(x) 1 +# endif #endif static Scheme_Object *seconds_to_date(int argc, Scheme_Object **argv)