{"record":{"id":"b530e3170303a74b","repo":"jstedfast/MailKit","slug":"buffer-md4","errorCode":null,"errorMessage":"buffer","messagePattern":"buffer","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/Ntlm/MD4.cs","lineNumber":290,"sourceCode":"\t\t\tHH (ref a, b, c, d, x[ 1], S31); /* 41 */\n\t\t\tHH (ref d, a, b, c, x[ 9], S32); /* 42 */\n\t\t\tHH (ref c, d, a, b, x[ 5], S33); /* 43 */\n\t\t\tHH (ref b, c, d, a, x[13], S34); /* 44 */\n\t\t\tHH (ref a, b, c, d, x[ 3], S31); /* 45 */\n\t\t\tHH (ref d, a, b, c, x[11], S32); /* 46 */\n\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)","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/Ntlm/MD4.cs#L272-L308","documentation":"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.","triggerScenarios":"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).","commonSituations":"NTLM message construction where the payload bytes were never produced (null); offsets/counts taken from malformed protocol fields; off-by-one in slice arithmetic.","solutions":["Ensure the byte array is allocated before hashing","Validate the (offset, count) window: 0 <= offset <= length and 0 <= count <= length - offset","Pass (buffer, 0, buffer.Length) for whole-array hashing to avoid window bugs"],"exampleFix":"// before\nbyte[] payload = GetPayload (); // may be null\nbyte[] h = md4.ComputeHash (payload, offset, count);\n// after\nbyte[] payload = GetPayload ();\nif (payload == null)\n    throw new InvalidOperationException (\"payload missing\");\nint off = Math.Min (offset, payload.Length);\nint cnt = Math.Min (count, payload.Length - off);\nbyte[] h = md4.ComputeHash (payload, off, cnt);","handlingStrategy":"validation","validationCode":"if (buffer == null)\n    throw new ArgumentException (\"buffer required\", nameof (buffer));\nif (offset < 0 || offset > buffer.Length)\n    throw new ArgumentException (\"offset out of range\", nameof (offset));\nif (count < 0 || offset > buffer.Length - count)\n    throw new ArgumentException (\"count out of range\", nameof (count));\nbyte[] h = md4.ComputeHash (buffer, offset, count);","typeGuard":"static bool IsValidSlice (byte[]? b, int off, int cnt) => b != null && off >= 0 && off <= b.Length && cnt >= 0 && off + cnt <= b.Length;","tryCatchPattern":"try {\n    byte[] h = md4.ComputeHash (buffer, offset, count);\n} catch (ArgumentException) {\n    byte[] h = md4.ComputeHash (buffer ?? Array.Empty<byte> (), 0, buffer?.Length ?? 0);\n}","preventionTips":["Use the (buffer, 0, buffer.Length) overload to avoid window math entirely","Validate slices at protocol-parse boundaries before hashing","Default null payloads to Array.Empty<byte>() upstream"],"tags":["csharp","argument-out-of-range","null-argument","hashing","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"}