Merge pull request #3056 from BrzVlad/fix-multiple-binprot
[mono.git] / mono / mini / mini-exceptions.c
index 46a12c45677b5339236d4dd1f77893f92cee68fe..b154daa8f31c77901cd04760ba9b3f236c20512d 100644 (file)
@@ -1551,6 +1551,8 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
                                        }
                                }
 
+                               MonoError isinst_error;
+                               mono_error_init (&isinst_error);
                                if (ei->flags == MONO_EXCEPTION_CLAUSE_NONE && mono_object_isinst_checked (ex_obj, catch_class, &error)) {
                                        setup_stack_trace (mono_ex, dynamic_methods, initial_trace_ips, &trace_ips);
                                        g_slist_free (dynamic_methods);
@@ -1562,7 +1564,7 @@ mono_handle_exception_internal_first_pass (MonoContext *ctx, MonoObject *obj, gi
                                        MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
                                        return TRUE;
                                }
-                               mono_error_cleanup (&error);
+                               mono_error_cleanup (&isinst_error);
                        }
                }
 
@@ -1695,7 +1697,15 @@ mono_handle_exception_internal (MonoContext *ctx, MonoObject *obj, gboolean resu
                                mono_error_assert_ok (&error);
                        }
                        if (msg == NULL) {
-                               msg = message ? mono_string_to_utf8 ((MonoString *) message) : g_strdup ("(System.Exception.Message property not available)");
+                               if (message) {
+                                       msg = mono_string_to_utf8_checked ((MonoString *) message, &error);
+                                       if (!is_ok (&error)) {
+                                               mono_error_cleanup (&error);
+                                               msg = g_strdup ("(error while display System.Exception.Message property)");
+                                       }
+                               } else {
+                                       msg = g_strdup ("(System.Exception.Message property not available)");
+                               }
                        }
                        g_print ("[%p:] EXCEPTION handling: %s.%s: %s\n", (void*)mono_native_thread_id_get (), mono_object_class (obj)->name_space, mono_object_class (obj)->name, msg);
                        g_free (msg);
@@ -2829,8 +2839,14 @@ mono_invoke_unhandled_exception_hook (MonoObject *exc)
                MonoString *str = mono_object_to_string (exc, &other);
                char *msg = NULL;
                
-               if (str)
-                       msg = mono_string_to_utf8 (str);
+               if (str) {
+                       MonoError inner_error;
+                       msg = mono_string_to_utf8_checked (str, &inner_error);
+                       if (!is_ok (&inner_error)) {
+                               msg = g_strdup_printf ("Nested exception while formatting original exception");
+                               mono_error_cleanup (&inner_error);
+                       }
+               }
                else if (other) {
                        char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
                        char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other);