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

Bug#571754: strftime("%c") crashes in (some) locations



On Sun, Feb 28, 2010 at 12:58:47PM +0100, Klaus Ethgen wrote:
> Hi,
> 
> Am So den 28. Feb 2010 um 10:39 schrieb Aurelien Jarno:
> > >    setlocale(LC_ALL, "de_DE");
> > >    tm = localtime(NULL);
> > 
> > This is wrong. localtime() doesn't accept a NULL pointer. You should
> > call time(NULL), and pass the result to localtime().
> 
> Might be but this isn't the line where it crashes. Also it work's very
> well filling the tm struct.
> 
> Note that the crash happens in the next line:
> > >    strftime(time, sizeof(time), "%c", tm);

Because tm is NULL.

> but only if the locale is set. This test program is only to demonstrate
> the error in strftime, in the original program I get the value for tm
> from other places (strptime).
> 
> But however, when rewriting the line to "tm = localtime(time(NULL));" I
> get a compile error as there is no such function called time. Maybe
> there is something really wrong with the eglibc?
> 

Because you have overrided the time() function with your time variable.
This is a bug in *your* code not in eglibc.

Here is the fixed version of your code, and it works perfectly.

#include <stdio.h>
#include <time.h>
#include <locale.h>

int main(int argc, char **argv)
{
   char strtime[255];
   time_t t;
   struct tm *tm;
   int ret;

   setlocale(LC_ALL, "de_DE");
   t = time(NULL);
   tm = localtime(&t);
   ret = strftime(strtime, sizeof(strtime), "%c", tm);
   fprintf(stdout, "%s\n", strtime);
} // int main(int argc, char **argv...

-- 
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
aurelien@aurel32.net                 http://www.aurel32.net



Reply to: