jstedfast/MailKit · error · ArgumentOutOfRangeException
count
Error message
count
What it means
ComputeHash throws ArgumentOutOfRangeException("count") when count is negative or offset+count extends past the buffer (the guard compares offset > buffer.Length - count). A wrong offset can therefore also raise this error.
Solutions
- Ensure count >= 0 and offset + count <= buffer.Length before calling
- Recompute count from the actual data length
- Catch ArgumentOutOfRangeException and validate the length source (e.g. header parse)
Example fix
// before hmac.ComputeHash(buf, 0, declaredLength); // untrusted length // after int count = Math.Min(declaredLength, buf.Length); if (count >= 0) hmac.ComputeHash(buf, 0, count);
Defensive patterns
Strategy: validation
Validate before calling
if (count < 0 || offset + count > buffer.Length) throw new ArgumentException("count out of bounds"); Try / catch
try { hmac.ComputeHash(buf, off, cnt); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "count") { /* recompute count */ } Prevention
- Clamp untrusted lengths to buffer.Length
- Validate parsed header lengths before use
- Bounds-assert count in hashing helpers
When it happens
Trigger: Negative counts, counts exceeding buffer.Length - offset, or count computed against a stale offset.
Common situations: Hashing a sub-range of an NTLM message with length fields taken from an incorrect header parse.
Related errors
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/cd850c79f1218ef4.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Security/Ntlm/HMACMD5.cs:109
{
hash.Init (new KeyParameter (Key));
}
public void Clear ()
{
Dispose (false);
}
public byte[] ComputeHash (byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException (nameof (buffer));
if (offset < 0 || offset > buffer.Length)
throw new ArgumentOutOfRangeException (nameof (offset));
if (count < 0 || offset > buffer.Length - count)
throw new ArgumentOutOfRangeException (nameof (count));
if (disposed)
throw new ObjectDisposedException ("HashAlgorithm");
HashCore (buffer, offset, count);
hashValue = HashFinal ();
return hashValue;
}
public byte[] ComputeHash (byte[] buffer)
{
if (buffer == null)
throw new ArgumentNullException (nameof (buffer));
return ComputeHash (buffer, 0, buffer.Length);
}
View on GitHub (pinned to 9d3859a785)