{"record":{"id":"d56971fa853b2d55","repo":"jstedfast/MailKit","slug":"specified-argument-was-out-of-range-of-valid-values","errorCode":null,"errorMessage":"Specified argument was out of range of valid values. (Parameter 'arrayIndex')","messagePattern":"Specified argument was out of range of valid values\\. \\(Parameter 'arrayIndex'\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/AccessRights.cs","lineNumber":230,"sourceCode":"\t\t/// <remarks>\n\t\t/// Copies all of the access rights into the array,\n\t\t/// starting at the specified array index.\n\t\t/// </remarks>\n\t\t/// <param name=\"array\">The array.</param>\n\t\t/// <param name=\"arrayIndex\">The array index.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"array\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"arrayIndex\"/> is out of range.\n\t\t/// </exception>\n\t\tpublic void CopyTo (AccessRight[] array, int arrayIndex)\n\t\t{\n\t\t\tif (array == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (array));\n\n\t\t\tif (arrayIndex < 0 || arrayIndex + Count > array.Length)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (arrayIndex));\n\n\t\t\tlist.CopyTo (array, arrayIndex);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Removes the specified access right.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Removes the specified access right.\n\t\t/// </remarks>\n\t\t/// <returns><see langword=\"true\" /> if the access right was removed; otherwise, <see langword=\"false\" />.</returns>\n\t\t/// <param name=\"right\">The access right.</param>\n\t\tpublic bool Remove (AccessRight right)\n\t\t{\n\t\t\treturn list.Remove (right);\n\t\t}\n\n\t\t/// <summary>","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/AccessRights.cs#L212-L248","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar arr = new AccessRight[3];\naccessRights.CopyTo(arr, 0); // Count == 5 -> out of range\n\n// after\nvar arr = new AccessRight[accessRights.Count];\naccessRights.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (arrayIndex < 0 || arrayIndex + accessRights.Count > array.Length)\n    throw new ArgumentException(\"Destination array too small\", nameof(array));\naccessRights.CopyTo(array, arrayIndex);","typeGuard":"bool FitsInArray(AccessRight[] arr, int idx, AccessRights rights) => arr != null && idx >= 0 && idx + rights.Count <= arr.Length;","tryCatchPattern":"try\n{\n    accessRights.CopyTo(array, arrayIndex);\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"arrayIndex\")\n{\n    logger.LogError(\"CopyTo: index {0} insufficient for {1} rights\", arrayIndex, accessRights.Count);\n}","preventionTips":["Size arrays as accessRights.Count + arrayIndex","Recompute Count immediately before copying; collections may have changed","Prefer fresh ToArray() over reusing smaller scratch buffers"],"tags":["argument-out-of-range","copyto","imap","mailkit"],"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"}