dotnet / dotnet/wpf

ObjectWriterContext.GetDestinationType may throw null reference exception

Open
#100 0 comments 0 reactions 0 assignees View on GitHub
Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: (e.g. 3.0 Preview1, or daily build number, use `dotnet --info`)
* Windows version: Windows 10 17025
* Does the bug reproduce also in WPF for .NET Framework 4.8?: do not know



**Problem description:**

ObjectWriterContext.GetDestinationType may throw null reference exception when CurrentFrame.Previous is not ObjectWriterFrame.

**Actual behavior:**

The code is in wpf/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs

internal XamlType GetDestinationType()
{
ObjectWriterFrame frame = _stack.CurrentFrame;

if (frame == null)
{
return null;
}

if (frame.Instance != null && frame.XamlType == null) // when true
{
//
// Text/TypeConverter, we need to go up a frame
frame = frame.Previous as ObjectWriterFrame; // when the frame.Previous is not ObjectWriterFrame that the frame is null
}

// for frame is null, calling the frame.Member will break
if (frame.Member == XamlLanguage.Initialization)
{
return frame.XamlType;
}
return frame.Member.Type;
}

https://github.com/dotnet/wpf/blob/bed20df8769a90615fb1a3a8efb3c18611283645/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ObjectWriterContext.cs#L325

**Expected behavior:**

We should check the `frame = frame.Previous as ObjectWriterFrame;` and the frame is not null.

**Minimal repro:**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.