This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB 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]

[committed] Sort windows environment before starting inferior


Microsoft documentation implies that the environment should be sorted.
The patch below ensures that it is.

cgf

2005-11-27  Christopher Faylor  <cgf@timesys.com>

	* win32-nat.c (env_sort): New function.
	(win32_create_inferior): Rename env to in_env.  Sort environment before
	handing it off to CreateProcess.
	(win32_current_sos): Quiet a compiler warning.

Index: win32-nat.c
===================================================================
RCS file: /cvs/uberbaum/gdb/win32-nat.c,v
retrieving revision 1.116
diff -u -p -r1.116 win32-nat.c
--- win32-nat.c	1 Nov 2005 14:07:00 -0000	1.116
+++ win32-nat.c	28 Nov 2005 01:13:01 -0000
@@ -1697,13 +1697,22 @@ win32_open (char *arg, int from_tty)
   error (_("Use the \"run\" command to start a Unix child process."));
 }
 
+/* Function called by qsort to sort environment strings.  */
+static int
+env_sort (const void *a, const void *b)
+{     
+  const char **p = (const char **) a; 
+  const char **q = (const char **) b;
+  return strcasecmp (*p, *q);
+}
+
 /* Start an inferior win32 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.
    ENV is the environment vector to pass.  Errors reported with error().  */
 
 static void
-win32_create_inferior (char *exec_file, char *allargs, char **env,
+win32_create_inferior (char *exec_file, char *allargs, char **in_env,
 		       int from_tty)
 {
   char *winenv;
@@ -1783,27 +1792,33 @@ win32_create_inferior (char *exec_file, 
        strings (i.e. two nulls terminate the list).  */
 
     /* Get total size for env strings.  */
-    for (envlen = 0, i = 0; env[i] && *env[i]; i++)
+    for (envlen = 0, i = 0; in_env[i] && *in_env[i]; i++)
       {
 	int j, len;
 
 	for (j = 0; conv_path_names[j]; j++)
 	  {
 	    len = strlen (conv_path_names[j]);
-	    if (strncmp (conv_path_names[j], env[i], len) == 0)
+	    if (strncmp (conv_path_names[j], in_env[i], len) == 0)
 	      {
-		if (cygwin_posix_path_list_p (env[i] + len))
+		if (cygwin_posix_path_list_p (in_env[i] + len))
 		  envlen += len
-		    + cygwin_posix_to_win32_path_list_buf_size (env[i] + len);
+		    + cygwin_posix_to_win32_path_list_buf_size (in_env[i] + len);
 		else
-		  envlen += strlen (env[i]) + 1;
+		  envlen += strlen (in_env[i]) + 1;
 		break;
 	      }
 	  }
 	if (conv_path_names[j] == NULL)
-	  envlen += strlen (env[i]) + 1;
+	  envlen += strlen (in_env[i]) + 1;
       }
 
+    size_t envsize = sizeof (in_env[0]) * (i + 1);
+    char **env = (char **) alloca (envsize);
+    memcpy (env, in_env, envsize);
+    /* Windows programs expect the environment block to be sorted.  */
+    qsort (env, i, sizeof (char *), env_sort);
+
     winenv = alloca (envlen + 1);
 
     /* Copy env strings into new buffer.  */
@@ -2226,7 +2241,7 @@ win32_current_sos (void)
 {
   struct so_list *sop;
   struct so_list *start = NULL;
-  struct so_list *last;
+  struct so_list *last = NULL;
 
   if (!solib_start.next && core_bfd)
     {


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