Ignore empty stack frames when printing stack trace from previous location
authorMarek Safar <marek.safar@gmail.com>
Thu, 3 Jan 2013 15:47:06 +0000 (16:47 +0100)
committerMarek Safar <marek.safar@gmail.com>
Thu, 3 Jan 2013 17:40:01 +0000 (18:40 +0100)
mcs/class/corlib/System/Exception.cs
mcs/class/corlib/Test/System.Runtime.ExceptionServices/ExceptionDispatchInfoTest.cs

index 40fb0cfe6bacf2fef3efa47d19af7ce2dcb26bd9..ec1529e082ee4907281fb85a29652385c57d596d 100644 (file)
@@ -188,8 +188,10 @@ namespace System
                        }
                }
 
-               void AddFrames (StringBuilder sb, string newline, string unknown, StackTrace st) {
-                       for (int i = 0; i < st.FrameCount; i++) {
+               bool AddFrames (StringBuilder sb, string newline, string unknown, StackTrace st)
+               {
+                       int i;
+                       for (i = 0; i < st.FrameCount; i++) {
                                StackFrame frame = st.GetFrame (i);
                                if (i == 0)
                                        sb.AppendFormat ("  {0} ", Locale.GetText ("at"));
@@ -214,6 +216,8 @@ namespace System
                                                                         frame.GetFileLineNumber ());
                                }
                        }
+
+                       return i != 0;
                }
 
                public virtual string StackTrace {
@@ -233,7 +237,9 @@ namespace System
                                // Add traces captured using ExceptionDispatchInfo
                                if (captured_traces != null) {
                                        foreach (var t in captured_traces) {
-                                               AddFrames (sb, newline, unknown, t);
+                                               if (!AddFrames (sb, newline, unknown, t))
+                                                       continue;
+
                                                sb.Append (Environment.NewLine);
                                                sb.Append ("--- End of stack trace from previous location where exception was thrown ---");
                                                sb.Append (Environment.NewLine);
@@ -379,11 +385,10 @@ namespace System
                }
 
                // For ExceptionDispatchInfo
-               internal void CaptureTrace () {
+               internal void CaptureTrace ()
+               {
                        if (captured_traces != null) {
-                               StackTrace[] new_traces = new StackTrace [captured_traces.Length + 1];
-                               Array.Copy (captured_traces, new_traces, captured_traces.Length);
-                               captured_traces = new_traces;
+                               Array.Resize (ref captured_traces, captured_traces.Length + 1);
                        } else {
                                captured_traces = new StackTrace [1];
                        }
index 8936dc9cde95e21eb171e9f4ca948c0932a1e5be..3be96507bf4bf421fcaea062f5461051a09f5177 100644 (file)
@@ -73,6 +73,7 @@ namespace MonoTests.System.Runtime.ExceptionServices
                        var orig_stack = orig.StackTrace;
                        try {
                                ed.Throw ();
+                               Assert.Fail ("#0");
                        } catch (Exception e) {
                                var s = e.StackTrace.Split ('\n');
                                Assert.AreEqual (4, s.Length, "#1");
@@ -81,6 +82,18 @@ namespace MonoTests.System.Runtime.ExceptionServices
                        }
                }
 
+               [Test]
+               public void ThrowWithEmptyFrames ()
+               {
+                       var edi = ExceptionDispatchInfo.Capture (new OperationCanceledException ());
+                       try {
+                               edi.Throw ();
+                               Assert.Fail ("#0");
+                       } catch (OperationCanceledException e) {
+                               Assert.IsFalse (e.StackTrace.Contains ("---"));
+                       }
+               }
+
        }
 }