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

[xaml] fix potential NRE in {Binding} #24756

Merged
merged 1 commit into from
Sep 23, 2024

Commits on Sep 21, 2024

  1. [xaml] fix potential NRE in {Binding}

    Fixes: dotnet#24740
    
    Using this in a project:
    
        <PackageReference Include="akgul.Maui.DataGrid" Version="4.0.4" />
    
    Then, in a XAML file:
    
        xmlns:dg="clr-namespace:Maui.DataGrid;assembly=Maui.DataGrid"
        ...
        <dg:DataGrid />
    
    Would crash with:
    
        [0:] [13:46:57 FTL] Inner Exception:
        System.NullReferenceException: Object reference not set to an instance of an object.
        at Microsoft.Maui.Controls.Xaml.BindingExtension.<Microsoft.Maui.Controls.Xaml.IMarkupExtension<Microsoft.Maui.Controls.BindingBase>.ProvideValue>g__CreateBinding|40_0(<>c__DisplayClass40_0& ) in /_/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs:line 46
        at Microsoft.Maui.Controls.Xaml.BindingExtension.Microsoft.Maui.Controls.Xaml.IMarkupExtension<Microsoft.Maui.Controls.BindingBase>.ProvideValue(IServiceProvider serviceProvider) in /_/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs:line 27
        at Maui.DataGrid.DataGrid.InitializeComponent() in D:\Maui.DataGrid\Maui.DataGrid\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\DataGrid.xaml.sg.cs:line 53
        at Maui.DataGrid.DataGrid..ctor() in D:\Maui.DataGrid\Maui.DataGrid\DataGrid.xaml.cs:line 46
        at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Constructor(Object obj, IntPtr* args)
        at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
    
    Inspecting the IL, I see:
    
        BindingBase val115 = ((IMarkupExtension<BindingBase>)(object)val31).ProvideValue((IServiceProvider)null);
    
    So, it is indeed passed a `null` `IServiceProvider`.
    
    Introduce a unit test and check for `null` as a fix.
    jonathanpeppers authored and PureWeen committed Sep 21, 2024
    Configuration menu
    Copy the full SHA
    d43b55c View commit details
    Browse the repository at this point in the history