Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 19, 2025

  • Explore repository and understand the issue
  • Locate the ArrayBufferWriter.cs file with the bug
  • Locate the existing test files
  • Build baseline (clr+libs) successfully
  • Fix the ArgumentException constructor call in ArrayBufferWriter.cs
  • Add a test to verify ParamName is set correctly
  • Build and test the changes - all 52,109 tests passed
  • Run CodeQL security check - no issues found
  • Address PR feedback to use AssertExtensions.Throws

Summary

Fixed the ArgumentException thrown by ArrayBufferWriter<T> when GetSpan() or GetMemory() is called with a negative sizeHint.

Problem: The exception was incorrectly constructed, causing the parameter name to appear in the Message property instead of the ParamName property.

Solution: Changed throw new ArgumentException(nameof(sizeHint)) to throw new ArgumentException(null, nameof(sizeHint)) in CheckAndResizeBuffer() method.

Testing: Updated the InvalidGetMemoryAndSpan() test to use AssertExtensions.Throws which verifies both the exception type and the ParamName property.

Security Summary

No security vulnerabilities were discovered or introduced by these changes.

Original prompt

This section details on the original issue you should resolve

<issue_title>ArrayBufferWriter throws an ArgumentException with the message 'sizeHint'</issue_title>
<issue_description>### Description

The ArgumentException thrown by ArrayBufferWriter has a message string with only sizeHint.
This issue occurs in the GetSpan and the GetMemory method.

Unhandled exception. System.ArgumentException: sizeHint 👈
   at System.Buffers.ArrayBufferWriter`1.CheckAndResizeBuffer(Int32 sizeHint)
   at System.Buffers.ArrayBufferWriter`1.GetSpan(Int32 sizeHint)
   at Program.<Main>$(String[] args) in /home/smdn/temp/sizehintexception/Program.cs:line 5

This is caused by an incorrect specification of the constructor arguments in the code that throws an ArgumentException, as shown below.
As a result, the parameter name that should have been set in the ParamName property will been mistakenly set in the Message property.

public Span<T> GetSpan(int sizeHint = 0)
{
CheckAndResizeBuffer(sizeHint);
Debug.Assert(_buffer.Length > _index);
return _buffer.AsSpan(_index);
}
private void CheckAndResizeBuffer(int sizeHint)
{
if (sizeHint < 0)
throw new ArgumentException(nameof(sizeHint));

Reproduction Steps

minimal repro code:

using System.Buffers;

var writer = new ArrayBufferWriter<byte>();

try {
  writer.GetSpan(sizeHint: -1);
}
catch (ArgumentException ex) {
  Console.WriteLine($"GetSpan ArgumentException Message: '{ex.Message}', ParamName: '{ex.ParamName}'");
}

try {
  writer.GetMemory(sizeHint: -1);
}
catch (ArgumentException ex) {
  Console.WriteLine($"GetMemory ArgumentException Message: '{ex.Message}', ParamName: '{ex.ParamName}'");
}

Expected behavior

Both Message and ParamName should be set correctly.

GetSpan ArgumentException Message: 'sizeHint is negative.', ParamName: 'sizeHint'
GetMemory ArgumentException Message: 'sizeHint is negative.', ParamName: 'sizeHint'

Actual behavior

The string sizeHint is wrongly set to Message, and nothing is set to ParamName.

GetSpan ArgumentException Message: 'sizeHint', ParamName: ''
GetMemory ArgumentException Message: 'sizeHint', ParamName: ''

Regression?

This issue seems to have existed since the code was first added.

Known Workarounds

No response

Configuration

`dotnet --info`` output:

.NET SDK:
 Version:           8.0.121
 Commit:            5b9595625d
 Workload version:  8.0.100-manifests.21648041

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  24.04
 OS Platform: Linux
 RID:         ubuntu.24.04-x64
 Base Path:   /usr/lib/dotnet/sdk/8.0.121/

.NET workloads installed:
 Workload version: 8.0.100-manifests.21648041
There are no installed workloads to display.

Host:
  Version:      8.0.21
  Architecture: x64
  Commit:       362ab6669d

.NET SDKs installed:
  8.0.121 [/usr/lib/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.21 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.21 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]

Other information

No response</issue_description>

<agent_instructions>Please fix the issue if it still exists, but only commit a test if you can write it without relying on the exact error message, as it may be localized. </agent_instructions>

Comments on the Issue (you are @copilot in this section)

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.

@Copilot Copilot AI self-assigned this Oct 19, 2025
@Copilot Copilot AI changed the title [WIP] Fix ArgumentException message in ArrayBufferWriter Fix ArgumentException parameter name in ArrayBufferWriter Oct 19, 2025
@Copilot Copilot AI requested a review from stephentoub October 19, 2025 14:25
Copilot finished work on behalf of stephentoub October 19, 2025 14:25
@stephentoub stephentoub marked this pull request as ready for review October 19, 2025 14:37
@Copilot Copilot AI review requested due to automatic review settings October 19, 2025 14:37
Copy link
Contributor

@Copilot Copilot AI left a 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 ArgumentException constructor call to properly set the parameter name
  • Enhances existing tests to verify the ParamName property 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

@Copilot Copilot AI requested a review from stephentoub October 19, 2025 14:59
Copilot finished work on behalf of stephentoub October 19, 2025 14:59
Copy link
Contributor

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

@stephentoub stephentoub enabled auto-merge (squash) October 20, 2025 02:55
@stephentoub stephentoub merged commit ef584b4 into main Oct 20, 2025
87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ArrayBufferWriter throws an ArgumentException with the message 'sizeHint'

4 participants