From: drh Date: Tue, 21 Jun 2011 15:34:05 +0000 (+0000) Subject: Backport the localtime() failure patch from trunk. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6a8a3a97ce1d67b09a8f5107124cbb93b10b89b5;p=thirdparty%2Fsqlite.git Backport the localtime() failure patch from trunk. FossilOrigin-Name: 2f5b37122b720505707814d08471e3ac3bdf3cb5 --- diff --git a/manifest b/manifest index e4e18ddfd1..4fc8562959 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,5 @@ ------BEGIN PGP SIGNED MESSAGE----- -Hash: SHA1 - -C Backport\sa\sfix\sfrom\s[6b0cabd017ed25]\s(version\s3.6.16\s-\s[2009-03-05])\sto\sthe\n3.6.1\sbranch,\sa\sfix\sthat\sprevents\sa\snull\spointer\sderef\sif\sone\sof\sthe\ncolumn\saccess\sroutines\ssuch\sas\ssqlite3_column_int()\sis\scalled\swith\sa\nNULL\sfirst\sparameter. -D 2011-03-07T16:12:27.175 +C Backport\sthe\slocaltime()\sfailure\spatch\sfrom\strunk. +D 2011-06-21T15:34:05.185 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 2713ea64947be3b35f35d9a3158bb8299c90b019 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -105,7 +102,7 @@ F src/btreeInt.h ab18c7b4980314e9e4b402e5dcde09f3c2545576 F src/build.c d8fe03f76229e333c9e1989eb22b4120f1f46376 F src/callback.c c9f75a4c403f166af3761df47d78a806587d63af F src/complete.c cb14e06dbe79dee031031f0d9e686ff306afe07c -F src/date.c 52a54811218a76da6235420f532ece841159a96d +F src/date.c 1478fb55fa23cc9b4d50341668c26f8a0d291abb F src/delete.c 28595a17b73d40e35fd285cd3741e9f5bade6b1d F src/expr.c d2046feae56970f15dd6fc89fb9193d973fbb644 F src/fault.c 3638519d1e0b82bccfafcb9f5ff491918b28f8e1 @@ -620,14 +617,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P ecb1419e4b359e68d583b627ac40cff33cbc06b8 -R f4eb136dedf0796ff3d52b70df802286 +P df3436405e423436f75ead99c8ec70494c32ccca +R 3fa15598d551e87da4b14a0afdfbde4b U drh -Z b8227083f3e49a4cb59e837a7075a68f ------BEGIN PGP SIGNATURE----- -Version: GnuPG v1.4.6 (GNU/Linux) - -iD8DBQFNdQPvoxKgR168RlERAhu1AJsHEjp8iaJdPsdB7OI0p57Rezp/bgCePKIO -88lrc0awe5uPsaCdvPInwzs= -=1nqg ------END PGP SIGNATURE----- +Z 22d55452306cbf44a4a74b182bcaa66c diff --git a/manifest.uuid b/manifest.uuid index 372a05549d..05eee2d7cf 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -df3436405e423436f75ead99c8ec70494c32ccca \ No newline at end of file +2f5b37122b720505707814d08471e3ac3bdf3cb5 \ No newline at end of file diff --git a/src/date.c b/src/date.c index 08ee30c38a..a6bbf2b9ad 100644 --- a/src/date.c +++ b/src/date.c @@ -53,23 +53,6 @@ #ifndef SQLITE_OMIT_DATETIME_FUNCS -/* -** On recent Windows platforms, the localtime_s() function is available -** as part of the "Secure CRT". It is essentially equivalent to -** localtime_r() available under most POSIX platforms, except that the -** order of the parameters is reversed. -** -** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. -** -** If the user has not indicated to use localtime_r() or localtime_s() -** already, check for an MSVC build environment that provides -** localtime_s(). -*/ -#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \ - defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) -#define HAVE_LOCALTIME_S 1 -#endif - /* ** A structure for holding a single date and time. */ @@ -423,15 +406,67 @@ static void clearYMD_HMS_TZ(DateTime *p){ p->validTZ = 0; } +/* +** On recent Windows platforms, the localtime_s() function is available +** as part of the "Secure CRT". It is essentially equivalent to +** localtime_r() available under most POSIX platforms, except that the +** order of the parameters is reversed. +** +** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx. +** +** If the user has not indicated to use localtime_r() or localtime_s() +** already, check for an MSVC build environment that provides +** localtime_s(). +*/ +#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \ + defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE) +#define HAVE_LOCALTIME_S 1 +#endif + +#ifndef SQLITE_OMIT_LOCALTIME +/* +** The following routine implements the rough equivalent of localtime_r() +** using whatever operating-system specific localtime facility that +** is available. This routine returns 0 on success and +** non-zero on any kind of error. +*/ +int osLocaltime(time_t *t, struct tm *pTm){ + int rc; +#if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \ + && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S) + struct tm *pX; + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex_enter(mutex); + pX = localtime(t); + if( pX ) *pTm = *pX; + sqlite3_mutex_leave(mutex); + rc = pX==0; +#else +#if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R + rc = localtime_r(t, pTm)==0; +#else + rc = localtime_s(pTm, t); +#endif /* HAVE_LOCALTIME_R */ +#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */ + return rc; +} +#endif /* SQLITE_OMIT_LOCALTIME */ + #ifndef SQLITE_OMIT_LOCALTIME /* ** Compute the difference (in milliseconds) ** between localtime and UTC (a.k.a. GMT) ** for the time value p where p is in UTC. */ -static int localtimeOffset(DateTime *p){ +static sqlite3_int64 localtimeOffset( + DateTime *p, /* Date at which to calculate offset */ + sqlite3_context *pCtx, /* Write error here if one occurs */ + int *pRc /* OUT: Error code. SQLITE_OK or ERROR */ +){ DateTime x, y; time_t t; + struct tm sLocal; + x = *p; computeYMD_HMS(&x); if( x.Y<1971 || x.Y>=2038 ){ @@ -449,42 +484,17 @@ static int localtimeOffset(DateTime *p){ x.validJD = 0; computeJD(&x); t = x.iJD/1000 - 2440587.5*86400.0; -#ifdef HAVE_LOCALTIME_R - { - struct tm sLocal; - localtime_r(&t, &sLocal); - y.Y = sLocal.tm_year + 1900; - y.M = sLocal.tm_mon + 1; - y.D = sLocal.tm_mday; - y.h = sLocal.tm_hour; - y.m = sLocal.tm_min; - y.s = sLocal.tm_sec; - } -#elif defined(HAVE_LOCALTIME_S) - { - struct tm sLocal; - localtime_s(&sLocal, &t); - y.Y = sLocal.tm_year + 1900; - y.M = sLocal.tm_mon + 1; - y.D = sLocal.tm_mday; - y.h = sLocal.tm_hour; - y.m = sLocal.tm_min; - y.s = sLocal.tm_sec; - } -#else - { - struct tm *pTm; - sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); - pTm = localtime(&t); - y.Y = pTm->tm_year + 1900; - y.M = pTm->tm_mon + 1; - y.D = pTm->tm_mday; - y.h = pTm->tm_hour; - y.m = pTm->tm_min; - y.s = pTm->tm_sec; - sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); + if( osLocaltime(&t, &sLocal) ){ + sqlite3_result_error(pCtx, "local time unavailable", -1); + *pRc = SQLITE_ERROR; + return 0; } -#endif + y.Y = sLocal.tm_year + 1900; + y.M = sLocal.tm_mon + 1; + y.D = sLocal.tm_mday; + y.h = sLocal.tm_hour; + y.m = sLocal.tm_min; + y.s = sLocal.tm_sec; y.validYMD = 1; y.validHMS = 1; y.validJD = 0; @@ -515,7 +525,7 @@ static int localtimeOffset(DateTime *p){ ** ** Return 0 on success and 1 if there is any kind of error. */ -static int parseModifier(const char *zMod, DateTime *p){ +static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){ int rc = 1; int n; double r; @@ -535,9 +545,8 @@ static int parseModifier(const char *zMod, DateTime *p){ */ if( strcmp(z, "localtime")==0 ){ computeJD(p); - p->iJD += localtimeOffset(p); + p->iJD += localtimeOffset(p, pCtx, &rc); clearYMD_HMS_TZ(p); - rc = 0; } break; } @@ -558,11 +567,15 @@ static int parseModifier(const char *zMod, DateTime *p){ else if( strcmp(z, "utc")==0 ){ double c1; computeJD(p); - c1 = localtimeOffset(p); + c1 = localtimeOffset(p, pCtx, &rc); p->iJD -= c1; clearYMD_HMS_TZ(p); - p->iJD += c1 - localtimeOffset(p); - rc = 0; + c1 = localtimeOffset(p, pCtx, &rc); + if( rc==SQLITE_OK ){ + p->iJD -= c1; + clearYMD_HMS_TZ(p); + p->iJD += c1 - localtimeOffset(p, pCtx, &rc); + } } #endif break; @@ -733,7 +746,8 @@ static int isDate( } } for(i=1; i