-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix ArgumentException parameter name in ArrayBufferWriter #120880
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
Fix ArgumentException parameter name in ArrayBufferWriter #120880
Conversation
Co-authored-by: stephentoub <[email protected]>
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 fixes a bug in ArrayBufferWriter<T> where GetSpan() and GetMemory() methods were throwing ArgumentException with incorrect properties when passed a negative sizeHint parameter.
- Changes the
ArgumentExceptionconstructor call to properly set the parameter name - Enhances existing tests to verify the
ParamNameproperty is correctly set
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs | Fixed ArgumentException constructor to use proper overload that sets ParamName correctly |
| src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs | Enhanced test to verify ParamName property is set to "sizeHint" for both GetSpan and GetMemory methods |
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.T.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <[email protected]>
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
Summary
Fixed the
ArgumentExceptionthrown byArrayBufferWriter<T>whenGetSpan()orGetMemory()is called with a negativesizeHint.Problem: The exception was incorrectly constructed, causing the parameter name to appear in the
Messageproperty instead of theParamNameproperty.Solution: Changed
throw new ArgumentException(nameof(sizeHint))tothrow new ArgumentException(null, nameof(sizeHint))inCheckAndResizeBuffer()method.Testing: Updated the
InvalidGetMemoryAndSpan()test to useAssertExtensions.Throwswhich verifies both the exception type and theParamNameproperty.Security Summary
No security vulnerabilities were discovered or introduced by these changes.
Original prompt
Fixes #120878
💡 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.