From fc8f5463f693e83b1266cecf73d60fa1e5061315 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 26 Jul 2025 09:04:42 +0000 Subject: [PATCH 1/3] Initial plan From a81f2ace231c6401fd089aa52122a3e9f5ded9ab Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 26 Jul 2025 09:39:12 +0000 Subject: [PATCH 2/3] Fix ArgumentOutOfRangeException in XmlTextReaderImpl.ReadData Co-authored-by: jeffhandley <1031940+jeffhandley@users.noreply.github.com> --- .../src/System/Xml/Core/XmlTextReaderImpl.cs | 5 +++++ .../tests/XmlReader/Tests/ReaderEncodingTests.cs | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs index b169310c64c0c6..8a194858acbb75 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @@ -3418,6 +3418,11 @@ private int ReadData() { _ps.bytesUsed = 0; } + else if (bytesLeft < 0) + { + // This can happen when encoding switch causes bytePos to be calculated incorrectly for malformed data + Throw(SR.Xml_InvalidCharInThisEncoding); + } else { Debug.Assert(_ps.bytes != null); diff --git a/src/libraries/System.Private.Xml/tests/XmlReader/Tests/ReaderEncodingTests.cs b/src/libraries/System.Private.Xml/tests/XmlReader/Tests/ReaderEncodingTests.cs index d5beb6db2532c4..26e5e4da7ea3dd 100644 --- a/src/libraries/System.Private.Xml/tests/XmlReader/Tests/ReaderEncodingTests.cs +++ b/src/libraries/System.Private.Xml/tests/XmlReader/Tests/ReaderEncodingTests.cs @@ -122,5 +122,19 @@ public static void ReadWithIncompleteBytes() XmlException ex = Assert.Throws(() => reader.Read()); Assert.Contains(_invalidCharMessageStart, ex.Message); } + + [Fact] + public static void ReadWithMalformedUtf8InXmlDeclaration() + { + // This test verifies the fix for issue #113061 where malformed UTF-8 sequences + // in XML declaration caused ArgumentOutOfRangeException instead of XmlException + string data = ""; + byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data); + var reader = XmlReader.Create(new MemoryStream(bytes)); + + // Should throw XmlException, not ArgumentOutOfRangeException + XmlException ex = Assert.Throws(() => reader.Read()); + Assert.Contains(_invalidCharInThisEncoding, ex.Message); + } } } From 680d0bcea91c7b2b0d8ee8490fad729a0c5102f4 Mon Sep 17 00:00:00 2001 From: Jeff Handley Date: Sat, 26 Jul 2025 02:47:08 -0700 Subject: [PATCH 3/3] Clearer reference to the issue in the code comment --- .../tests/XmlReader/Tests/ReaderEncodingTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.Xml/tests/XmlReader/Tests/ReaderEncodingTests.cs b/src/libraries/System.Private.Xml/tests/XmlReader/Tests/ReaderEncodingTests.cs index 26e5e4da7ea3dd..d0b8657d5fff79 100644 --- a/src/libraries/System.Private.Xml/tests/XmlReader/Tests/ReaderEncodingTests.cs +++ b/src/libraries/System.Private.Xml/tests/XmlReader/Tests/ReaderEncodingTests.cs @@ -126,7 +126,7 @@ public static void ReadWithIncompleteBytes() [Fact] public static void ReadWithMalformedUtf8InXmlDeclaration() { - // This test verifies the fix for issue #113061 where malformed UTF-8 sequences + // This test verifies the fix for issue dotnet/runtime#113061 where malformed UTF-8 sequences // in XML declaration caused ArgumentOutOfRangeException instead of XmlException string data = ""; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);