Re: libc/locale bug in strftime()?

1997-02-20 Thread Riku Saikkonen
is the source of your greaf, your 'struct tm' is not properly initalized
before a call to strftime().  You don't dynamically allocate your
structure, but it is allocated on the runtime stack, therefore it most
likely contains rubbish data, that breaks strftime().

Oops. Yes, you're right. I didn't think of that... *blush*

(Some history: I first found the bug when I noticed that a self-compiled
gnuplot 3.6beta-315 dumped core on its set locale command (which, among
other things, read the month names into gnuplot's internal structures). I
traced the bug to strftime() and wrote the test program I posted. But
apparently didn't think of valid initalisation...

I now got the newest beta of gnuplot (3.6beta-325), and it works (zeros the
struct tm before calling strftime()), so I guess someone else noticed the
bug too...)

By the way, thanks for the quick replies! I'm impressed. :)

-- 
-=- Rjs -=- [EMAIL PROTECTED], [EMAIL PROTECTED]


--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word unsubscribe to
[EMAIL PROTECTED] . Trouble? e-mail to [EMAIL PROTECTED]


libc/locale bug in strftime()?

1997-02-19 Thread Riku Saikkonen
[I'd submit this as a bug report, but I'm not sure which package has the
bug, or even if it is Debian-specific.]

[I posted about this a week ago to comp.os.linux.development.system; no one
replied. Later I got a chance to test it on a Slackware system (with libc
5.2.something, I think), and the bug wasn't there...]

I think I found a bug in libc 5.4.20. The strftime() function dumps core
when called with %b or %B in the format string (to get month names). But
if I compile the program statically, it works, strangely enough. The bug is
reproducible. Here's a transcript:

{anar}rjs:~/t$ cat k.c
#include stdio.h
#include time.h

main()
{
  char s[32];
  struct tm tm;
  
  tm.tm_mon=1;
  strftime(s,32,%b,tm);
  printf(s='%s'\n,s);

  exit(0);
}
{anar}rjs:~/t$ gcc -o k k.c
{anar}rjs:~/t$ ls -l k.c k
-rwxr-xr-x   1 rjs  rjs  4121 Feb 12 00:05 k
-rw-r--r--   1 rjs  rjs   160 Feb 12 00:04 k.c
{anar}rjs:~/t$ ./k
Segmentation fault (core dumped)
{anar}rjs:~/t$ ls -l k.c k core
-rw---   1 rjs  rjs258048 Feb 12 00:05 core
-rwxr-xr-x   1 rjs  rjs  4121 Feb 12 00:05 k
-rw-r--r--   1 rjs  rjs   160 Feb 12 00:04 k.c
{anar}rjs:~/t$ gcc -static -o k k.c
{anar}rjs:~/t$ ./k
s='Feb'
{anar}rjs:~/t$ 

My system is a Debian 1.2.6 installation with libc 5.4.20, gcc 2.7.2.1,
Linux kernel 2.0.27 (kernel compiled by me, nothing very special; libc and
gcc are from Debian, libc5_5.4.20-1 and gcc_2.7.2.1-4), running on a
Pentium-100. My locale settings are as installed by Debian (no LOCALE or
LANG environment variables set).

Can someone else try the above code to see if it happens on Debian in
general or only my system? Can someone fix the bug? :)

--
-=- Rjs -=- [EMAIL PROTECTED], [EMAIL PROTECTED]


--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word unsubscribe to
[EMAIL PROTECTED] . Trouble? e-mail to [EMAIL PROTECTED]


Re: libc/locale bug in strftime()?

1997-02-19 Thread Orn E. Hansen

Looking a  little better at your code... I expect you are assuming that
the memory allocation routine, has null'ed your data (I think I remember
that a 'struct tm' filled with zeroes should give Jan 1, 1970?).  This
is the source of your greaf, your 'struct tm' is not properly initalized
before a call to strftime().  You don't dynamically allocate your
structure, but it is allocated on the runtime stack, therefore it most
likely contains rubbish data, that breaks strftime().

main()
{
  char s[32];
  struct tm tm;
  
  tm.tm_mon=1;
  strftime(s,32,%b,tm);



Ørn Einar Hansen [EMAIL PROTECTED]
  [EMAIL PROTECTED]
   home+fax; +46 035 217194


--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word unsubscribe to
[EMAIL PROTECTED] . Trouble? e-mail to [EMAIL PROTECTED]


Re: libc/locale bug in strftime()?

1997-02-19 Thread Orn E. Hansen

It is not a problem with strftime(), it works fine here (a demo at the end
of the letter) and I'm using libc5 version 5.4.20-1.  Nor is it a bug with
locale, as you're not using it in your demo (see below).

#include time.h
#include locale.h

main()
{
  time_t cur_time = time(NULL);
  struct tm *loct;
  char *str_time, buf[128];

  setlocale(LC_ALL, );
  str_time = ctime(cur_time);
  printf(%s\n, str_time);
  loct = localtime(cur_time);
  str_time = asctime(loct);
  printf(%s\n, str_time);
  strftime(buf, sizeof(buf), %a %d %b. %Y %Z %X, loct);
  printf(%s\n, buf);
  strftime(buf, sizeof(buf), %B, loct);
  printf(%s\n, buf);
}



Ørn Einar Hansen [EMAIL PROTECTED]
  [EMAIL PROTECTED]
   home+fax; +46 035 217194


--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word unsubscribe to
[EMAIL PROTECTED] . Trouble? e-mail to [EMAIL PROTECTED]