jstedfast/MailKit · error · ArgumentOutOfRangeException
Specified argument was out of range of valid values…
Error message
Specified argument was out of range of valid values. (Parameter 'arrayIndex')
What it means
AccessRights.CopyTo validates that arrayIndex is non-negative and that arrayIndex + Count fits within the destination array, throwing ArgumentOutOfRangeException(nameof(arrayIndex)) at MailKit/AccessRights.cs:230 otherwise. The check prevents writing past the end of the caller's buffer.
Solutions
- Size the array to accessRights.Count (or Count + arrayIndex) before calling CopyTo.
- Verify arrayIndex is >= 0 and arrayIndex + accessRights.Count <= array.Length before the call.
- Use arr = accessRights.ToArray() or CopyTo(accessRights) helpers instead of manual CopyTo.
Example fix
// before var arr = new AccessRight[3]; accessRights.CopyTo(arr, 0); // Count == 5 -> out of range // after var arr = new AccessRight[accessRights.Count]; accessRights.CopyTo(arr, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (arrayIndex < 0 || arrayIndex + accessRights.Count > array.Length)
throw new ArgumentException("Destination array too small", nameof(array));
accessRights.CopyTo(array, arrayIndex); Type guard
bool FitsInArray(AccessRight[] arr, int idx, AccessRights rights) => arr != null && idx >= 0 && idx + rights.Count <= arr.Length;
Try / catch
try
{
accessRights.CopyTo(array, arrayIndex);
}
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "arrayIndex")
{
logger.LogError("CopyTo: index {0} insufficient for {1} rights", arrayIndex, accessRights.Count);
} Prevention
- Size arrays as accessRights.Count + arrayIndex
- Recompute Count immediately before copying; collections may have changed
- Prefer fresh ToArray() over reusing smaller scratch buffers
When it happens
Trigger: Calling accessRights.CopyTo(array, negativeIndex) or CopyTo(array, arrayIndex) where array.Length - arrayIndex < accessRights.Count — e.g. copying 5 rights into an array of length 3 at index 0, or using index 10 in a length-5 array.
Common situations: Reusing a small scratch array across calls without resizing; passing a cursor/offset from another collection without checking remaining capacity; off-by-one errors when the array was sized from a different count.
Related errors
- Value cannot be null. (Parameter 'array')
- Specified argument was out of range of valid values…
- arrayIndex
- index
- offset
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/d56971fa853b2d55.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/AccessRights.cs:230
/// <remarks>
/// Copies all of the access rights into the array,
/// starting at the specified array index.
/// </remarks>
/// <param name="array">The array.</param>
/// <param name="arrayIndex">The array index.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="array"/> is <see langword="null" />.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="arrayIndex"/> is out of range.
/// </exception>
public void CopyTo (AccessRight[] array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException (nameof (array));
if (arrayIndex < 0 || arrayIndex + Count > array.Length)
throw new ArgumentOutOfRangeException (nameof (arrayIndex));
list.CopyTo (array, arrayIndex);
}
/// <summary>
/// Removes the specified access right.
/// </summary>
/// <remarks>
/// Removes the specified access right.
/// </remarks>
/// <returns><see langword="true" /> if the access right was removed; otherwise, <see langword="false" />.</returns>
/// <param name="right">The access right.</param>
public bool Remove (AccessRight right)
{
return list.Remove (right);
}
/// <summary>View on GitHub (pinned to 9d3859a785)