{"record":{"id":"cd850c79f1218ef4","repo":"jstedfast/MailKit","slug":"count-hmacmd5","errorCode":null,"errorMessage":"count","messagePattern":"count","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/Ntlm/HMACMD5.cs","lineNumber":109,"sourceCode":"\t\t{\n\t\t\thash.Init (new KeyParameter (Key));\n\t\t}\n\n\t\tpublic void Clear ()\n\t\t{\n\t\t\tDispose (false);\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 (\"HashAlgorithm\");\n\n\t\t\tHashCore (buffer, offset, count);\n\t\t\thashValue = HashFinal ();\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}\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/Ntlm/HMACMD5.cs#L91-L127","documentation":"ComputeHash throws ArgumentOutOfRangeException(\"count\") when count is negative or offset+count extends past the buffer (the guard compares offset > buffer.Length - count). A wrong offset can therefore also raise this error.","triggerScenarios":"Negative counts, counts exceeding buffer.Length - offset, or count computed against a stale offset.","commonSituations":"Hashing a sub-range of an NTLM message with length fields taken from an incorrect header parse.","solutions":["Ensure count >= 0 and offset + count <= buffer.Length before calling","Recompute count from the actual data length","Catch ArgumentOutOfRangeException and validate the length source (e.g. header parse)"],"exampleFix":"// before\nhmac.ComputeHash(buf, 0, declaredLength); // untrusted length\n// after\nint count = Math.Min(declaredLength, buf.Length);\nif (count >= 0)\n\thmac.ComputeHash(buf, 0, count);","handlingStrategy":"validation","validationCode":"if (count < 0 || offset + count > buffer.Length) throw new ArgumentException(\"count out of bounds\");","typeGuard":null,"tryCatchPattern":"try { hmac.ComputeHash(buf, off, cnt); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* recompute count */ }","preventionTips":["Clamp untrusted lengths to buffer.Length","Validate parsed header lengths before use","Bounds-assert count in hashing helpers"],"tags":["csharp","hmac","argument-out-of-range"],"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"}