dotnet/wpf · error · ArgumentException
Cannot set seek pointer to a negative position.
Error message
Cannot set seek pointer to a negative position.
What it means
After resolving the requested position against the given SeekOrigin, NetStream.Seek checks the computed target and throws ArgumentException (SR.SeekNegative) when it is below zero. This library throws it because stream positions are non-negative.
Solutions
- Clamp the target with Math.Max(0, currentPos + offset) before seeking
- Track the current position (stream.Position) and only seek back as far as 0
- For relative movement, guard against negative results: if (pos + offset < 0) offset = -pos;
Example fix
// before stream.Seek(currentOffset - recordSize, SeekOrigin.Begin); // after long target = Math.Max(0, currentOffset - recordSize); stream.Seek(target, SeekOrigin.Begin);
Defensive patterns
Strategy: validation
Validate before calling
long target = origin switch {
SeekOrigin.Begin => offset,
SeekOrigin.Current => stream.Position + offset,
SeekOrigin.End => stream.Length + offset,
_ => throw new ArgumentException("invalid origin") };
if (target < 0) target = 0;
stream.Seek(target, SeekOrigin.Begin); Type guard
static long ClampPosition(long p) => Math.Max(0, p);
Try / catch
try { stream.Seek(offset, origin); } catch (ArgumentException) { stream.Seek(0, SeekOrigin.Begin); } Prevention
- Clamp computed seek targets to >= 0
- Track current position before relative rewinds
- Write a SafeSeek helper and use it everywhere instead of raw Seek
When it happens
Trigger: Calling Seek with an offset that computes to a negative position, e.g. Seek(-100, SeekOrigin.Begin) or Seek(-1000, SeekOrigin.Current) when the current position is less than 1000.
Common situations: Rewinding farther than the amount read so far; subtracting record sizes when parsing without tracking position; copying offset arithmetic from another stream implementation with different clamping semantics.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- SeekOrigin value is not valid.
- A read or write operation references a location outside the…
- ArgumentOutOfRangeException (Parameter 'origin')
- can’t seek on baseStream
- Cannot access Stream object because it was closed or…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/85bd29f793f6fa2c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs:256
temp = _position + offset;
break;
}
case SeekOrigin.End:
{
temp = Length + offset;
break;
}
default:
{
throw new ArgumentOutOfRangeException(nameof(origin), SR.SeekOriginInvalid);
}
}
}
if (temp < 0)
{
throw new ArgumentException(SR.SeekNegative);
}
#if DEBUG
if (System.IO.Packaging.PackWebRequestFactory._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation("NetStream.Seek() pos:{0}", temp);
#endif
_position = temp;
return _position;
}
/// <summary>
/// Logical byte position in this stream
/// </summary>
public override long Position
{
get
{View on GitHub (pinned to 81131a70a4)