jstedfast/MailKit · error · ArgumentNullException

buffer

Error message

buffer

What it means

MD4.ComputeHash(buffer, offset, count) throws ArgumentNullException named 'buffer' when the input array is null, and ArgumentOutOfRange when offset/count do not describe a valid sub-range of the buffer. These guards run before any hashing state is touched.

Solutions

  1. Ensure the byte array is allocated before hashing
  2. Validate the (offset, count) window: 0 <= offset <= length and 0 <= count <= length - offset
  3. Pass (buffer, 0, buffer.Length) for whole-array hashing to avoid window bugs

Example fix

// before
byte[] payload = GetPayload (); // may be null
byte[] h = md4.ComputeHash (payload, offset, count);
// after
byte[] payload = GetPayload ();
if (payload == null)
    throw new InvalidOperationException ("payload missing");
int off = Math.Min (offset, payload.Length);
int cnt = Math.Min (count, payload.Length - off);
byte[] h = md4.ComputeHash (payload, off, cnt);
Defensive patterns

Strategy: validation

Validate before calling

if (buffer == null)
    throw new ArgumentException ("buffer required", nameof (buffer));
if (offset < 0 || offset > buffer.Length)
    throw new ArgumentException ("offset out of range", nameof (offset));
if (count < 0 || offset > buffer.Length - count)
    throw new ArgumentException ("count out of range", nameof (count));
byte[] h = md4.ComputeHash (buffer, offset, count);

Type guard

static bool IsValidSlice (byte[]? b, int off, int cnt) => b != null && off >= 0 && off <= b.Length && cnt >= 0 && off + cnt <= b.Length;

Try / catch

try {
    byte[] h = md4.ComputeHash (buffer, offset, count);
} catch (ArgumentException) {
    byte[] h = md4.ComputeHash (buffer ?? Array.Empty<byte> (), 0, buffer?.Length ?? 0);
}

Prevention

When it happens

Trigger: Passing a null byte[]; passing offset < 0 or offset > buffer.Length; passing count < 0 or a count where offset > buffer.Length - count (region overruns the buffer).

Common situations: NTLM message construction where the payload bytes were never produced (null); offsets/counts taken from malformed protocol fields; off-by-one in slice arithmetic.

Related errors


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

Appendix: source

Thrown at MailKit/Security/Ntlm/MD4.cs:290

			HH (ref a, b, c, d, x[ 1], S31); /* 41 */
			HH (ref d, a, b, c, x[ 9], S32); /* 42 */
			HH (ref c, d, a, b, x[ 5], S33); /* 43 */
			HH (ref b, c, d, a, x[13], S34); /* 44 */
			HH (ref a, b, c, d, x[ 3], S31); /* 45 */
			HH (ref d, a, b, c, x[11], S32); /* 46 */
			HH (ref c, d, a, b, x[ 7], S33); /* 47 */
			HH (ref b, c, d, a, x[15], S34); /* 48 */

			state [0] += a;
			state [1] += b;
			state [2] += c;
			state [3] += d;
		}

		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 (nameof (MD4));

			HashCore (buffer, offset, count);
			hashValue = HashFinal ();
			Initialize ();

			return hashValue;
		}

		public byte[] ComputeHash (byte[] buffer)

View on GitHub (pinned to 9d3859a785)