Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing an unitialized array throws a NullReferenceException but at the wrong line #42994

Closed
vsfeedback opened this issue Apr 1, 2020 · 7 comments
Assignees
Labels
Area-Compilers Area-External Resolution-Duplicate The described behavior is tracked in another issue

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


Consider the following code to reproduce:

static string[] SomeArray = null;

static void Main(string[] args)
{
    SomeArray[1] = string. Empty; // Line #15
    DoSomething(); // Line #16
}
static void DoSomething() { }

Obviously, SomeArray is never initialized and trying to access it (at line #15) would throw a NullReferenceException. However, the debugger shows the exception at the next line (line #16) and the stack trace is as follows:

> ConsoleApp1.exe! ConsoleApp1.Program.Main(string[] args) Line 16 C#

I was able to reproduce this on both VS2017 and VS2019.


Original Comments

Visual Studio Feedback System on 3/31/2020, 00:55 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

@YairHalberstadt
Copy link
Contributor

Is this a debug or release build?

@jcouv
Copy link
Member

jcouv commented Apr 3, 2020

I can repro this (16.6p2, Debug configuration for the project). The highlight for the breakpoint currently shows DoSomething() which is incorrect.
It could be a sequence point or a debugger issue.

@jcouv
Copy link
Member

jcouv commented Apr 3, 2020

Tagging @cshung
FYI @gafter @chuckries

I verified that the sequence points are produced as I expect (see repro below).
This feels much like this previously reported issue: #33995 where the thrown exception is shown one instruction too late.

        [Fact, WorkItem(42994, "https://github.com/dotnet/roslyn/issues/42994")]
        public void Repro()
        {
            string source = @"
class C
{
    static string[] SomeArray = null;

    static void M()
    {
        SomeArray[1] = string. Empty;
        DoSomething();
    }
    static void DoSomething() { }
}
";
            var compilation = CompileAndVerify(source);

            compilation.VerifyIL("C.M",
@"
{
  // Code size       18 (0x12)
  .maxstack  3
  // sequence point: SomeArray[1] = string. Empty;
  IL_0000:  ldsfld     ""string[] C.SomeArray""
  IL_0005:  ldc.i4.1
  IL_0006:  ldsfld     ""string string.Empty""
  IL_000b:  stelem.ref
  // sequence point: DoSomething();
  IL_000c:  call       ""void C.DoSomething()""
  // sequence point: }
  IL_0011:  ret
}
", sequencePoints: "C.M", source: source);
        }

@cshung
Copy link
Member

cshung commented Apr 7, 2020

@dotnet/dotnet-diag

@tommcdon
Copy link
Member

tommcdon commented Apr 8, 2020

@mikem8361 seems similar to dotnet/runtime#34226

@mikem8361
Copy link
Member

mikem8361 commented Apr 8, 2020 via email

@jcouv
Copy link
Member

jcouv commented Apr 8, 2020

Thanks. Closing as duplicate of dotnet/runtime#34226

@jcouv jcouv closed this as completed Apr 8, 2020
@jcouv jcouv added the Resolution-Duplicate The described behavior is tracked in another issue label Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Area-External Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

6 participants