Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit 9819624

Browse files
committed
Merge pull request #103 from razzmatazz/bugfix-nre-in-exceptionframe-cs
Fixes a NullReferenceException in ExceptionFrame.cs
2 parents 681e457 + e4e90c6 commit 9819624

4 files changed

Lines changed: 90 additions & 7 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ restore:
99

1010
test: restore
1111
xbuild "./src/SharpRaven.build"
12-
mono --runtime=v4.0.30319 ./src/packages/NUnit.Runners.2.6.4/tools/nunit-console.exe ./src/tests/SharpRaven.UnitTests/bin/Release/net45/SharpRaven.UnitTests.dll -exclude=NuGet,NoMono -nodots
12+
mono --debug --runtime=v4.0.30319 ./src/packages/NUnit.Runners.2.6.4/tools/nunit-console.exe ./src/tests/SharpRaven.UnitTests/bin/Release/net45/SharpRaven.UnitTests.dll -exclude=NuGet,NoMono -nodots

src/app/SharpRaven/Data/ExceptionFrame.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,24 @@ public ExceptionFrame(StackFrame frame)
6060
}
6161

6262
var method = frame.GetMethod();
63+
if (method != null)
64+
{
65+
Module = (method.DeclaringType != null) ? method.DeclaringType.FullName : null;
66+
Function = method.Name;
67+
Source = method.ToString();
68+
}
69+
else
70+
{
71+
// on some platforms (e.g. on mono), StackFrame.GetMethod() may return null
72+
// e.g. for this stack frame:
73+
// at (wrapper dynamic-method) System.Object:lambda_method (System.Runtime.CompilerServices.Closure,object,object))
74+
75+
Module = "(unknown)";
76+
Function = "(unknown)";
77+
Source = "(unknown)";
78+
}
79+
6380
Filename = frame.GetFileName();
64-
Module = (method.DeclaringType != null) ? method.DeclaringType.FullName : null;
65-
Function = method.Name;
66-
Source = method.ToString();
6781
LineNumber = lineNo;
6882
ColumnNumber = frame.GetFileColumnNumber();
6983
}
@@ -206,4 +220,4 @@ public override string ToString()
206220
return sb.ToString();
207221
}
208222
}
209-
}
223+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#region License
2+
3+
// Copyright (c) 2016 The Sentry Team and individual contributors.
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without modification, are permitted
7+
// provided that the following conditions are met:
8+
//
9+
// 1. Redistributions of source code must retain the above copyright notice, this list of
10+
// conditions and the following disclaimer.
11+
//
12+
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13+
// conditions and the following disclaimer in the documentation and/or other materials
14+
// provided with the distribution.
15+
//
16+
// 3. Neither the name of the Sentry nor the names of its contributors may be used to
17+
// endorse or promote products derived from this software without specific prior written
18+
// permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
21+
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22+
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26+
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27+
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
#endregion
30+
31+
using System;
32+
using System.Diagnostics;
33+
using System.Reflection;
34+
35+
using NUnit.Framework;
36+
37+
using SharpRaven.Data;
38+
using SharpRaven.UnitTests.Utilities;
39+
40+
namespace SharpRaven.UnitTests.Data
41+
{
42+
internal class StackFrameWithNullMethod: StackFrame
43+
{
44+
public override MethodBase GetMethod()
45+
{
46+
return null;
47+
}
48+
}
49+
50+
[TestFixture]
51+
public class ExceptionFrameTests
52+
{
53+
[Test]
54+
public void Constructor_NullFrameMethod_DoesNotThrow()
55+
{
56+
// on some platforms (e.g. on mono), StackFrame.GetMethod() may return null
57+
// e.g. for this stack frame:
58+
// at (wrapper dynamic-method) System.Object:lambda_method (System.Runtime.CompilerServices.Closure,object,object))
59+
60+
var stackFrame = new StackFrameWithNullMethod();
61+
var frame = new ExceptionFrame(stackFrame);
62+
63+
Assert.AreEqual("(unknown)", frame.Module);
64+
Assert.AreEqual("(unknown)", frame.Function);
65+
Assert.AreEqual("(unknown)", frame.Source);
66+
}
67+
}
68+
}

src/tests/SharpRaven.UnitTests/SharpRaven.UnitTests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
@@ -77,6 +77,7 @@
7777
<Compile Include="..\..\CommonAssemblyInfo.cs">
7878
<Link>Properties\CommonAssemblyInfo.cs</Link>
7979
</Compile>
80+
<Compile Include="Data\ExceptionFrameTests.cs" />
8081
<Compile Include="Data\JsonPacketFactoryTests.cs" />
8182
<Compile Include="Data\JsonPacketTests.cs" />
8283
<Compile Include="Data\SentryExceptionTests.cs" />
@@ -131,4 +132,4 @@
131132
<Target Name="AfterBuild">
132133
</Target>
133134
-->
134-
</Project>
135+
</Project>

0 commit comments

Comments
 (0)