-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Improve error message when seeking to negative position in FileStream #120626
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
Conversation
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves the error message when attempting to seek to a negative position in a FileStream by replacing a generic system error with a clear, descriptive message that helps developers quickly identify the issue.
- Replaces unhelpful "The parameter is incorrect" message with "An attempt was made to move the position before the beginning of the stream"
- Maintains backward compatibility by keeping the same IOException type
- Adds comprehensive test coverage to verify the improved error message across all SeekOrigin modes
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs |
Updates error handling to throw IOException with descriptive message using existing localized resource |
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs |
Adds test method to verify the improved error message for negative seek operations |
Tagging subscribers to this area: @dotnet/area-system-io |
Co-authored-by: stephentoub <[email protected]>
Fixes #issue_number
Problem
When attempting to seek to a negative position in a FileStream (e.g.,
fs.Seek(-1, SeekOrigin.Begin)
), users encountered a confusing and unhelpful error message:This generic system error message provided no indication of what was actually wrong, forcing developers to spend time debugging to discover they were attempting to seek before the beginning of the stream.
Solution
Changed the error message to be clear and descriptive:
The fix replaces the call to
FileStreamHelpers.ThrowInvalidArgument(_fileHandle)
with a direct throw ofIOException
using the existingSR.IO_SeekBeforeBegin
localized resource string. This makes FileStream's error messaging consistent with other stream implementations likeMemoryStream
,UnmanagedMemoryStream
, andReadOnlyMemoryStream
, which already use this same message.Changes
OSFileStreamStrategy.Seek
to throwIOException
with theIO_SeekBeforeBegin
message when the calculated position is negativeTesting
This improvement helps developers quickly identify and fix issues with negative seek operations, significantly reducing debugging time.
Fixes #112112
Original prompt
Fixes #112112
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.