setenv() causes memory leak on PowerPC

The code below (which is a variation of the setenv() example taken from
Help) causes memory leak on PowerPC. The very same code works ok on x86.
I guess it is a bug.

------------- cut -----------------
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
char* path;
int i;

for(;:wink: {
if( setenv( “INCLUDE”, “/usr/nto/include:/home/fred/include”, 1 ) == 0 ) {
}
sched_yield();
}
return EXIT_SUCCESS;
}
------------- cut -----------------

Here is also sin output:

/ppcbe/bin/sin -p 16390 mem

setenv 16390 4K 33M 4K 516K 20698
zero 4803F000 00000000 ----- ----- 4K
RW_
setenv 48040000 00000000 4K SR_X_ 4K
PRWX_
zero 48042000 00000000 ----- ----- 16K
RW_
zero 48046000 00000000 ----- ----- 16K
RW_
zero 4804A000 00000000 ----- ----- 16K
RW_
zero 4804E000 00000000 ----- ----- 16K
RW_
zero 48052000 00000000 ----- ----- 16K
RW_
zero 48056000 00000000 ----- ----- 16K

etc

Garry Turcotte wrote:

You should use putenv and modify the string yourself.
setenv() is not allowed to free the memory.
(see the rationale for putenv in the SUS)

It should be added as a caveat to the docs for setenv.

I can not agree more. The following statement in setenv() help chapter
is misleading: “if name exists and overwrite is nonzero, the variable’s
old value is overwritten with value”. My interpretation was that POSIX’s
setenv(env, val, 1) mimics behaviour of UNIX’s putenv(string).

Thanks, Garry.

Dmitri Poustovalov wrote:

The code below (which is a variation of the setenv() example taken
from Help) causes memory leak on PowerPC. The very same code works ok
on x86. I guess it is a bug.

------------- cut -----------------
#include <stdio.h
#include <stdlib.h

int main( void )
{
char* path;
int i;

for(;:wink: {
if( setenv( “INCLUDE”, “/usr/nto/include:/home/fred/include”,
1 ) == 0 ) {
}
sched_yield();
}
return EXIT_SUCCESS;
}
------------- cut -----------------

Here is also sin output:

/ppcbe/bin/sin -p 16390 mem

setenv 16390 4K 33M 4K 516K
20698
zero 4803F000 00000000 ----- -----
4K RW_
setenv 48040000 00000000 4K SR_X_
4K PRWX_
zero 48042000 00000000 ----- -----
16K RW_
zero 48046000 00000000 ----- -----
16K RW_
zero 4804A000 00000000 ----- -----
16K RW_
zero 4804E000 00000000 ----- -----
16K RW_
zero 48052000 00000000 ----- -----
16K RW_
zero 48056000 00000000 ----- ----- 16K

etc

You should use putenv and modify the string yourself.
setenv() is not allowed to free the memory.
(see the rationale for putenv in the SUS)

It should be added as a caveat to the docs for setenv.

Dmitri Poustovalov wrote:

The code below (which is a variation of the setenv() example taken from
Help) causes memory leak on PowerPC. The very same code works ok on x86.
I guess it is a bug.

------------- cut -----------------
#include <stdio.h
#include <stdlib.h

int main( void )
{
char* path;
int i;

for(;:wink: {
if( setenv( “INCLUDE”, “/usr/nto/include:/home/fred/include”, 1
) == 0 ) {
}
sched_yield();
}
return EXIT_SUCCESS;
}
------------- cut -----------------

Here is also sin output:

/ppcbe/bin/sin -p 16390 mem

setenv 16390 4K 33M 4K 516K 20698
zero 4803F000 00000000 ----- ----- 4K
RW_
setenv 48040000 00000000 4K SR_X_ 4K
PRWX_
zero 48042000 00000000 ----- ----- 16K
RW_
zero 48046000 00000000 ----- ----- 16K
RW_
zero 4804A000 00000000 ----- ----- 16K
RW_
zero 4804E000 00000000 ----- ----- 16K
RW_
zero 48052000 00000000 ----- ----- 16K
RW_
zero 48056000 00000000 ----- ----- 16K

etc