jstedfast/MailKit · error · ArgumentOutOfRangeException

inputCount

Error message

inputCount

What it means

DES.TransformBlock validates inputCount with ArgumentOutOfRangeException: it must be >= 0 and inputOffset + inputCount must fit within inputBuffer (note the guard checks inputOffset > inputBuffer.Length - inputCount). A negative count or a range exceeding the buffer is rejected before any crypto work.

Solutions

  1. Ensure inputCount is 8 for every block (this transform requires full blocks) and that the buffer actually contains 8 bytes at inputOffset.
  2. Handle partial final blocks separately (pad or use TransformFinalBlock).
  3. Validate count against buffer length: inputOffset + inputCount <= inputBuffer.Length.

Example fix

// before
des.TransformBlock(buf, off, 8, out, 0); // fewer than 8 bytes remain

// after
if (buf.Length - off >= 8)
    des.TransformBlock(buf, off, 8, out, 0);
Defensive patterns

Strategy: validation

Validate before calling

int remaining = inputBuffer.Length - inputOffset;
if (remaining >= 8)
    des.TransformBlock(inputBuffer, inputOffset, 8, outputBuffer, 0);

Type guard

static bool FullBlockAvailable(byte[] buf, int off) => buf.Length - off >= 8;

Try / catch

try {
    des.TransformBlock(buf, off, 8, outBuf, 0);
} catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "inputCount") {
    // partial tail: pad or use TransformFinalBlock
}

Prevention

When it happens

Trigger: Calling TransformBlock with inputCount < 0, or with inputCount such that inputOffset + inputCount exceeds inputBuffer.Length (e.g., asking to read 8 bytes from a 5-byte tail without shrinking the count).

Common situations: Processing the final partial block of a stream without handling the leftover bytes; count hardcoded to 8 while the buffer only holds fewer remaining bytes; sign-flipped lengths from int arithmetic overflow.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/26a0e4f9af0e0bdd. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Security/Ntlm/DES.cs:114

			public int InputBlockSize {
				get { return 8; }
			}

			public int OutputBlockSize {
				get { return 8; }
			}

			public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
			{
				if (inputBuffer == null)
					throw new ArgumentNullException ("inputBuffer");

				if (inputOffset < 0 || inputOffset > inputBuffer.Length)
					throw new ArgumentOutOfRangeException ("inputOffset");

				if (inputCount < 0 || inputOffset > inputBuffer.Length - inputCount)
					throw new ArgumentOutOfRangeException ("inputCount");

				if (inputCount != 8)
					throw new ArgumentOutOfRangeException ("inputCount", "Can only transform 8 bytes at a time.");

				if (outputBuffer == null)
					throw new ArgumentNullException ("outputBuffer");

				if (outputOffset < 0 || outputOffset > outputBuffer.Length - 8)
					throw new ArgumentOutOfRangeException ("outputOffset");

				return engine.ProcessBlock (inputBuffer, inputOffset, outputBuffer, outputOffset);
			}

			public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
			{
				if (inputBuffer == null)
					throw new ArgumentNullException ("inputBuffer");

View on GitHub (pinned to 9d3859a785)