Bug 21023 - C# compiler incorrectly handles the case where null is passed in place of a "params object[]" parameter
Summary: C# compiler incorrectly handles the case where null is passed in place of a "...
Status: RESOLVED INVALID
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.4.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-07-01 14:57 UTC by Chris Howie
Modified: 2014-07-01 15:17 UTC (History)
1 user (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 INVALID

Description Chris Howie 2014-07-01 14:57:57 UTC
On Mono 3.4.0, it appears that using null as an argument to Delegate.DynamicInvoke() when the corresponding parameter is a non-nullable value type results in the target delegate receiving the default value for that delegate.

Simple test case:

------------8<------------
using System;

class TestCase
{
    static void Main()
    {
        Delegate d = new Func<int, int>(i => i);

        Console.WriteLine(d.DynamicInvoke(null));
    }
}
------------8<------------

On Mono, this prints 0.  On MS.NET, it crashes with System.Reflection.TargetParameterCountException.
Comment 1 Chris Howie 2014-07-01 14:58:50 UTC
"default value for that delegate" should of course be "default value for that type".
Comment 2 Chris Howie 2014-07-01 15:10:38 UTC
Apparently this is an issue with the compiler, not the runtime.  If you use:

Console.WriteLine(d.DynamicInvoke(new object[] { null }));

Then the behavior is the same on both platforms (0 is printed).  So DynamicInvoke is working correctly; the issue is that the Mono C# compiler does not correctly handle the case where null is passed where "params object[]" is expected.

Microsoft's compiler handles the case by passing a null object[].

Mono's compiler handles the case by passing an object[] with a single null element.
Comment 3 Chris Howie 2014-07-01 15:17:02 UTC
Sorry, I don't think this is a compiler bug after all, but an issue with DynamicInvoke.  I am closing out this mess of a bug report and will re-open a proper one when I have all of the information sorted.