jstedfast/MailKit · error · ArgumentNullException

inputStream

Error message

inputStream

What it means

ComputeHash(Stream inputStream) throws ArgumentNullException named "inputStream" when the stream is null. Like the byte[] overload, MD4 validates the parameter up front before reading any data, matching standard HashAlgorithm behavior for streaming hashes.

Solutions

  1. Ensure the stream is opened successfully and non-null before hashing; propagate open failures instead of swallowing them.
  2. Guard with if (stream != null) or throw a descriptive exception for missing input at a higher level.
  3. If null means "no data", decide explicitly: either skip hashing or hash an empty byte array via ComputeHash(Array.Empty<byte>()).
  4. Catch ArgumentNullException at the API boundary to report missing input clearly.

Example fix

// before
using (var stream = path != null ? File.OpenRead(path) : null)
    hash = md4.ComputeHash(stream); // NRE/ANG when path == null
// after
if (!File.Exists(path)) throw new FileNotFoundException(path);
using (var stream = File.OpenRead(path))
    hash = md4.ComputeHash(stream);
Defensive patterns

Strategy: validation

Validate before calling

if (inputStream == null) throw new ArgumentNullException(nameof(inputStream));
var hash = md4.ComputeHash(inputStream);

Type guard

static bool CanHash(Stream s) => s != null;

Try / catch

try { hash = md4.ComputeHash(stream); }
catch (ArgumentNullException ex) when (ex.ParamName == "inputStream") { log.LogError(ex, "Stream not opened"); throw; }

Prevention

When it happens

Trigger: Calling md4.ComputeHash(stream) where the stream variable is null — e.g. File.OpenRead failed to be assigned, a factory method returned null, or a nullable property was passed unconditionally.

Common situations: Hashing a file whose open call was wrapped in try/catch that swallowed the error and left the stream null; pipeline code where an optional attachment/stream is absent; DI/factory returning null in tests.

Related errors


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

Appendix: source

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

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

			return hashValue;
		}

		public byte[] ComputeHash (byte[] buffer)
		{
			if (buffer == null)
				throw new ArgumentNullException (nameof (buffer));

			return ComputeHash (buffer, 0, buffer.Length);
		}

		public byte[] ComputeHash (Stream inputStream)
		{
			if (inputStream == null)
				throw new ArgumentNullException (nameof (inputStream));

			// don't read stream unless object is ready to use
			if (disposed)
				throw new ObjectDisposedException (nameof (MD4));

			var buffer = new byte[4096];
			int nread;

			do {
				if ((nread = inputStream.Read (buffer, 0, buffer.Length)) > 0)
					HashCore (buffer, 0, nread);
			} while (nread > 0);

			hashValue = HashFinal ();
			Initialize ();

			return hashValue;
		}

View on GitHub (pinned to 9d3859a785)