{"record":{"id":"b52e24eb3e276117","repo":"jstedfast/MailKit","slug":"count-md4","errorCode":null,"errorMessage":"count","messagePattern":"count","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/Ntlm/MD4.cs","lineNumber":296,"sourceCode":"\t\t\tHH (ref c, d, a, b, x[ 7], S33); /* 47 */\n\t\t\tHH (ref b, c, d, a, x[15], S34); /* 48 */\n\n\t\t\tstate [0] += a;\n\t\t\tstate [1] += b;\n\t\t\tstate [2] += c;\n\t\t\tstate [3] += d;\n\t\t}\n\n\t\tpublic byte[] ComputeHash (byte[] buffer, int offset, int count)\n\t\t{\n\t\t\tif (buffer == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (buffer));\n\n\t\t\tif (offset < 0 || offset > buffer.Length)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (offset));\n\n\t\t\tif (count < 0 || offset > buffer.Length - count)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (count));\n\n\t\t\tif (disposed)\n\t\t\t\tthrow new ObjectDisposedException (nameof (MD4));\n\n\t\t\tHashCore (buffer, offset, count);\n\t\t\thashValue = HashFinal ();\n\t\t\tInitialize ();\n\n\t\t\treturn hashValue;\n\t\t}\n\n\t\tpublic byte[] ComputeHash (byte[] buffer)\n\t\t{\n\t\t\tif (buffer == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (buffer));\n\n\t\t\treturn ComputeHash (buffer, 0, buffer.Length);\n\t\t}","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/Ntlm/MD4.cs#L278-L314","documentation":"ComputeHash(buffer, offset, count) throws ArgumentOutOfRangeException named \"count\" when count is negative or when offset + count exceeds the buffer (the check offset > buffer.Length - count). This guarantees the hashing region stays inside the array. It is thrown before HashCore runs, so the hash state is untouched.","triggerScenarios":"Calling ComputeHash(buffer, offset, count) with count < 0, or with offset > buffer.Length - count (e.g. ComputeHash(data, data.Length - 2, 5) on a 10-byte array, or count passed as -1 from a failed length calculation).","commonSituations":"Mixing up byte counts with character counts when hashing a string's encoding; passing the wrong variable as count (offset instead of length); reading a 16-bit or 32-bit length prefix that was malformed.","solutions":["Verify count is the number of bytes to hash and satisfies offset + count <= buffer.Length; fix the arithmetic.","If the region is genuinely longer than the buffer, recompute the correct sub-range before calling.","Validate inputs with (count >= 0 && offset <= buffer.Length - count) before invoking.","If count comes from external data, validate/parse it upstream and fail with a clear message."],"exampleFix":"// before\nhasher.ComputeHash(buf, offset, len); // len could exceed buf.Length - offset\n// after\nint safeLen = Math.Min(len, buf.Length - offset);\nhasher.ComputeHash(buf, offset, safeLen);","handlingStrategy":"validation","validationCode":"if (count < 0 || offset > buffer.Length - count)\n    throw new ArgumentOutOfRangeException(nameof(count), $\"offset={offset}, count={count}, len={buffer.Length}\");","typeGuard":"static bool IsValidCount(byte[] buffer, int offset, int count) =>\n    count >= 0 && offset <= buffer.Length - count;","tryCatchPattern":"try { hash = md4.ComputeHash(data, offset, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { log.LogError(ex, \"count exceeds buffer\"); throw; }","preventionTips":["Compute counts as buffer.Length - offset, never as independent values","Distinguish byte counts from char counts (use Encoding.GetByteCount)","Clamp final chunks with Math.Min"],"tags":["argument-exception","hash","out-of-range","md4"],"backgroundTag":"argument-out-of-range","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}