{"record":{"id":"7ae330c80334d674","repo":"jstedfast/MailKit","slug":"inputcount-md4","errorCode":null,"errorMessage":"inputCount","messagePattern":"inputCount","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/Ntlm/MD4.cs","lineNumber":348,"sourceCode":"\t\t\t\t\tHashCore (buffer, 0, nread);\n\t\t\t} while (nread > 0);\n\n\t\t\thashValue = HashFinal ();\n\t\t\tInitialize ();\n\n\t\t\treturn hashValue;\n\t\t}\n\n\t\tpublic int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)\n\t\t{\n\t\t\tif (inputBuffer == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (inputBuffer));\n\n\t\t\tif (inputOffset < 0 || inputOffset > inputBuffer.Length)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (inputOffset));\n\n\t\t\tif (inputCount < 0 || inputOffset > inputBuffer.Length - inputCount)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (inputCount));\n\n\t\t\tif (outputBuffer != null) {\n\t\t\t\tif (outputOffset < 0 || outputOffset > outputBuffer.Length - inputCount)\n\t\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (outputOffset));\n\t\t\t}\n\n\t\t\tHashCore (inputBuffer, inputOffset, inputCount);\n\n\t\t\tif (outputBuffer != null)\n\t\t\t\tBuffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);\n\n\t\t\treturn inputCount;\n\t\t}\n\n\t\tpublic byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)\n\t\t{\n\t\t\tif (inputCount < 0)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (inputCount));","sourceCodeStart":330,"sourceCodeEnd":366,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/Ntlm/MD4.cs#L330-L366","documentation":"TransformBlock throws ArgumentOutOfRangeException named \"inputCount\" when inputCount is negative or when inputOffset + inputCount exceeds inputBuffer.Length (check: inputOffset > inputBuffer.Length - inputCount). It enforces that the block to hash lies entirely inside the input array, consistent with ICryptoTransform semantics.","triggerScenarios":"TransformBlock(buf, off, -1, null, 0), or TransformBlock(buf, off, len, null, 0) where off + len > buf.Length — e.g. passing full length while also passing a non-zero offset, or a byte-count/char-count mixup.","commonSituations":"Last-chunk logic computing count as remaining + blockSize; passing Encoding string lengths (chars) instead of byte counts; reusing a constant block size for the final short chunk.","solutions":["Clamp the final chunk: count = Math.Min(blockSize, inputBuffer.Length - inputOffset).","Verify count is bytes actually available: require count >= 0 && inputOffset <= inputBuffer.Length - count.","Fix length sources (use GetByteCount for encoded text, not string Length).","Validate external counts upstream before the transform loop."],"exampleFix":"// before\ntransform.TransformBlock(buf, offset, blockSize, null, 0); // final short chunk overruns\n// after\nint count = Math.Min(blockSize, buf.Length - offset);\nif (count > 0) transform.TransformBlock(buf, offset, count, null, 0);","handlingStrategy":"validation","validationCode":"if (inputCount < 0 || inputOffset > inputBuffer.Length - inputCount)\n    throw new ArgumentOutOfRangeException(nameof(inputCount));","typeGuard":"static bool IsValidCount(byte[] buf, int offset, int count) => count >= 0 && offset <= buf.Length - count;","tryCatchPattern":"try { transform.TransformBlock(buf, offset, count, null, 0); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"inputCount\") { log.LogError(ex, \"count {Count} at offset {Offset} exceeds len {Len}\", count, offset, buf.Length); throw; }","preventionTips":["Clamp every chunk with Math.Min(blockSize, buf.Length - offset)","Use byte counts (GetByteCount) not char counts","Test the final short chunk explicitly"],"tags":["argument-exception","crypto","out-of-range","transformblock"],"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-15T23:17:13.987Z"}