Bug 33573

Summary: Function call in fixed get called 3 times.
Product: [Mono] Compilers Reporter: Florian Kummer <kummer>
Component: C#Assignee: Marek Safar <masafa>
Severity: normal CC: kummer, mono-bugs+mono
Priority: ---    
Version: 4.0.0   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

Description Florian Kummer 2015-09-02 13:46:30 UTC
I use the following code:

using System;

namespace Error {
  class MainClass {
    public static double[] GetTempBuffer() {
      Console.WriteLine("GetTempBuffer ... ");
      return new double[4];

    public static void Main(string[] args) {
      unsafe {
        //double[] _dummy = GetTempBuffer(); // works
        //fixed(double* dummy = _dummy) {    // works
        fixed(double* dummy = GetTempBuffer()) {    // fails: ges called 3 times !!!!

When i compile with mcs and execute I get the following output:
GetTempBuffer ... 
GetTempBuffer ... 
GetTempBuffer ... 

Compiling with csc under Windows gives the correct result for both, .NET and mono execution.
The problem seems to happen during compilation; the disassemble of the mcs looks as:


.method public hidebysig static void  Main(string[] args) cil managed
  // Code size       45 (0x2d)
  .maxstack  2
  .locals init (float64* pinned V_0)
  IL_0000:  call       float64[] Error.MainClass::GetTempBuffer()
  IL_0005:  brfalse    IL_0016
  IL_000a:  call       float64[] Error.MainClass::GetTempBuffer()
  IL_000f:  ldlen
  IL_0010:  conv.i4
  IL_0011:  brtrue     IL_001d
  IL_0016:  ldc.i4.0
  IL_0017:  conv.u
  IL_0018:  br         IL_0028
  IL_001d:  call       float64[] Error.MainClass::GetTempBuffer()
  IL_0022:  ldc.i4.0
  IL_0023:  ldelema    [mscorlib]System.Double
  IL_0028:  stloc.0
  IL_0029:  ldc.i4.0
  IL_002a:  conv.u
  IL_002b:  stloc.0
  IL_002c:  ret
} // end of method MainClass::Main
Comment 1 Florian Kummer 2015-09-03 05:48:07 UTC
*********$ mcs --version
Mono C# compiler version
Comment 2 Marek Safar 2015-09-03 07:07:02 UTC
Fixed in master and Mono 4.2