dotnet / dotnet/wpf

OutOfMemoryException in XamlReader.Parse

Open
#312 2 comments 1 reaction 0 assignees View on GitHub
area-System.Xaml Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

The following line of code results in the parser getting stuck in a loop, adding simulated `EndMember` and `EndObject` nodes to a list until it eventually throws an `OutOfMemoryException`.

XamlReader.Parse("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">Invalid Markup</DataTemplate>");
System.OutOfMemoryException : Array dimensions exceeded supported range.

at System.Collections.Generic.List`1.set_Capacity(Int32 value)
at System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
at System.Collections.Generic.List`1.Add(T item)
at System.Xaml.XamlNodeList.Add(XamlNodeType nodeType, Object data)
at System.Xaml.DeferringWriter.WriteEndMember()
at System.Xaml.XamlObjectWriter.WriteEndMember()
at System.Xaml.XamlObjectWriter.Dispose(Boolean disposing)
at System.Xaml.XamlWriter.System.IDisposable.Dispose()
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, Boolean skipJournaledProperties, Uri baseUri)
at System.Windows.Markup.XamlReader.Load(XamlReader xamlReader, ParserContext parserContext)
at System.Windows.Markup.XamlReader.Load(XmlReader reader, ParserContext parserContext, XamlParseMode parseMode)
at System.Windows.Markup.XamlReader.Load(XmlReader reader)
at System.Windows.Markup.XamlReader.Parse(String xamlText)

I understand that the xaml is invalid and would be perfectly happy if it threw a `XamlParseException`, but I think `OutOfMemoryException` is the wrong way to deal with this situation :)

_This issue has been moved from https://developercommunity.visualstudio.com/content/problem/439875/outofmemoryexception-in-xamlreaderparse.html
VSTS ticketId: 781519_
_These are the original issue comments:_
(no comments)
_These are the original issue solutions:_
(no solutions)

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.