This is the mail archive of the
pthreads-win32@sources.redhat.com
mailing list for the pthreas-win32 project.
Dangerous: localtime_r, gmtime_r
- From: "Brozinski, Stefan" <stefan dot brozinski at materna dot de>
- To: "'pthreads-win32 at sources dot redhat dot com'" <pthreads-win32 at sources dot redhat dot com>
- Date: Tue, 23 Sep 2003 14:06:44 +0200
- Subject: Dangerous: localtime_r, gmtime_r
Hi all,
there is a problem with the current implementation of the thread safe
version of "localtime" and "gmtime" currently implemented as a macro in
<pthread.h>.
The current definition for localtime_r is:
#define localtime_r( _clock, _result ) \
( *(_result) = *localtime( (_clock) ), \
(_result) )
The problem is that localtime() may return NULL. At least Microsoft's
implementation in MSVCRT[D].DLL does in fact return NULL if certain
conditions are met. Thus, a page fault may occur when using this
implementation.
I suggest to change the implementation from the current #define to a 'real'
function like:
struct tm *localtime_r(time_t *_clock, struct tm *_result)
{
struct tm *p = localtime(_clock);
if (p)
*(_result) = *p;
return p;
}
Regards
Stefan