Dangerous: localtime_r, gmtime_r

Brozinski, Stefan stefan.brozinski@materna.de
Tue Sep 23 12:06:00 GMT 2003


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



More information about the Pthreads-win32 mailing list