dotnet/wpf · error · ArgumentException
' ' is not a valid value for ' '.
Error message
'{1}' is not a valid value for '{0}'. What it means
The internal IStream.Read implementation validates that the requested byte count is non-negative before delegating to the underlying unsafe stream. A negative cb yields ArgumentException with SR.InvalidArgumentValue formatting ('cb' is not a valid value).
Solutions
- Ensure the count passed to Read is >= 0, clamping if needed
- Check upstream arithmetic that computes the read length
- Catch ArgumentException around the Read call and log the offending cb value
Example fix
// before stream.Read(buffer, bytesRemaining); // after int cb = Math.Max(0, bytesRemaining); stream.Read(buffer, cb);
Defensive patterns
Strategy: validation
Validate before calling
if (cb < 0) throw new ArgumentOutOfRangeException(nameof(cb));
Try / catch
try { stream.Read(buffer, cb, out read); } catch (ArgumentException ex) when (ex.Message.Contains("cb")) { /* clamp cb and retry */ } Prevention
- Clamp read lengths with Math.Max(0, n)
- Check offset arithmetic feeding cb
- Never use -1 as a byte-count sentinel
When it happens
Trigger: Calling IStream.Read (or code that funnels into it) with cb < 0.
Common situations: Incorrect buffer/length math (e.g. subtracting larger offsets), or marshaling bugs passing negative counts.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Specified argument was out of the range of valid values.
- SR.InvalidTempFileName
- SR.PackagingWriteNotSupported
- SR.ReachPackaging_PartFromDifferentContainer
- SR.ReadBufferTooSmall
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/7f621a5bec35e468.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/NativeCompoundFileAPIs.cs:610
{
MS.Win32.UnsafeNativeMethods.SafeReleaseComObject((object) _unsafeStream);
}
}
finally
{
_unsafeStream = null;
}
}
//
// IStream Implementation
//
void IStream.Read(Byte[] pv, int cb, out int pcbRead)
{
if (cb < 0)
{
throw new ArgumentException(SR.Format(SR.InvalidArgumentValue, "cb", cb.ToString(CultureInfo.InvariantCulture)));
}
_unsafeStream.Read(pv, cb, out pcbRead);
}
void IStream.Write(Byte[] pv, int cb, out int pcbWritten)
{
if (cb < 0)
{
throw new ArgumentException(SR.Format(SR.InvalidArgumentValue, "cb", cb.ToString(CultureInfo.InvariantCulture)));
}
_unsafeStream.Write(pv, cb, out pcbWritten);
}
// IStream portionView on GitHub (pinned to 81131a70a4)