Use eglib win32 helper functions without MAX_PATH limit
authorRyan Lucia <rylucia@microsoft.com>
Wed, 18 Sep 2019 19:56:37 +0000 (15:56 -0400)
committerLarry Ewing <lewing@microsoft.com>
Mon, 7 Oct 2019 14:44:41 +0000 (09:44 -0500)
Commit migrated from https://github.com/mono/mono/commit/ca5926fa3fb595c720fb427c7bd74968ebfbeb42

src/mono/mono/metadata/w32process-internals.h
src/mono/mono/metadata/w32process-unix.c
src/mono/mono/metadata/w32process.c

index b5fc4b6..96fd556 100644 (file)
@@ -45,11 +45,11 @@ mono_w32process_get_pid (gpointer handle);
 gboolean
 mono_w32process_try_get_modules (gpointer process, gpointer *modules, guint32 size, guint32 *needed);
 
-gunichar2 *
-mono_w32process_module_get_name (gpointer process, gpointer module, guint32 *len);
+gboolean
+mono_w32process_module_get_name (gpointer process, gpointer module, gunichar2 **str, guint32 *len);
 
-gunichar2 *
-mono_w32process_module_get_filename (gpointer process, gpointer module, guint32 *len);
+gboolean
+mono_w32process_module_get_filename (gpointer process, gpointer module, gunichar2 **str, guint32 *len);
 
 gboolean
 mono_w32process_module_get_information (gpointer process, gpointer module, MODULEINFO *modinfo, guint32 size);
index 1713237..dbb8e95 100644 (file)
@@ -913,8 +913,8 @@ mono_w32process_try_get_modules (gpointer handle, gpointer *modules, guint32 siz
        return TRUE;
 }
 
-gunichar2 *
-mono_w32process_module_get_filename (gpointer handle, gpointer module, guint32 *len)
+gboolean
+mono_w32process_module_get_filename (gpointer handle, gpointer module, gunichar2 **str, guint32 *len)
 {
        gint pid;
        gsize bytes = 0;
@@ -924,26 +924,35 @@ mono_w32process_module_get_filename (gpointer handle, gpointer module, guint32 *
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Getting module file name, process handle %p module %p " G_GUINT32_FORMAT,
                    __func__, handle, module);
 
+       if (str == NULL || len == NULL)
+               return FALSE;
+
+       *str = NULL;
        *len = 0;
 
        pid = mono_w32process_get_pid (handle);
        if (pid == 0)
-               return NULL;
+               return FALSE;
 
        path = mono_w32process_get_path (pid);
        if (path == NULL)
-               return NULL;
+               return FALSE;
 
        proc_path = mono_unicode_from_external (path, &bytes);
+       if (proc_path == NULL) {
+               g_free (path);
+               return FALSE;
+       }
 
+       *str = mono_unicode_from_external (path, &bytes);
        *len = bytes / sizeof (gunichar2);
 
        g_free (path);
-       return proc_path;
+       return TRUE;
 }
 
-gunichar2 *
-mono_w32process_module_get_name (gpointer handle, gpointer module, guint32 *len)
+gboolean
+mono_w32process_module_get_name (gpointer handle, gpointer module, gunichar2 **str, guint32 *len)
 {
        MonoW32Handle *handle_data;
        MonoW32HandleProcess *process_handle;
@@ -958,19 +967,23 @@ mono_w32process_module_get_name (gpointer handle, gpointer module, guint32 *len)
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Getting module base name, process handle %p module %p " G_GUINT32_FORMAT,
                   __func__, handle, module);
 
+       if (str == NULL || len == NULL)
+               return FALSE;
+
+       *str = NULL;
        *len = 0;
 
        if (!mono_w32handle_lookup_and_ref (handle, &handle_data)) {
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown handle %p", __func__, handle);
                mono_w32error_set_last (ERROR_INVALID_HANDLE);
-               return NULL;
+               return FALSE;
        }
 
        if (handle_data->type != MONO_W32TYPE_PROCESS) {
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: unknown process handle %p", __func__, handle);
                mono_w32error_set_last (ERROR_INVALID_HANDLE);
                mono_w32handle_unref (handle_data);
-               return NULL;
+               return FALSE;
        }
 
        process_handle = (MonoW32HandleProcess*) handle_data->specific;
@@ -983,7 +996,7 @@ mono_w32process_module_get_name (gpointer handle, gpointer module, guint32 *len)
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't get modules %p", __func__, handle);
                g_free (pname);
                mono_w32handle_unref (handle_data);
-               return 0;
+               return FALSE;
        }
 
        /* If module != NULL compare the address.
@@ -1025,19 +1038,20 @@ mono_w32process_module_get_name (gpointer handle, gpointer module, guint32 *len)
                        /* bugger */
                        g_free (procname_ext);
                        mono_w32handle_unref (handle_data);
-                       return NULL;
+                       return FALSE;
                }
 
+               *str = procname;
                *len = bytes / sizeof (gunichar2);
 
                g_free (procname_ext);
                mono_w32handle_unref (handle_data);
-               return procname;
+               return TRUE;
        }
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Can't find procname_ext %p", __func__, handle);
        mono_w32handle_unref (handle_data);
-       return NULL;
+       return FALSE;
 }
 
 gboolean
index ea7fb03..0362863 100644 (file)
@@ -33,48 +33,16 @@ mono_w32process_try_get_modules (gpointer process, HMODULE *modules, guint32 siz
        return EnumProcessModules (process, modules, size, needed);
 }
 
-static gunichar2 *
-mono_w32process_module_get_name (gpointer process, gpointer module, guint32 *len)
-{
-       gunichar2 *basename = NULL;
-       guint32 size = 260; // reasonable length to start with given historical behavior
-
-       basename = g_new0 (gunichar2, size);
-       *len = GetModuleBaseNameW (process, (HMODULE)module, basename, size);
-       // GetModuleBaseNameW will set the null byte but include it in the returned length
-       while (*len >= size) {
-               if (*len == 0) {
-                       g_free (basename);
-                       return NULL;
-               }
-               size *= 2; // double the buffer and try again
-               g_free (basename);
-               basename = g_new0 (gunichar2, size);
-               *len = GetModuleBaseNameW (process, (HMODULE)module, basename, size);
-       }
-       return basename;
+static gboolean
+mono_w32process_module_get_name (gpointer process, gpointer module, gunichar2 **str, guint32 *len)
+{
+       return mono_get_module_basename (process, module, str, len);
 }
 
-static gunichar2 *
-mono_w32process_module_get_filename (gpointer process, gpointer module, guint32 *len)
+static gboolean
+mono_w32process_module_get_filename (gpointer process, gpointer module, gunichar2 **str, guint32 *len)
 {
-       gunichar2 *basename = NULL;
-       guint32 size = 260; // reasonable length to start with given historical behavior
-
-       basename = g_new0 (gunichar2, size);
-       *len = GetModuleFileNameExW (process, (HMODULE)module, basename, size);
-       // GetModuleFileNameExW will set the null byte but _not_ include it in the returned length
-       while (*len >= (size - 1)) {
-               if (*len == 0) {
-                       g_free (basename);
-                       return NULL;
-               }
-               size *= 2; // double the buffer and try again
-               g_free (basename);
-               basename = g_new0 (gunichar2, size);
-               *len = GetModuleFileNameExW (process, (HMODULE)module, basename, size);
-       }
-       return basename;
+       return mono_get_module_filename_ex (process, module, str, len);
 }
 
 static gboolean
@@ -595,9 +563,7 @@ ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObjectHandle this_
        return_val_if_nok (error, NULL_HANDLE_ARRAY);
 
        for (i = 0; i < module_count; i++) {
-               modname = mono_w32process_module_get_name (process, mods [i], &modname_len);
-               filename = mono_w32process_module_get_filename (process, mods [i], &filename_len);
-               if (modname && filename) {
+               if (mono_w32process_module_get_name (process, mods [i], &modname, &modname_len) && mono_w32process_module_get_filename (process, mods [i], &filename, &filename_len)) {
                        process_add_module (module, filever, str, process, mods [i], filename, modname, get_process_module_class (), error);
                        if (is_ok (error))
                                MONO_HANDLE_ARRAY_SETREF (temp_arr, num_added++, module);
@@ -644,8 +610,7 @@ ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE process, MonoE
        if (!mono_w32process_try_get_modules (process, &mod, sizeof (mod), &needed))
                return NULL_HANDLE_STRING;
 
-       name = mono_w32process_module_get_name (process, mod, &len);
-       if (!name)
+       if (!mono_w32process_module_get_name (process, mod, &name, &len))
                return NULL_HANDLE_STRING;
 
        MonoStringHandle res = mono_string_new_utf16_handle (mono_domain_get (), name, len, error);