jstedfast/MailKit · error · ArgumentOutOfRangeException
inputCount
Error message
inputCount
What it means
TransformBlock throws ArgumentOutOfRangeException named "inputCount" when inputCount is negative or when inputOffset + inputCount exceeds inputBuffer.Length (check: inputOffset > inputBuffer.Length - inputCount). It enforces that the block to hash lies entirely inside the input array, consistent with ICryptoTransform semantics.
Solutions
- Clamp the final chunk: count = Math.Min(blockSize, inputBuffer.Length - inputOffset).
- Verify count is bytes actually available: require count >= 0 && inputOffset <= inputBuffer.Length - count.
- Fix length sources (use GetByteCount for encoded text, not string Length).
- Validate external counts upstream before the transform loop.
Example fix
// before transform.TransformBlock(buf, offset, blockSize, null, 0); // final short chunk overruns // after int count = Math.Min(blockSize, buf.Length - offset); if (count > 0) transform.TransformBlock(buf, offset, count, null, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (inputCount < 0 || inputOffset > inputBuffer.Length - inputCount)
throw new ArgumentOutOfRangeException(nameof(inputCount)); Type guard
static bool IsValidCount(byte[] buf, int offset, int count) => count >= 0 && offset <= buf.Length - count;
Try / catch
try { transform.TransformBlock(buf, offset, count, null, 0); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "inputCount") { log.LogError(ex, "count {Count} at offset {Offset} exceeds len {Len}", count, offset, buf.Length); throw; } Prevention
- Clamp every chunk with Math.Min(blockSize, buf.Length - offset)
- Use byte counts (GetByteCount) not char counts
- Test the final short chunk explicitly
When it happens
Trigger: TransformBlock(buf, off, -1, null, 0), or TransformBlock(buf, off, len, null, 0) where off + len > buf.Length — e.g. passing full length while also passing a non-zero offset, or a byte-count/char-count mixup.
Common situations: Last-chunk logic computing count as remaining + blockSize; passing Encoding string lengths (chars) instead of byte counts; reusing a constant block size for the final short chunk.
Related errors
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/7ae330c80334d674.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/Security/Ntlm/MD4.cs:348
HashCore (buffer, 0, nread);
} while (nread > 0);
hashValue = HashFinal ();
Initialize ();
return hashValue;
}
public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
if (inputBuffer == null)
throw new ArgumentNullException (nameof (inputBuffer));
if (inputOffset < 0 || inputOffset > inputBuffer.Length)
throw new ArgumentOutOfRangeException (nameof (inputOffset));
if (inputCount < 0 || inputOffset > inputBuffer.Length - inputCount)
throw new ArgumentOutOfRangeException (nameof (inputCount));
if (outputBuffer != null) {
if (outputOffset < 0 || outputOffset > outputBuffer.Length - inputCount)
throw new ArgumentOutOfRangeException (nameof (outputOffset));
}
HashCore (inputBuffer, inputOffset, inputCount);
if (outputBuffer != null)
Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
return inputCount;
}
public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
{
if (inputCount < 0)
throw new ArgumentOutOfRangeException (nameof (inputCount));View on GitHub (pinned to 9d3859a785)