From 264f1a1ec2526c54dd4be36aa9ef6feee6f4eaa9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:01:17 +0000 Subject: [PATCH 1/4] Initial plan From 9a45d72f51a69d80a8fa2faddc48039b4e499f34 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:37:02 +0000 Subject: [PATCH 2/4] Improve OSFileStreamStrategy Seek error message for negative positions Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- .../src/System/IO/Strategies/OSFileStreamStrategy.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs index 7e83e6ca80941e..bd7f9fef10ed28 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs @@ -158,8 +158,8 @@ public sealed override long Seek(long offset, SeekOrigin origin) } else { - // keep throwing the same exception we did when seek was causing actual offset change - FileStreamHelpers.ThrowInvalidArgument(_fileHandle); + // keep throwing the same exception type we did when seek was causing actual offset change + throw new IOException(SR.IO_SeekBeforeBegin); } // Prevent users from overwriting data in a file that was opened in append mode. From c115384a56b809759e3a11a2adee6c4ba5a5b8bb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 17:39:10 +0000 Subject: [PATCH 3/4] Add test to verify clear error message for negative seek positions Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- .../FileStream/Seek.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs index d934fa2b7730de..8147d29379d7ac 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs @@ -7,6 +7,31 @@ namespace System.IO.Tests { public class FileStream_Seek : FileSystemTest { + [Theory] + [InlineData(0)] + [InlineData(10)] + public void SeekNegativePositionThrowsWithClearMessage(int bufferSize) + { + string fileName = GetTestFilePath(); + using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize)) + { + fs.Write(TestBuffer, 0, TestBuffer.Length); + + // Seek to negative position from Begin should throw IOException with clear message + IOException ex = Assert.Throws(() => fs.Seek(-1, SeekOrigin.Begin)); + Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase); + + // Seek to negative position from Current should throw IOException with clear message + fs.Position = 5; + ex = Assert.Throws(() => fs.Seek(-10, SeekOrigin.Current)); + Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase); + + // Seek to negative position from End should throw IOException with clear message + ex = Assert.Throws(() => fs.Seek(-fs.Length - 1, SeekOrigin.End)); + Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase); + } + } + [Theory] [InlineData(0)] [InlineData(10)] From ffab470114f06135951bbab57b8403df726b93f7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 11 Oct 2025 20:41:33 +0000 Subject: [PATCH 4/4] Remove test that validates exception message content Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- .../FileStream/Seek.cs | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs index 8147d29379d7ac..d934fa2b7730de 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs @@ -7,31 +7,6 @@ namespace System.IO.Tests { public class FileStream_Seek : FileSystemTest { - [Theory] - [InlineData(0)] - [InlineData(10)] - public void SeekNegativePositionThrowsWithClearMessage(int bufferSize) - { - string fileName = GetTestFilePath(); - using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize)) - { - fs.Write(TestBuffer, 0, TestBuffer.Length); - - // Seek to negative position from Begin should throw IOException with clear message - IOException ex = Assert.Throws(() => fs.Seek(-1, SeekOrigin.Begin)); - Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase); - - // Seek to negative position from Current should throw IOException with clear message - fs.Position = 5; - ex = Assert.Throws(() => fs.Seek(-10, SeekOrigin.Current)); - Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase); - - // Seek to negative position from End should throw IOException with clear message - ex = Assert.Throws(() => fs.Seek(-fs.Length - 1, SeekOrigin.End)); - Assert.Contains("before the beginning", ex.Message, StringComparison.OrdinalIgnoreCase); - } - } - [Theory] [InlineData(0)] [InlineData(10)]