Bug 14587 - MCS bombs on NullReferenceException because of unreachability issue
Summary: MCS bombs on NullReferenceException because of unreachability issue
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
: 15730 ()
Depends on:
Blocks:
 
Reported: 2013-09-10 04:53 UTC by Shay Rojansky
Modified: 2013-10-27 03:55 UTC (History)
2 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:
RESOLVED FIXED

Description Shay Rojansky 2013-09-10 04:53:11 UTC
Trying to compile the code below on mono trunk (3.2.3, master/6531ed7) on Linux bombs with NullReferenceException.

Investigating this a little, it seems that the specific trigger is a switch located in an unreachable section, before a statement that refers to a local variable from before the switch. In other words, in the code below, the compiler correctly deletes the unreachable declaration of s, but for some reason statements after the switch are considered reachable - and the reference to s breaks.

The code:

using System;

public class Program
{
        public static void Main(string[] args)
        {
               Go();
        }

        public static string Go()
        {
                throw new Exception("FOO");

                var account = "yo";
                if (account == null)
                {
                }

                var s = "yo";

                switch (8)
                {
                case 1:
                case 2:
                        break;
                default:
                        throw new NotSupportedException("BLABLA");
                }

                return s;
        }
}

The output / stack trace:
Program.cs(14,17): warning CS0162: Unreachable code detected
Program.cs(25,25): warning CS0162: Unreachable code detected

Unhandled Exception:
Mono.CSharp.InternalErrorException: Program.cs(10,30): Program.Go() ---> Mono.CSharp.InternalErrorException: Program.cs(11,9): ---> System.NullReferenceException: Object reference not set to an instance of an object
  at IKVM.Reflection.Emit.ILGenerator.Emit (OpCode opc, IKVM.Reflection.Emit.LocalBuilder local) [0x00000] in <filename unknown>:0
  at Mono.CSharp.EmitContext.Emit (OpCode opcode, IKVM.Reflection.Emit.LocalBuilder local) [0x00000] in <filename unknown>:0
  at Mono.CSharp.LocalVariable.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.VariableReference.EmitLoad (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.VariableReference.Emit (Mono.CSharp.EmitContext ec, Boolean leave_copy) [0x00000] in <filename unknown>:0
  at Mono.CSharp.VariableReference.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Return.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.ParametersBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.MethodData.Emit (Mono.CSharp.TypeDefinition parent) [0x00000] in <filename unknown>:0
  at Mono.CSharp.MethodOrOperator.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Method.Emit () [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.CSharp.Method.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.TypeDefinition.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.ClassOrStruct.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Class.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.TypeDefinition.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.ModuleContainer.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.AssemblyDefinitionStatic.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: Mono.CSharp.InternalErrorException: Program.cs(10,30): Program.Go() ---> Mono.CSharp.InternalErrorException: Program.cs(11,9): ---> System.NullReferenceException: Object reference not set to an instance of an object
  at IKVM.Reflection.Emit.ILGenerator.Emit (OpCode opc, IKVM.Reflection.Emit.LocalBuilder local) [0x00000] in <filename unknown>:0
  at Mono.CSharp.EmitContext.Emit (OpCode opcode, IKVM.Reflection.Emit.LocalBuilder local) [0x00000] in <filename unknown>:0
  at Mono.CSharp.LocalVariable.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.VariableReference.EmitLoad (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.VariableReference.Emit (Mono.CSharp.EmitContext ec, Boolean leave_copy) [0x00000] in <filename unknown>:0
  at Mono.CSharp.VariableReference.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Return.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Statement.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.Block.DoEmit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.ExplicitBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.ParametersBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.CSharp.ToplevelBlock.Emit (Mono.CSharp.EmitContext ec) [0x00000] in <filename unknown>:0
  at Mono.CSharp.MethodData.Emit (Mono.CSharp.TypeDefinition parent) [0x00000] in <filename unknown>:0
  at Mono.CSharp.MethodOrOperator.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Method.Emit () [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.CSharp.Method.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.TypeDefinition.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.ClassOrStruct.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Class.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.TypeDefinition.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.NamespaceContainer.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.TypeContainer.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.ModuleContainer.EmitContainer () [0x00000] in <filename unknown>:0
  at Mono.CSharp.AssemblyDefinition.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.AssemblyDefinitionStatic.Emit () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0
Comment 1 Marek Safar 2013-09-24 13:37:07 UTC
Fixed in master
Comment 2 Marek Safar 2013-10-27 03:55:33 UTC
*** Bug 15730 has been marked as a duplicate of this bug. ***