Re: Problem with setlocale (found in libecpg) [accessing a memory location after freeing it]

Karel Zak <zakkr@zf.jcu.cz>

From: Karel Zak <zakkr@zf.jcu.cz>
To: Michael Meskes <meskes@postgresql.org>
Cc: Christof Petig <christof@petig-baender.de>, pgsql-patches@postgresql.org
Date: 2001-09-27T15:26:56Z
Lists: pgsql-hackers
On Tue, Sep 25, 2001 at 08:15:06PM +0200, Michael Meskes wrote:
> > 
> > Well at least on glibc-2.2 it seems that setlocale retuns a pointer to
> > malloced memory, and frees this pointer on subsequent calls to
> 
> Doesn't look that way on my system. The following programs simply dumps core
> in free().
> 
> #include <locale.h>   
> #include <stdio.h>
> 
> main()
> {
> 	const char *locale=setlocale(LC_NUMERIC, NULL);   
> 	
> 	printf("%c\n", locale);
> 	free(locale);
> }

 Because you bad use setlocale().

 The setlocale(LC_NUMERIC, NULL) returns actual LC_NUMERIC setting, but 
your program hasn't some setting, because you don't call:

setlocale(LC_NUMERIC, "") or setlocale(LC_NUMERIC, "some_locales")

 before setlocale(LC_NUMERIC, NULL), try this program:


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

int
main()
{
        char *locale;

        /* create array with locales names */
        setlocale(LC_NUMERIC, "");

        /* returns data from actual setting */
        locale = setlocale(LC_NUMERIC, NULL);

        printf("%s\n", locale);
        free((void *) locale);
        exit(1);
}

 and don't forget set LC_ALL before program runnig. With default locales "C" 
it is same as with NULL. 

Previous code:

$ export LC_ALL="cs_CZ"
$ ./loc
  cs_CZ
$ export LC_ALL="C"
$ ./loc
  C
  Segmentation fault	<-- in free()


 .... and see locale/setlocale.c in glibc sources :-)

	Karel

-- 
 Karel Zak  <zakkr@zf.jcu.cz>
 http://home.zf.jcu.cz/~zakkr/
 
 C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz