Subject: Re: strange setlocale() issue
To: Markus Illenseer <markus@core.de>
From: Simon Burge <simonb@wasabisystems.com>
List: tech-userlevel
Date: 10/14/2003 09:11:42
Markus Illenseer wrote:

> 
> This small programm does not work as expected:
> 
> #include <stdio.h>
> #include <locale.h>
> 
> int main(void)
> {
>         const char *foo = "bar";
>         printf("Start\n");
>         if( NULL == setlocale(LC_ALL, "zh_TW.eucTW") )
>         {
>                 printf("Start1\n");
>                 /* yes, I know, the 2nd argument should not be NULL */
>                 printf("error! locale is: %s\n", setlocale(LC_ALL, NULL));
>         }
>         else
>         {
>                 printf("Start2\n");
>                 printf("OK %s\n", foo);
>         }
>         printf("End\n");
> }
> 
>  It does print "Start" and then exits (!) with no error, coredump or
> whatsoever. The other printf() commands are completely ignored, which
> makes me believe, that the setlocale() in the if-clause crashes. A ktrace,
> ktruss and gdb trace did not reveal anything, though.
> 
>  Any idea what might happen here?
> 
>  I am hunting down this bug for several days now, which started as a crash
> of my Apache/PHP when using the above setlocale().

On -current i386 with /usr/lib complied with -g, I see:

(gdb) run
Starting program: /tmp/z1 
Start

Program received signal SIGSEGV, Segmentation fault.
0x480de9ec in vfprintf_unlocked (fp=0xbfbff130, fmt0=0x480f2e38 "%s/%s/%s", 
    ap=0xbfbff1bc "\035.\017H\200h\020Hæ-\017H ")
    at /home/src/lib/libc/stdio/vfprintf.c:459
459                             goto rflag;
(gdb) bt
#0  0x480de9ec in vfprintf_unlocked (fp=0xbfbff130, 
    fmt0=0x480f2e38 "%s/%s/%s", ap=0xbfbff1bc "\035.\017H\200h\020Hæ-\017H ")
    at /home/src/lib/libc/stdio/vfprintf.c:459
#1  0x480dc92c in _snprintf (
    str=0xbfbff250 "/usr/share/locale/zh_TW.eucTW/LC_CTYPE", n=1024, 
    fmt=0x480f2e38 "%s/%s/%s") at /home/src/lib/libc/stdio/snprintf.c:85
#2  0x480d1de8 in loadlocale (category=3)
    at /home/src/lib/libc/locale/setlocale.c:268
#3  0x480d1a91 in __setlocale (category=0, locale=0x8048987 "zh_TW.eucTW")
    at /home/src/lib/libc/locale/setlocale.c:190
#4  0x480d199a in __setlocale_mb_len_max_32 (category=0, 
    locale=0x8048987 "zh_TW.eucTW")
    at /home/src/lib/libc/locale/setlocale32.c:48
#5  0x08048860 in main () at z1.c:8
#6  0x080485e2 in ___start (argc=1, argv=0xbfbff74c, envp=0xbfbff754, 
    cleanup=0x4804c5e0 <_rtld_exit>, obj=0x48056200, ps_strings=0xbfbffff0)
    at /home/src/lib/csu/i386_elf/crt0.c:94

Simon.
--
Simon Burge                            <simonb@wasabisystems.com>
NetBSD Support and Service:         http://www.wasabisystems.com/