public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Problem with putenv() in global constructor
@ 2003-01-07  9:46 Gregory Shpitalnik
  2003-01-07  9:49 ` Gregory Shpitalnik
  2003-01-07 11:51 ` Alexandre Oliva
  0 siblings, 2 replies; 4+ messages in thread
From: Gregory Shpitalnik @ 2003-01-07  9:46 UTC (permalink / raw)
  To: gcc, gcc

[-- Attachment #1: Type: text/plain, Size: 1922 bytes --]

Hi !

I found some strange putenv() function behavior when it's used in a
global object (C++ code).
constructor. I work on SPARC/Solaris 8 with g++/gcc version 2.95.3
20010315 (release).
Below is the C++ program that sets new (not existed yet) environment
variable MYPATH
with the help of putenv() C-function in the global object constructor.
It turned out that:
1. This environment variable setting is invisible within the main()
function
2. Resetting of this environment variable in the main() function causes
"freeing of non heap memory - attempting to free memory on the stack"
problem
(this is the message from Rational purify tool) within putenv() function
and
does not change the environment variable.
It seems that something goes wrong within the data structure
that hold environment variables.

I compiled the program with "g++ -Wall test.cpp -o test".
------------------------------------------
// test.cpp

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


static const char* env_var = "MYPATH";

class MyClass
{
public:
     MyClass()
     {
           cout << env_var << " = " << getenv(env_var) << endl;
           char* value = (char*) malloc(100);
           strcpy(value, "MYPATH=/tmp:/users/gregory");
           putenv(value);
           cout << env_var << " = " << getenv(env_var) << endl;
     }
};

MyClass a;

int main(int argc, char *argv[])
{
  cout << env_var << " = " << getenv(env_var) << endl;
  char* value = (char*) malloc(100);
  strcpy(value, "MYPATH=gregory");
  putenv(value);
  cout << env_var << " = " << getenv(env_var) << endl;

  return 0;
}
-------------------------------------------
The output of the program is:
-----------------------------
MYPATH = (null)
MYPATH = /tmp:/users/galileo101/gregory
MYPATH = (null)
MYPATH = (null)
-----------------------------

Is it a known problem ? Is it a feature or a bug ?

Thanks,

Gregory Shpitalnik
gregory@il.marvell.com


[-- Attachment #2: Card for Gregory Shpitalnik --]
[-- Type: text/x-vcard, Size: 180 bytes --]

begin:vcard 
n:Shpitalnik;Gregory
x-mozilla-html:FALSE
org:Galileo;SD
adr:;;;;;;
version:2.1
email;internet:gregory@il.marvell.com
x-mozilla-cpt:;0
fn:Gregory Shpitalnik
end:vcard

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Problem with putenv() in global constructor
  2003-01-07  9:46 Problem with putenv() in global constructor Gregory Shpitalnik
@ 2003-01-07  9:49 ` Gregory Shpitalnik
  2003-01-07 11:51 ` Alexandre Oliva
  1 sibling, 0 replies; 4+ messages in thread
From: Gregory Shpitalnik @ 2003-01-07  9:49 UTC (permalink / raw)
  To: gcc, gcc

[-- Attachment #1: Type: text/plain, Size: 1922 bytes --]

Hi !

I found some strange putenv() function behavior when it's used in a
global object (C++ code).
constructor. I work on SPARC/Solaris 8 with g++/gcc version 2.95.3
20010315 (release).
Below is the C++ program that sets new (not existed yet) environment
variable MYPATH
with the help of putenv() C-function in the global object constructor.
It turned out that:
1. This environment variable setting is invisible within the main()
function
2. Resetting of this environment variable in the main() function causes
"freeing of non heap memory - attempting to free memory on the stack"
problem
(this is the message from Rational purify tool) within putenv() function
and
does not change the environment variable.
It seems that something goes wrong within the data structure
that hold environment variables.

I compiled the program with "g++ -Wall test.cpp -o test".
------------------------------------------
// test.cpp

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


static const char* env_var = "MYPATH";

class MyClass
{
public:
     MyClass()
     {
           cout << env_var << " = " << getenv(env_var) << endl;
           char* value = (char*) malloc(100);
           strcpy(value, "MYPATH=/tmp:/users/gregory");
           putenv(value);
           cout << env_var << " = " << getenv(env_var) << endl;
     }
};

MyClass a;

int main(int argc, char *argv[])
{
  cout << env_var << " = " << getenv(env_var) << endl;
  char* value = (char*) malloc(100);
  strcpy(value, "MYPATH=gregory");
  putenv(value);
  cout << env_var << " = " << getenv(env_var) << endl;

  return 0;
}
-------------------------------------------
The output of the program is:
-----------------------------
MYPATH = (null)
MYPATH = /tmp:/users/galileo101/gregory
MYPATH = (null)
MYPATH = (null)
-----------------------------

Is it a known problem ? Is it a feature or a bug ?

Thanks,

Gregory Shpitalnik
gregory@il.marvell.com


[-- Attachment #2: Card for Gregory Shpitalnik --]
[-- Type: text/x-vcard, Size: 180 bytes --]

begin:vcard 
n:Shpitalnik;Gregory
x-mozilla-html:FALSE
org:Galileo;SD
adr:;;;;;;
version:2.1
email;internet:gregory@il.marvell.com
x-mozilla-cpt:;0
fn:Gregory Shpitalnik
end:vcard

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with putenv() in global constructor
  2003-01-07  9:46 Problem with putenv() in global constructor Gregory Shpitalnik
  2003-01-07  9:49 ` Gregory Shpitalnik
@ 2003-01-07 11:51 ` Alexandre Oliva
  2003-01-07 12:06   ` Alexandre Oliva
  1 sibling, 1 reply; 4+ messages in thread
From: Alexandre Oliva @ 2003-01-07 11:51 UTC (permalink / raw)
  To: Gregory Shpitalnik; +Cc: gcc, gcc

On Jan  7, 2003, Gregory Shpitalnik <gregory@il.marvell.com> wrote:

> I found some strange putenv() function behavior when it's used in a
> global object (C++ code).
> constructor. I work on SPARC/Solaris 8 with g++/gcc version 2.95.3
> 20010315 (release).

Well, then you're probably going to have to discuss this with Sun
engineers who wrote their C library.  putenv() is not implemented in
GCC, GCC just uses whatever the C library of the OS you're running
has to offer.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                 aoliva@{redhat.com, gcc.gnu.org}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist                Professional serial bug killer

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Problem with putenv() in global constructor
  2003-01-07 11:51 ` Alexandre Oliva
@ 2003-01-07 12:06   ` Alexandre Oliva
  0 siblings, 0 replies; 4+ messages in thread
From: Alexandre Oliva @ 2003-01-07 12:06 UTC (permalink / raw)
  To: Gregory Shpitalnik; +Cc: gcc, gcc

On Jan  7, 2003, Gregory Shpitalnik <gregory@il.marvell.com> wrote:

> I found some strange putenv() function behavior when it's used in a
> global object (C++ code).
> constructor. I work on SPARC/Solaris 8 with g++/gcc version 2.95.3
> 20010315 (release).

Well, then you're probably going to have to discuss this with Sun
engineers who wrote their C library.  putenv() is not implemented in
GCC, GCC just uses whatever the C library of the OS you're running
has to offer.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                 aoliva@{redhat.com, gcc.gnu.org}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist                Professional serial bug killer

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-01-07 11:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-07  9:46 Problem with putenv() in global constructor Gregory Shpitalnik
2003-01-07  9:49 ` Gregory Shpitalnik
2003-01-07 11:51 ` Alexandre Oliva
2003-01-07 12:06   ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).