[loader] Add descriptive error messages to class failure.
[mono.git] / mono / metadata / process.c
index dd4ac82ee77f6dd8bb4a9207103b69333f19a6ca..96814db8480518f5f35f83caea8c66f18a9ec520 100644 (file)
@@ -6,6 +6,7 @@
  *
  * Copyright 2002 Ximian, Inc.
  * Copyright 2002-2006 Novell, Inc.
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include <config.h>
@@ -26,6 +27,7 @@
 #include <mono/io-layer/io-layer.h>
 /* FIXME: fix this code to not depend so much on the internals */
 #include <mono/metadata/class-internals.h>
+#include <mono/utils/w32handle.h>
 
 #define LOGDEBUG(...)  
 /* define LOGDEBUG(...) g_message(__VA_ARGS__)  */
@@ -487,16 +489,10 @@ ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject *this_obj,
        guint32 count = 0, module_count = 0, assembly_count = 0;
        guint32 i, num_added = 0;
        GPtrArray *assemblies = NULL;
-       static HANDLE current_process = 0;
-       
-       if (current_process == 0) {
-               int pid = mono_process_current_pid ();
-               current_process = ves_icall_System_Diagnostics_Process_GetProcess_internal (pid);
-       }
 
        stash_system_assembly (this_obj);
 
-       if (process == current_process) {
+       if (GetProcessId (process) == mono_process_current_pid ()) {
                assemblies = get_domain_assemblies (mono_domain_get ());
                assembly_count = assemblies->len;
        }
@@ -506,7 +502,9 @@ ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject *this_obj,
        }
 
        count = module_count + assembly_count; 
-       temp_arr = mono_array_new (mono_domain_get (), mono_class_get_process_module_class (), count);
+       temp_arr = mono_array_new_checked (mono_domain_get (), mono_class_get_process_module_class (), count, &error);
+       if (mono_error_set_pending_exception (&error))
+               return NULL;
 
        for (i = 0; i < module_count; i++) {
                if (GetModuleBaseName (process, mods[i], modname, MAX_PATH) &&
@@ -538,7 +536,9 @@ ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject *this_obj,
                arr = temp_arr;
        } else {
                /* shorter version of the array */
-               arr = mono_array_new (mono_domain_get (), mono_class_get_process_module_class (), num_added);
+               arr = mono_array_new_checked (mono_domain_get (), mono_class_get_process_module_class (), num_added, &error);
+               if (mono_error_set_pending_exception (&error))
+                       return NULL;
 
                for (i = 0; i < num_added; i++)
                        mono_array_setref (arr, i, mono_array_get (temp_arr, MonoObject*, i));
@@ -688,6 +688,7 @@ ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoProcessStartIn
 MonoBoolean
 ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoProcessStartInfo *proc_start_info, HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle, MonoProcInfo *process_info)
 {
+       MonoError error G_GNUC_UNUSED;
        gboolean ret;
        gunichar2 *dir;
        STARTUPINFO startinfo={0};
@@ -722,7 +723,11 @@ ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoProcessStartInf
        free_shell_path = FALSE;
        if (cmd) {
                gchar *newcmd, *tmp;
-               tmp = mono_string_to_utf8 (cmd);
+               tmp = mono_string_to_utf8_checked (cmd, &error);
+               if (mono_error_set_pending_exception (&error)) {
+                       g_free (spath);
+                       return NULL;
+               }
                newcmd = g_strdup_printf ("%s %s", spath, tmp);
                cmd = mono_string_new_wrapper (newcmd);
                g_free (tmp);
@@ -849,6 +854,7 @@ MonoArray *
 ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
 {
 #if !defined(HOST_WIN32)
+       MonoError error;
        MonoArray *procs;
        gpointer *pidarray;
        int i, count;
@@ -858,7 +864,11 @@ ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
                mono_set_pending_exception (mono_get_exception_not_supported ("This system does not support EnumProcesses"));
                return NULL;
        }
-       procs = mono_array_new (mono_domain_get (), mono_get_int32_class (), count);
+       procs = mono_array_new_checked (mono_domain_get (), mono_get_int32_class (), count, &error);
+       if (mono_error_set_pending_exception (&error)) {
+               g_free (pidarray);
+               return NULL;
+       }
        if (sizeof (guint32) == sizeof (gpointer)) {
                memcpy (mono_array_addr (procs, guint32, 0), pidarray, count * sizeof (gint32));
        } else {
@@ -869,6 +879,7 @@ ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
 
        return procs;
 #else
+       MonoError error;
        MonoArray *procs;
        gboolean ret;
        DWORD needed;
@@ -896,7 +907,12 @@ ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
        } while (TRUE);
 
        count = needed / sizeof (guint32);
-       procs = mono_array_new (mono_domain_get (), mono_get_int32_class (), count);
+       procs = mono_array_new_checked (mono_domain_get (), mono_get_int32_class (), count, &error);
+       if (mono_error_set_pending_exception (&error)) {
+               g_free (pids);
+               return NULL;
+       }
+
        memcpy (mono_array_addr (procs, guint32, 0), pids, needed);
        g_free (pids);
        pids = NULL;