Re: Problem with setlocale (found in libecpg) [accessing a memory location after freeing it] - Mailing list pgsql-patches

From Karel Zak
Subject Re: Problem with setlocale (found in libecpg) [accessing a memory location after freeing it]
Date
Msg-id 20010927172655.L31778@zf.jcu.cz
Whole thread Raw
In response to Re: Problem with setlocale (found in libecpg) [accessing a memory location after freeing it]  (Michael Meskes <meskes@postgresql.org>)
List 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@zf.jcu.cz>
 http://home.zf.jcu.cz/~zakkr/

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

pgsql-patches by date:

Previous
From: greg@turnstep.com
Date:
Subject: psql microsecond timing patch
Next
From: Tom Lane
Date:
Subject: Re: psql microsecond timing patch