Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Corinna Vinschen <vinschen@redhat.com>
To: gdb-patches@sourceware.org
Subject: Re: [RFA] testsuite: Add a test for passing of environment variables to inferior
Date: Wed, 05 Oct 2011 11:40:00 -0000	[thread overview]
Message-ID: <20111005113941.GA13366@calimero.vinschen.de> (raw)
In-Reply-To: <003901cc82b0$471e3a00$d55aae00$@muller@ics-cnrs.unistra.fr>

On Oct  4 18:11, Pierre Muller wrote:
> here is an update that unsets all environment
> variables both in environ list before calling
> CreateProcess and in in_env list after...
> 
>   This passes the new updated test I just sent.

Oh boy, I guess there's no way around that for now.  I will create
a new cygwin_internal call for just such a scenario, as proposed in
http://sourceware.org/ml/gdb-patches/2011-10/msg00079.html and add a
patch to GDB to use it if it's available.  That way, we can limit this
complicated method to current and older versions of Cygwin.

> +  /* Reset all environment variables to avoid leftover on next run. */
> +  for (i = 0; environ[i] && *environ[i]; i++)
> +    {
> +      char *equalpos;
> +      char *copy = alloca (strlen(environ[i]) + 1);

If the environment is very large, using alloca here in a loop might
result in a stack overflow.  Pretty unlikely, I assume, but possible.

> +      strcpy (copy, environ[i]);
> +      equalpos = strchr (copy, '=');
> +      if (equalpos)
> +	*equalpos = '\0';
> +      SetEnvironmentVariableA (copy, NULL);

Since Cygwin 1.7, the Cygwin multibyte charset does not correspond with
the current Windows ANSI codepage.  Cygwin's default multibyte charset
is UTF-8, for instance, while the ANSI Windows functions will use some
arbitrary Windows codepage depending on system and user language.  So we
should convert the variables to UNICODE and use the corresponding Win32
function.

Here's my proposal, based on your patch.  I'll work on using the
yet-to-be-created new cygwin_internal call after I implemented it in
Cygwin.

	* windows-nat.c: Include wchar.h to avoid compiler warnings.
	(clear_win32_environment): New function for Cygwin to clear out
	Win32 environment.
	(windows_create_inferior): Prepare new environment from in_env
	for Cygwin, too.

Index: windows-nat.c
===================================================================
RCS file: /cvs/src/src/gdb/windows-nat.c,v
retrieving revision 1.219
diff -u -p -r1.219 windows-nat.c
--- windows-nat.c	28 Sep 2011 09:07:54 -0000	1.219
+++ windows-nat.c	5 Oct 2011 11:36:10 -0000
@@ -40,6 +40,7 @@
 #include <imagehlp.h>
 #include <psapi.h>
 #ifdef __CYGWIN__
+#include <wchar.h>
 #include <sys/cygwin.h>
 #endif
 #include <signal.h>
@@ -1963,6 +1964,28 @@ envvar_cmp (const void *a, const void *b
 }
 #endif
 
+#ifdef __CYGWIN__
+static void
+clear_win32_environment (char **env)
+{
+  int i;
+  size_t len;
+  wchar_t *copy = NULL, *equalpos;
+
+  for (i = 0; env[i] && *env[i]; i++)
+    {
+      len = mbstowcs (NULL, env[i], 0) + 1;
+      copy = (wchar_t *) xrealloc (copy, len * sizeof (wchar_t));
+      mbstowcs (copy, env[i], len);
+      equalpos = wcschr (copy, L'=');
+      if (equalpos)
+        *equalpos = L'\0';
+      SetEnvironmentVariableW (copy, NULL);
+    }
+  xfree (copy);
+}
+#endif
+
 /* Start an inferior windows child process and sets inferior_ptid to its pid.
    EXEC_FILE is the file to run.
    ALLARGS is a string containing the arguments to the program.
@@ -1980,6 +2003,7 @@ windows_create_inferior (struct target_o
   cygwin_buf_t *toexec;
   cygwin_buf_t *cygallargs;
   cygwin_buf_t *args;
+  char **old_env;
   size_t len;
   int tty;
   int ostdin, ostdout, ostderr;
@@ -2066,7 +2090,11 @@ windows_create_inferior (struct target_o
   strcat (args, cygallargs);
 #endif
 
+  /* Reset all Win32 environment variables to avoid leftover on next run. */
+  clear_win32_environment (environ);
   /* Prepare the environment vars for CreateProcess.  */
+  old_env = environ;
+  environ = in_env;
   cygwin_internal (CW_SYNC_WINENV);
 
   if (!inferior_io_terminal)
@@ -2101,6 +2129,12 @@ windows_create_inferior (struct target_o
 		       NULL,	/* current directory */
 		       &si,
 		       &pi);
+  /* Reset all environment variables to avoid leftover on next run. */
+  clear_win32_environment (in_env);
+  /* Restore normal GDB environment variables.  */
+  environ = old_env;
+  cygwin_internal (CW_SYNC_WINENV);
+
   if (tty >= 0)
     {
       close (tty);


Corinna

-- 
Corinna Vinschen
Cygwin Project Co-Leader
Red Hat


  reply	other threads:[~2011-10-05 11:40 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-04 12:36 Pierre Muller
2011-10-04 13:45 ` Corinna Vinschen
2011-10-04 14:43   ` Eli Zaretskii
2011-10-04 14:48     ` Pierre Muller
2011-10-04 14:50   ` Pierre Muller
2011-10-04 15:13     ` Corinna Vinschen
2011-10-04 16:09       ` [RFA-v2] " Pierre Muller
2011-10-04 16:11       ` [RFA] " Pierre Muller
2011-10-05 11:40         ` Corinna Vinschen [this message]
2011-10-05 12:08           ` Pierre Muller
2011-10-05 12:52             ` Corinna Vinschen
2011-10-06 14:51               ` Christopher Faylor
2011-10-06 15:03                 ` Corinna Vinschen
2011-10-07 12:26                   ` Christopher Faylor
2011-10-07 13:53                     ` Corinna Vinschen
     [not found]       ` <12954.5351061553$1317744599@news.gmane.org>
2011-10-04 17:27         ` [RFA-v2] " Tom Tromey
2011-10-04 21:35           ` [RFA-v3] " Pierre Muller
     [not found]           ` <29288.743020925$1317764135@news.gmane.org>
2011-10-05 13:53             ` Tom Tromey
2011-10-05 14:25               ` Pierre Muller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111005113941.GA13366@calimero.vinschen.de \
    --to=vinschen@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox