Subject: Re: bug#28180: [w32] Unicode characters in subprocess (git) arguments changed to space

From: Noam Postavsky <[email-masked]>
Date: Mon, 28 Aug 2017 10:42:14 -0400

On Tue, Aug 22, 2017 at 10:54 AM, Eli Zaretskii <[email-masked]> wrote:

> As to the "other places", the only problem I'm aware of is that the
> encoding of the command-line arguments, when they arrive at w32proc.c,
> is not known in advance, so this must be somehow fixed/changed,
> otherwise we will be unable to re-encode them in UTF-16.  I believe
> the comment in w32.c does mention that.

Just to understand the issue better, I applied the attached diff to
use CreateProcessW. It seemed to work, but only when I start emacs
from mingw's msys shell. When running from cmd.exe it still translates
to space.

Furthermore, when I run an unpatched Emacs from the msys shell, the
output of the test I posted above is different:

(call-process "git" nil '(t t) nil
              "-c" "alias.x=!x() { printf '%s' \"$1\" | od -tx1; }; x"
"x" "(好)")
0000000 28 c3 a5 c2 a5 c2 bd 29
0000010

Do you have any idea what setting could cause this?
diff --git c/src/process.c i/src/process.c
index e7ee99a..2c17b5a 100644
--- c/src/process.c
+++ i/src/process.c
@@ -1881,8 +1881,7 @@ usage: (make-process &rest ARGS)  */)
 	  if (STRING_MULTIBYTE (arg))
 	    {
 	      if (NILP (arg_encoding))
-		arg_encoding = (complement_process_encoding_system
-				(XPROCESS (proc)->encode_coding_system));
+		arg_encoding = Qutf_8;
 	      arg = code_convert_string_norecord (arg, arg_encoding, 1);
 	    }
 	  tem = Fcons (arg, tem);
diff --git c/src/w32proc.c i/src/w32proc.c
index 76af55f..86aaee2 100644
--- c/src/w32proc.c
+++ i/src/w32proc.c
@@ -1204,14 +1204,13 @@ static BOOL
 create_child (char *exe, char *cmdline, char *env, int is_gui_app,
 	      pid_t * pPid, child_process *cp)
 {
-  STARTUPINFO start;
+  STARTUPINFOW start;
   SECURITY_ATTRIBUTES sec_attrs;
 #if 0
   SECURITY_DESCRIPTOR sec_desc;
 #endif
   DWORD flags;
   char dir[ MAX_PATH ];
-  char *p;
   const char *ext;
 
   if (cp == NULL) emacs_abort ();
@@ -1242,14 +1241,8 @@ create_child (char *exe, char *cmdline, char *env, int is_gui_app,
   sec_attrs.lpSecurityDescriptor = NULL /* &sec_desc */;
   sec_attrs.bInheritHandle = FALSE;
 
-  filename_to_ansi (process_dir, dir);
-  /* Can't use unixtodos_filename here, since that needs its file name
-     argument encoded in UTF-8.  OTOH, process_dir, which _is_ in
-     UTF-8, points, to the directory computed by our caller, and we
-     don't want to modify that, either.  */
-  for (p = dir; *p; p = CharNextA (p))
-    if (*p == '/')
-      *p = '\\';
+  strcpy (dir, process_dir);
+  unixtodos_filename (dir);
 
   /* CreateProcess handles batch files as exe specially.  This special
      handling fails when both the batch file and arguments are quoted.
@@ -1265,8 +1258,13 @@ create_child (char *exe, char *cmdline, char *env, int is_gui_app,
 	   : CREATE_NEW_CONSOLE);
   if (NILP (Vw32_start_process_inherit_error_mode))
     flags |= CREATE_DEFAULT_ERROR_MODE;
-  if (!CreateProcessA (exe, cmdline, &sec_attrs, NULL, TRUE,
-		       flags, env, dir, &start, &cp->procinfo))
+
+  wchar_t exeW[MAX_PATH], cmdlineW[MAX_PATH], dirW[MAX_PATH];
+  filename_to_utf16 (exe, exeW);
+  filename_to_utf16 (cmdline, cmdlineW);
+  filename_to_utf16 (dir, dirW);
+  if (!CreateProcessW (exeW, cmdlineW, &sec_attrs, NULL, TRUE,
+                       flags, env, dirW, &start, &cp->procinfo))
     goto EH_Fail;
 
   cp->pid = (int) cp->procinfo.dwProcessId;