Skip to content

Undocumented ArgumentOutOfRangeException in XmlReader created from MemoryStream #113061

Closed

Description

@fuzzah

Description

If an XmlReader object is created via XmlReader.Create(stream) with a MemoryStream having certain data, then an undocumented ArgumentOutOfRangeException may be thrown.

Reproduction Steps

Create a console app:

dotnet new console -n XmlReaderReproduce
cd XmlReaderReproduce

Put the following code in the Program.cs file:

using System.Xml;

string data = "<?xml version=\"1.0\xbf\"?>";

byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(data);
MemoryStream stream = new MemoryStream(byteArray);

try
{
    using (var xml = XmlReader.Create(stream))
    {
        while (xml.Read()) { }
    }
}
catch (XmlException) { }

Build and run the app. In my case:

dotnet publish --self-contained -r linux-x64 -c Release
bin/Release/net8.0/linux-x64/XmlReaderReproduce

Expected behavior

No errors or any other output, as all exceptions related to System.Xml should be caught in the try..catch block

Actual behavior

The app exits with an unhandled ArgumentOutOfRangeException, which is undocumented for the XmlReader class:

Unhandled exception. System.ArgumentOutOfRangeException: count ('-2') must be a non-negative value. (Parameter 'count')
Actual value was -2.
   at System.ArgumentOutOfRangeException.ThrowNegative[T](T value, String paramName)
   at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)
   at System.Xml.XmlTextReaderImpl.ReadData()
   at System.Xml.XmlTextReaderImpl.SwitchEncoding(Encoding newEncoding)
   at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl)
   at System.Xml.XmlTextReaderImpl.Read()
   at Program.<Main>$(String[] args) in /root/XmlReaderReproduce/Program.cs:line 12
Aborted (core dumped)

Regression?

The error did not appear in 6.0.0-rtm.21522.10

Known Workarounds

There is no error if the XmlReader object is created with a StringReader instead:

using System.Xml;

string data = "<?xml version=\"1.0\xbf\"?>";

try
{
    using (var xml = XmlReader.Create(new StringReader(data)))
    {
        while (xml.Read()) { }
    }
}
catch (XmlException) { }

Configuration

I used the Docker image mcr.microsoft.com/dotnet/sdk:8.0.

dotnet info:

dotnet --info
.NET SDK:
 Version:           8.0.406
 Commit:            73c77e847d
 Workload version:  8.0.400-manifests.f7661779
 MSBuild version:   17.11.22+17752cebd

Runtime Environment:
 OS Name:     debian
 OS Version:  12
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/8.0.406/

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.

Host:
  Version:      8.0.13
  Architecture: x64
  Commit:       eba546b0f0

.NET SDKs installed:
  8.0.406 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.13 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.13 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other information

Found by Linux Verification Center (linuxtesting.org) with AFL++ and SharpFuzz.
Reporter: Valery Korolyov ([email protected])
Organization: Garda Technologies ([email protected])

Activity

  1. added
    untriaged
    on Mar 3, 2025
  2. dotnet-policy-service commented on Mar 3, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-xml
    See info in area-owners.md if you want to be subscribed.

  3. added and removed
    untriaged
    on Jul 26, 2025
  4. added this to the Future milestone on Jul 26, 2025
  5. added
    help wanted
    on Jul 26, 2025
  6. added a commit that references this issue on Feb 14, 2026
    05f3072
  7. added a commit that references this issue on Mar 16, 2026
    da46c68
  8. locked and limited conversation to collaborators on Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Xmlhelp wantedregression-from-last-release

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions