This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: problem with inheriting environment


On Mon, Jul 01, 2002 at 01:47:35AM +0100, Chris January wrote:
>To duplicate, add the 'export' flag to your CYGWIN environment variable.

That does duplicate it.

>In _addenv, there is the line:
>    envhere = cur_environ ()[offset] = (char *) (ENVMALLOC ? strdup (name) :
>name);
>ENVMALLOC is not set by default, so the entry in the environment table gets
>set to the value of name. However when name goes out of scope in the calling
>process, it's contents may be overwritten. Now the environment table entry
>will be pointing to random memory.
>Therefore, strdup must be called in all situations. Also, the original entry
>should be free'ed to prevent a memory leak.

If you do a 'cvs annotate', you'll see that this is not new code.  It
was added in November 2000.  I don't recall now but you might even be
able to find some discussion about this.  What I think this means is
that the 'export' option has been broken since then.

For fun, try the following code on linux:

  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <time.h>
  #include <string.h>
  #include <sys/wait.h>

  int
  main (int argc, char **argv, char **envirin)
  {
    char buf[80];
    sprintf (buf, "FFFFFFFFFFFF=27");
    putenv (buf);
    sprintf (buf, "GGGGGGGGGGGG=28");
    system ("env");
  }

You'll see that the environment has no hint of FFFFFFFFFFFF=27 but
does have a GGGGGGGGGGGG=28 in it.  To me, this means that the
environment does not get automatically malloced.  Even if putenv
did automatically malloc the things it puts in the environment,
you can't rely on the fact that the user's program didn't manipulate
the environment directly, adding non-malloced strings to it.

So, I've made the CYGWIN export code strdup its options.  I've also
caused the export option to only work when cygwin settings were
retrieved from the registry.

Thanks very much for tracking this down.  I am mystified as to why
this would suddenly show up now, though.  I don't see how it ever
would have worked correctly -- at least not for the last 1.5 years
anyway.

cgf

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]