Bug 41509 - DLR crashes when trying to convert a object to null.
Summary: DLR crashes when trying to convert a object to null.
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Core ()
Version: 4.2.0 (C6)
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2016-06-04 14:42 UTC by matthi.d
Modified: 2016-09-02 09:04 UTC (History)
2 users (show)

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:

Description matthi.d 2016-06-04 14:42:05 UTC

1. Open Repl
2. Enter:

    public class TestConvert : System.Dynamic.DynamicObject
        public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result)
            result = null;
            return true;

    public class Test : System.Dynamic.DynamicObject
        public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
            result = new TestConvert();
            return true;

        public static void TestMethod()
            dynamic t = new Test();
            string result = t.SomeMethod();

3. Enter:

Test.TestMethod(); // Works on .NET, crashes on mono

Expected: No Exception
System.InvalidCastException: The result type 'null' of the dynamic binding produced by the object with type 'TestConvert' for the binder 'Microsoft.CSharp.RuntimeBinder.CSharpConvertBinder' is not compatible with the result type 'System.String' expected by the call site.
  at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object)
  at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet] (System.Runtime.CompilerServices.CallSite site, System.Dynamic.T0 arg0) <0x406df090 + 0x00520> in <filename unknown>:0
  at Test.TestMethod () <0x406d98e0 + 0x00189> in <filename unknown>:0
  at <InteractiveExpressionClass>.Host (System.Object& $retval) <0x406d98c0 + 0x0000b> in <filename unknown>:0
  at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) <0x405e5ed0 + 0x000e0> in <filename unknown>:0
  at Mono.CSharpShell.Evaluate (System.String input) <0x405e5dc0 + 0x00053> in <filename unknown>:0

Tested with:

matthid@localhost ~ $ mono --version
Mono JIT compiler version 4.2.3 (Stable Sun Apr  3 14:36:23 CEST 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen

I might send a PR for this (if one is welcome). I think the problem is here: https://github.com/mono/mono/blob/master/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/DynamicObject.cs#L505
Comment 1 matthi.d 2016-06-04 14:43:19 UTC
Problem happened here: https://github.com/pythonnet/pythonnet/pull/219
Comment 2 matthi.d 2016-06-04 14:50:58 UTC
By the way. The Reference-Source uses another condition: https://github.com/mono/mono/blob/master/mcs/class/referencesource/System.Core/Microsoft/Scripting/Actions/DynamicObject.cs#L507
Comment 3 Marek Safar 2016-09-02 09:04:34 UTC
Fixed in master and Mono 4.6