Skip to content

ArrayBufferWriter throws an ArgumentException with the message 'sizeHint' #120878

@smdn

Description

@smdn

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

Metadata

Metadata

Assignees

Labels

area-System.BuffersenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions