Win32: Pass a mutable string to CreateProcess.
authorGerald Combs <gerald@wireshark.org>
Thu, 28 Apr 2016 22:15:32 +0000 (15:15 -0700)
committerGerald Combs <gerald@wireshark.org>
Thu, 28 Apr 2016 23:50:28 +0000 (23:50 +0000)
CreateProcess can modify its second (lpCommandLine) argument. Don't
pass it the output of utf_8to16.

Constify the return value of utf_8to16.

Change-Id: I0d4361396e90c88a4ab2a3f2f0e058230e897fdf
Reviewed-on: https://code.wireshark.org/review/15155
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Gerald Combs <gerald@wireshark.org>
capchild/capture_sync.c
ui/win32/file_dlg_win32.c
wsutil/unicode-utils.c
wsutil/unicode-utils.h

index edd0c5de7a559917020c5293752f8c63eb7a8c09..d1aceee5ebf6cb4156dd19a08cb111369d25f41c 100644 (file)
@@ -367,6 +367,7 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf
     HANDLE signal_pipe;                     /* named pipe used to send messages from parent to child (currently only stop) */
     GString *args = g_string_sized_new(200);
     gchar *quoted_arg;
     HANDLE signal_pipe;                     /* named pipe used to send messages from parent to child (currently only stop) */
     GString *args = g_string_sized_new(200);
     gchar *quoted_arg;
+    gunichar2 *wcommandline;
     SECURITY_ATTRIBUTES sa;
     STARTUPINFO si;
     PROCESS_INFORMATION pi;
     SECURITY_ATTRIBUTES sa;
     STARTUPINFO si;
     PROCESS_INFORMATION pi;
@@ -681,9 +682,10 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf
         g_string_append(args, quoted_arg);
         g_free(quoted_arg);
     }
         g_string_append(args, quoted_arg);
         g_free(quoted_arg);
     }
+    wcommandline = g_utf8_to_utf16(args->str, (glong)args->len, NULL, NULL, NULL);
 
     /* call dumpcap */
 
     /* call dumpcap */
-    if(!CreateProcess(utf_8to16(argv[0]), utf_8to16(args->str), NULL, NULL, TRUE,
+    if(!CreateProcess(utf_8to16(argv[0]), wcommandline, NULL, NULL, TRUE,
                       CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
         report_failure("Couldn't run %s in child process: %s",
                        args->str, win32strerror(GetLastError()));
                       CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
         report_failure("Couldn't run %s in child process: %s",
                        args->str, win32strerror(GetLastError()));
@@ -694,12 +696,15 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf
             g_free( (gpointer) argv[i]);
         }
         g_free( (gpointer) argv);
             g_free( (gpointer) argv[i]);
         }
         g_free( (gpointer) argv);
+        g_string_free(args, TRUE);
+        g_free(wcommandline);
         return FALSE;
     }
     cap_session->fork_child = pi.hProcess;
     /* We may need to store this and close it later */
     CloseHandle(pi.hThread);
     g_string_free(args, TRUE);
         return FALSE;
     }
     cap_session->fork_child = pi.hProcess;
     /* We may need to store this and close it later */
     CloseHandle(pi.hThread);
     g_string_free(args, TRUE);
+    g_free(wcommandline);
 
     cap_session->signal_pipe_write_fd = signal_pipe_write_fd;
 
 
     cap_session->signal_pipe_write_fd = signal_pipe_write_fd;
 
@@ -814,6 +819,7 @@ sync_pipe_open_command(char** argv, int *data_read_fd,
     HANDLE data_pipe[2];                    /* pipe used to send data from child to parent */
     GString *args = g_string_sized_new(200);
     gchar *quoted_arg;
     HANDLE data_pipe[2];                    /* pipe used to send data from child to parent */
     GString *args = g_string_sized_new(200);
     gchar *quoted_arg;
+    gunichar2 *wcommandline;
     SECURITY_ATTRIBUTES sa;
     STARTUPINFO si;
     PROCESS_INFORMATION pi;
     SECURITY_ATTRIBUTES sa;
     STARTUPINFO si;
     PROCESS_INFORMATION pi;
@@ -934,9 +940,10 @@ sync_pipe_open_command(char** argv, int *data_read_fd,
         g_string_append(args, quoted_arg);
         g_free(quoted_arg);
     }
         g_string_append(args, quoted_arg);
         g_free(quoted_arg);
     }
+    wcommandline = g_utf8_to_utf16(args->str, (glong)args->len, NULL, NULL, NULL);
 
     /* call dumpcap */
 
     /* call dumpcap */
-    if(!CreateProcess(utf_8to16(argv[0]), utf_8to16(args->str), NULL, NULL, TRUE,
+    if(!CreateProcess(utf_8to16(argv[0]), wcommandline, NULL, NULL, TRUE,
                       CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
         *msg = g_strdup_printf("Couldn't run %s in child process: %s",
                                args->str, win32strerror(GetLastError()));
                       CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
         *msg = g_strdup_printf("Couldn't run %s in child process: %s",
                                args->str, win32strerror(GetLastError()));
@@ -948,12 +955,15 @@ sync_pipe_open_command(char** argv, int *data_read_fd,
             g_free( (gpointer) argv[i]);
         }
         g_free( (gpointer) argv);
             g_free( (gpointer) argv[i]);
         }
         g_free( (gpointer) argv);
+        g_string_free(args, TRUE);
+        g_free(wcommandline);
         return -1;
     }
     *fork_child = pi.hProcess;
     /* We may need to store this and close it later */
     CloseHandle(pi.hThread);
     g_string_free(args, TRUE);
         return -1;
     }
     *fork_child = pi.hProcess;
     /* We may need to store this and close it later */
     CloseHandle(pi.hThread);
     g_string_free(args, TRUE);
+    g_free(wcommandline);
 #else /* _WIN32 */
     /* Create a pipe for the child process to send us messages */
     if (pipe(sync_pipe) < 0) {
 #else /* _WIN32 */
     /* Create a pipe for the child process to send us messages */
     if (pipe(sync_pipe) < 0) {
index 15eca21a30b63d11f485ae48eafda8cb2f84944e..9d9526c3829b492a726836d3c0efa706e90c9918 100644 (file)
@@ -1438,7 +1438,7 @@ append_file_extension_type(GArray *sa, int et)
     GString* description_str = g_string_new("");
     gchar sep;
     GSList *extensions_list, *extension;
     GString* description_str = g_string_new("");
     gchar sep;
     GSList *extensions_list, *extension;
-    TCHAR *str16;
+    const TCHAR *str16;
     guint16 zero = 0;
 
     /* Construct the list of patterns. */
     guint16 zero = 0;
 
     /* Construct the list of patterns. */
@@ -1471,7 +1471,7 @@ append_file_extension_type(GArray *sa, int et)
 
 static TCHAR *
 build_file_open_type_list(void) {
 
 static TCHAR *
 build_file_open_type_list(void) {
-    TCHAR *str16;
+    const TCHAR *str16;
     int et;
     GArray* sa;
     static const guint16 zero = 0;
     int et;
     GArray* sa;
     static const guint16 zero = 0;
@@ -1565,7 +1565,7 @@ append_file_type(GArray *sa, int ft)
     GString* description_str = g_string_new("");
     gchar sep;
     GSList *extensions_list, *extension;
     GString* description_str = g_string_new("");
     gchar sep;
     GSList *extensions_list, *extension;
-    TCHAR *str16;
+    const TCHAR *str16;
     guint16 zero = 0;
 
     extensions_list = wtap_get_file_extensions_list(ft, TRUE);
     guint16 zero = 0;
 
     extensions_list = wtap_get_file_extensions_list(ft, TRUE);
index 688de928433cc2248ce4072bbd935aceeb553bbe..8ecf85b0c26859d9117010893dcbf6158ce3cd79 100644 (file)
@@ -59,7 +59,7 @@ ws_utf8_char_len(guint8 ch)
  */
 
 /* Convert from UTF-8 to UTF-16. */
  */
 
 /* Convert from UTF-8 to UTF-16. */
-wchar_t *
+const wchar_t *
 utf_8to16(const char *utf8str)
 {
   static wchar_t *utf16buf[3];
 utf_8to16(const char *utf8str)
 {
   static wchar_t *utf16buf[3];
index 865563e231bc9e66124c23d880beac99c7108405..902a4fb4b5e65b921b7e9df9715b35577150ab74 100644 (file)
@@ -55,7 +55,7 @@ int ws_utf8_char_len(guint8 ch);
  * NULL.  The return value should NOT be freed by the caller.
  */
 WS_DLL_PUBLIC
  * NULL.  The return value should NOT be freed by the caller.
  */
 WS_DLL_PUBLIC
-wchar_t * utf_8to16(const char *utf8str);
+const wchar_t * utf_8to16(const char *utf8str);
 
 /** Create a UTF-16 string (in place) according to the format string.
  *
 
 /** Create a UTF-16 string (in place) according to the format string.
  *