Bug 17975 - Internal compiler error due to NullReferenceException when catching exception type with type parameters
Summary: Internal compiler error due to NullReferenceException when catching exception...
Status: VERIFIED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-02-24 14:45 UTC by Richard Cook
Modified: 2014-02-25 13:17 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Richard Cook 2014-02-24 14:45:28 UTC
Source code that catches exception classes with type parameters cause a NullReferenceException and a resulting internal compiler error (CS0584). The stack trace is roughly as follows:

Internal compiler error details: System.NullReferenceException: Object reference not set to an instance of an object
  at Mono.CSharp.TypeSpec.IsBaseClass (Mono.CSharp.TypeSpec type, Mono.CSharp.TypeSpec baseClass, Boolean dynamicIsObject) [0x00000] in <filename unknown>:0
  at Mono.CSharp.TryCatch.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Block.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.ParametersBlock.Resolve (Mono.CSharp.FlowBranching parent, Mono.CSharp.BlockContext rc, IMethodData md) [0x00000] in <filename unknown>:0
    {

Here's a simple reproducer:

===== Test.cs =====
     1	class ExceptionWithTypeParameter<T> : System.Exception { }
     2	
     3	class Test
     4	{
     5	    void M()
     6	    {
     7	        try { }
     8	        catch (ExceptionWithTypeParameter<int> e) { }
     9	        catch { }
    10	    }
    11	}
    12	
===== End of Test.cs =====

This code works in pre-3.x.y series of mcs such as 2.10.8.1:

===== mcs output =====
> mcs --version
Mono C# compiler version 2.10.8.1
[No errors]
===== End of mcs output =====

I've tested several of the 3.x.y series of mcs compiler which all fail in the same way:

===== mcs output =====
> mcs --version
Mono C# compiler version 3.0.7.0
bz61270.cs(6,5): error CS0584: Internal compiler error: Object reference not set to an instance of an object
Compilation failed: 1 error(s), 0 warnings

===== End of mcs output =====

Thanks, Richard.

Richard Cook | Principal engineer
Coverity | Columbia Center Tower | 701 Fifth Avenue, Suite 1220 | Seattle, WA 98104
The Leader in Development Testing
Read our profile in Forbes, Coverity Gets Code Right 25% Faster
Comment 1 Richard Cook 2014-02-24 15:30:33 UTC
I believe the fix will look something like the following:

===== diff =====
--- a/mcs/mcs/typespec.cs
+++ b/mcs/mcs/typespec.cs
@@ -618,12 +618,15 @@ namespace Mono.CSharp
                 //
                 // class B : A<object> {}
                 //
-                type = type.BaseType;
-                while (type != null) {
-                    if (TypeSpecComparer.IsEqual (type, baseClass))
-                        return true;
-
+                if (type != null)
+                {
                     type = type.BaseType;
+                    while (type != null) {
+                        if (TypeSpecComparer.IsEqual (type, baseClass))
+                            return true;
+
+                        type = type.BaseType;
+                    }
                 }
 
                 return false;
===== End of diff =====
Comment 2 Marek Safar 2014-02-24 17:09:47 UTC
Fixed in master