{"record":{"id":"2d567caed10ee9d9","repo":"jstedfast/MailKit","slug":"arrayindex","errorCode":null,"errorMessage":"arrayIndex","messagePattern":"arrayIndex","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/BodyPartCollection.cs","lineNumber":146,"sourceCode":"\t\t/// <remarks>\n\t\t/// Copies all of the body parts within the collection 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 (BodyPart[] 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\tcollection.CopyTo (array, arrayIndex);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Removes the specified body part.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Removes the specified body part.\n\t\t/// </remarks>\n\t\t/// <returns><see langword=\"true\" /> if the body part was removed; otherwise, <see langword=\"false\" />.</returns>\n\t\t/// <param name=\"part\">The body part.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"part\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\tpublic bool Remove (BodyPart part)\n\t\t{\n\t\t\tif (part == null)","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/BodyPartCollection.cs#L128-L164","documentation":"BodyPartCollection.CopyTo throws ArgumentOutOfRangeException when arrayIndex is negative or arrayIndex + Count exceeds array.Length, i.e. there is not enough room in the destination array at the given offset. The parameter name 'arrayIndex' is passed as the exception's paramName.","triggerScenarios":"Calling CopyTo(array, -1); calling CopyTo(array, 1) on a 1-element collection into a 1-element array; copying Count items at an offset that leaves fewer than Count slots free.","commonSituations":"Manually sized arrays that don't account for Count; off-by-one offsets when writing multiple collections into one shared buffer; using array.Length of a differently-sized array than intended after refactoring.","solutions":["Size the destination as bodyParts.Count + arrayIndex or start at index 0: bodyParts.CopyTo(array, 0).","Validate before calling: if (arrayIndex >= 0 && arrayIndex + bodyParts.Count <= array.Length).","Use foreach/ToArray() to avoid manual index math entirely."],"exampleFix":"// before\nvar array = new BodyPart[collection.Count];\ncollection.CopyTo(array, 1); // out of range\n// after\nvar array = new BodyPart[collection.Count];\ncollection.CopyTo(array, 0);","handlingStrategy":"validation","validationCode":"if (arrayIndex < 0 || arrayIndex + bodyParts.Count > array.Length) throw new ArgumentException(\"Insufficient space in destination array\", nameof(arrayIndex));","typeGuard":"bool FitsInArray(int count, int index, int length) => index >= 0 && index + count <= length;","tryCatchPattern":"try { bodyParts.CopyTo(array, offset); } catch (ArgumentOutOfRangeException ex) { /* resize array or reset offset */ }","preventionTips":["Size destination arrays as Count + offset","Watch for <= vs < errors in copy loops","Unit-test CopyTo with boundary offsets (0, Count-1, Count)"],"tags":["argument-out-of-range","copyto","array-bounds","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"}