{"record":{"id":"b99049be3f9f45e4","repo":"jstedfast/MailKit","slug":"argumentoutofrangeexception","errorCode":null,"errorMessage":"ArgumentOutOfRangeException","messagePattern":"ArgumentOutOfRangeException","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/DuplexStream.cs","lineNumber":164,"sourceCode":"\t\t/// <returns>A long value representing the length of the stream in bytes.</returns>\n\t\t/// <value>The length of the stream.</value>\n\t\t/// <exception cref=\"System.NotSupportedException\">\n\t\t/// The stream does not support seeking.\n\t\t/// </exception>\n\t\tpublic override long Length {\n\t\t\tget { throw new NotSupportedException (); }\n\t\t}\n\n\t\tstatic void ValidateArguments (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 || count > (buffer.Length - offset))\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (count));\n\t\t}\n\n\t\tvoid CheckDisposed ()\n\t\t{\n\t\t\tif (disposed)\n\t\t\t\tthrow new ObjectDisposedException (nameof (DuplexStream));\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Reads a sequence of bytes from the stream and advances the position\n\t\t/// within the stream by the number of bytes read.\n\t\t/// </summary>\n\t\t/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many\n\t\t/// bytes are not currently available, or zero (0) if the end of the stream has been reached.</returns>\n\t\t/// <param name=\"buffer\">The buffer.</param>\n\t\t/// <param name=\"offset\">The buffer offset.</param>\n\t\t/// <param name=\"count\">The number of bytes to read.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/DuplexStream.cs#L146-L182","documentation":"ValidateArguments guards DuplexStream.Read/ReadAsync/Write/WriteAsync. It throws ArgumentOutOfRangeException when offset is negative or beyond buffer.Length, or when count is negative or exceeds buffer.Length - offset. It ensures read/write operations never access memory outside the supplied buffer.","triggerScenarios":"Calling Read/Write (or their async forms) on a DuplexStream with a negative offset, offset > buffer.Length, negative count, or count > buffer.Length - offset.","commonSituations":"Off-by-one buffer math; reusing buffer-size constants after changing buffer allocation; passing a user-supplied offset/count straight through without clamping.","solutions":["Clamp or validate offset and count against buffer.Length before the call: 0 <= offset <= buffer.Length and 0 <= count <= buffer.Length - offset.","Fix the arithmetic that computes offset/count (check for subtraction underflow producing negatives).","If values come from external input, validate them at the boundary with a clear error message."],"exampleFix":"// before\nstream.Read (buffer, offset, count);\n// after\nif (offset < 0 || offset > buffer.Length) throw new ArgumentException (nameof (offset));\nif (count < 0 || count > buffer.Length - offset) throw new ArgumentException (nameof (count));\nstream.Read (buffer, offset, count);","handlingStrategy":"validation","validationCode":"static bool IsValidWindow (byte[] buffer, int offset, int count) =>\n    buffer != null && offset >= 0 && offset <= buffer.Length && count >= 0 && count <= buffer.Length - offset;","typeGuard":"bool ValidBufferArgs (byte[] b, int off, int cnt) => b != null && off >= 0 && off <= b.Length && cnt >= 0 && cnt <= b.Length - off;","tryCatchPattern":"try { stream.Read (buffer, offset, count); } catch (ArgumentOutOfRangeException ex) { /* log ex.ParamName: offset or count */ }","preventionTips":["Centralize buffer-window math in one validated helper instead of repeating arithmetic at call sites.","Beware of computed offsets after resizing buffers; recompute count = buffer.Length - offset.","Validate externally supplied offset/count at the API boundary."],"tags":["io","stream","argument-out-of-range","buffer"],"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"}