From ecb7a5606cdb077c86ed81a7db81df5038824de6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fridrich=20=C5=A0trba?= Date: Wed, 28 Apr 2010 14:19:00 +0200 Subject: [PATCH] Pass the environment to CreateProcessA correctly and be more defensive --- dbus/dbus-spawn-win.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/dbus/dbus-spawn-win.c b/dbus/dbus-spawn-win.c index 6c32ab2..29be65a 100644 --- a/dbus/dbus-spawn-win.c +++ b/dbus/dbus-spawn-win.c @@ -471,7 +471,7 @@ protect_argv (char **argv, /* From GPGME, relicensed by g10 Code GmbH. */ static char * -build_commandline (char **argv) +compose_string (char **strings, char separator) { int i; int n = 0; @@ -479,33 +479,46 @@ build_commandline (char **argv) char *p; const char *ptr; - for (i = 0; argv[i]; i++) - n += strlen (argv[i]) + 1; + if (!strings || !strings[0]) + return 0; + for (i = 0; strings[i]; i++) + n += strlen (strings[i]) + 1; n++; buf = p = malloc (n); if (!buf) return NULL; - for (i = 0; argv[i]; i++) + for (i = 0; strings[i]; i++) { - strcpy (p, argv[i]); - p += strlen (argv[i]); - *(p++) = ' '; + strcpy (p, strings[i]); + p += strlen (strings[i]); + *(p++) = separator; } - if (i) - p--; + p--; + *(p++) = '\0'; *p = '\0'; return buf; } +static char * +build_commandline (char **argv) +{ + return compose_string (argv, ' '); +} + +static char * +build_env_string (char** envp) +{ + return compose_string (envp, '\0'); +} static HANDLE -spawn_program (const char* name, char** argv, char** envp) +spawn_program (char* name, char** argv, char** envp) { PROCESS_INFORMATION pi = { NULL, 0, 0, 0 }; STARTUPINFOA si; - char *arg_string; + char *arg_string, *env_string; BOOL result; #ifdef DBUS_WINCE @@ -516,11 +529,16 @@ spawn_program (const char* name, char** argv, char** envp) if (!arg_string) return INVALID_HANDLE_VALUE; + env_string = build_env_string(envp); + memset (&si, 0, sizeof (si)); si.cb = sizeof (si); result = CreateProcessA (name, arg_string, NULL, NULL, FALSE, 0, - envp, NULL, &si, &pi); + (LPVOID)env_string, NULL, &si, &pi); free (arg_string); + if (env_string) + free (env_string); + if (!result) return INVALID_HANDLE_VALUE; -- 2.7.4