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

From: Karel Zak <zakkr(at)zf(dot)jcu(dot)cz>
To: Michael Meskes <meskes(at)postgresql(dot)org>
Cc: Christof Petig <christof(at)petig-baender(dot)de>, pgsql-patches(at)postgresql(dot)org
Subject: Re: Problem with setlocale (found in libecpg) [accessing a memory location after freeing it]
Date: 2001-09-27 15:26:56
Message-ID: 20010927172655.L31778@zf.jcu.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

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(at)zf(dot)jcu(dot)cz>
http://home.zf.jcu.cz/~zakkr/

C, PostgreSQL, PHP, WWW, http://docs.linux.cz, http://mape.jcu.cz

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Rene Pijlman 2001-09-27 16:17:06 Re: [Fwd: Re: JDBC update wont, plz help.]
Previous Message Dave Page 2001-09-27 15:00:41 Re: [HACKERS] UTF-8 support

Browse pgsql-patches by date

  From Date Subject
Next Message greg 2001-09-27 15:35:58 psql microsecond timing patch
Previous Message Vianen, Jeroen van 2001-09-27 14:05:22 Fix for broken JDBC's getColumn() (take 2)