Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Jul 2007 12:22:17 +1000
From:      Peter Jeremy <peterjeremy@optushome.com.au>
To:        Andrey Chernov <ache@nagual.pp.ru>, freebsd-current@freebsd.org
Subject:   Re: HEADS UP:  getenv() and family API change
Message-ID:  <20070711022217.GA19840@turion.vk2pj.dyndns.org>
In-Reply-To: <20070710224619.GA31654@nagual.pp.ru>
References:  <20070703182400.Q1449@baba.farley.org> <Xuu8UV3Bay@dmeyer.dinoex.sub.org> <20070709145418.T52164@thor.farley.org> <20070710154148.GA22873@nagual.pp.ru> <20070710213602.GX3434@turion.vk2pj.dyndns.org> <20070710224619.GA31654@nagual.pp.ru>

index | next in thread | previous in thread | raw e-mail

[-- Attachment #1 --]
On 2007-Jul-11 02:46:19 +0400, Andrey Chernov <ache@nagual.pp.ru> wrote:
>On Wed, Jul 11, 2007 at 07:36:02AM +1000, Peter Jeremy wrote:
>> On 2007-Jul-10 19:41:48 +0400, Andrey Chernov <ache@nagual.pp.ru> wrote:
>> >To say strictly, copying somewhere is not neccessary since this way works 
>> >too:
>> >
>> >static char *s = "PATH=/bin";
>> >
>> >putenv(s);
>> 
>> I thought the C compiler was still free to place the string into RO
>> memory and/or coalesce it with other strings in that case.
>> 
>> Wouldn't the following be clearer (s is forced to be writable):
>> 
>> static char s[] = "PATH=/bin";
>> 
>> putenv(s);
>
>This two are the same, since there is no "const", so compiler can't put 
>static char *s
>into RO memory.

Maybe that's the theory but neither gcc 4.2.0 (as in -current) or
gcc 3.4.6 (as in 6-stable) work that way.  The following program
shows that both 's' and 't' are initialised to point to the same
read-only string.

----8<------
#include <stdio.h>

int main () {
    static char *s = "PATH=/bin";
    static char *t = "PATH=/bin";
    static char u[] = "PATH=/bin";

    if (s == t)
	printf("s & t are common\n");

    s = "PATH=/sbin";
    printf("%s\n", s);

    u[4] = ':';
    printf("u=\"%s\"\n", u);
    t[4] = ':';
    printf("t=\"%s\"\n", t);
    s[4] = ':';
    printf("s=\"%s\"\n", s);

    return 0;
}
----8<------

gives the following results (-current shown, 6-stable is the same):
cc -O2 -fno-strict-aliasing -pipe -march=athlon-xp  x.c  -o x
server% ./x
s & t are common
PATH=/sbin
u="PATH:/bin"
zsh: segmentation fault (core dumped)  ./x
server% gcc -v
Using built-in specs.
Target: i386-undermydesk-freebsd
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 4.2.0 20070514 [FreeBSD]
server% 

-- 
Peter Jeremy

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (FreeBSD)

iD8DBQFGlD7Z/opHv/APuIcRAoo/AJ93bYmcX1mkNEkrRnwiRWydKzSnRQCcDT5A
tXQa5LhMIo/kKrjAGsDD5Rg=
=4D42
-----END PGP SIGNATURE-----
help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070711022217.GA19840>