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

XmlReader.Create throws IndexOutOfRangeException #28615

Closed
Tracked by #93172 ...
Metalnem opened this issue Feb 4, 2019 · 3 comments
Closed
Tracked by #93172 ...

XmlReader.Create throws IndexOutOfRangeException #28615

Metalnem opened this issue Feb 4, 2019 · 3 comments
Assignees
Milestone

Comments

@Metalnem
Copy link

Metalnem commented Feb 4, 2019

XmlReader.Create(Stream) method sometimes throws IndexOutOfRangeException (the documentation says that it can only throw ArgumentNullException and SecurityException). Here's the full program that reproduces this:

using System.IO;
using System.Xml;

namespace Test
{
  public class Program
  {
    public static void Main(string[] args)
    {
      var b = new byte[] { 60, 0, 0, 0, 0, 34, 1, 0, 62, 100, 60, 47, 97, 62, 10 };
      XmlReader.Create(new MemoryStream(b));
    }
  }
}

The stack trace:

Exception has occurred: CLR/System.IndexOutOfRangeException
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Private.Xml.dll: 'Index was outside the bounds of the array.'
   at System.Xml.Ucs4Decoder.Ucs4ToUTF16(UInt32 code, Char[] chars, Int32 charIndex)
   at System.Xml.Ucs4Decoder4321.GetFullChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex)
   at System.Xml.Ucs4Decoder.Convert(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex, Int32 charCount, Boolean flush, Int32& bytesUsed, Int32& charsUsed, Boolean& completed)
   at System.Xml.XmlTextReaderImpl.InvalidCharRecovery(Int32& bytesCount, Int32& charsCount)
   at System.Xml.XmlTextReaderImpl.GetChars(Int32 maxCharsCount)
   at System.Xml.XmlTextReaderImpl.ReadData()
   at System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
   at System.Xml.XmlTextReaderImpl.FinishInitStream()
   at System.Xml.XmlTextReaderImpl..ctor(Stream stream, Byte[] bytes, Int32 byteCount, XmlReaderSettings settings, Uri baseUri, String baseUriStr, XmlParserContext context, Boolean closeInput)
   at System.Xml.XmlReaderSettings.CreateReader(Stream input, Uri baseUri, String baseUriString, XmlParserContext inputContext)
   at System.Xml.XmlReader.Create(Stream input)

The environment:

.NET Core SDK (reflecting any global.json):
 Version:   2.2.103
 Commit:    8edbc2570a

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx.10.14-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.2.103/

Found via SharpFuzz.

@danmoseley
Copy link
Member

Same on .NET Framework. @krwq is this by design? If so we should update docs.

@Metalnem do you expect to find more issues with "SharpFuzz"? Sounds interesting. Are you manually comparing exceptions thrown with the docs, or ingesting docs somehow?

@krwq
Copy link
Member

krwq commented Feb 4, 2019

This does not seem by design. Please make sure to look at the perf when fixing this as it might not be worth to add the extra checks.

cc: @buyaa-n

@Metalnem
Copy link
Author

Metalnem commented Feb 5, 2019

@danmosemsft Yes, I expect to find a lot of similar issues across the whole standard library (XmlReader just happened to be the first class I've tested, for no particual reason). I'm currently manually comparing exceptions, but I'll look into automating it in the future (my current goal is to cover the most vulnerable classes in the library first).

@buyaa-n buyaa-n self-assigned this Feb 6, 2019
@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants