{"record":{"id":"c3bf885dac139233","repo":"stride3d/stride","slug":"invalid-new-index-index-for-source-length-sourcelength","errorCode":null,"errorMessage":"Invalid new index {index} for source length {sourceLength}","messagePattern":"Invalid new index (.+?) for source length (.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/Dequeue.cs","lineNumber":391,"sourceCode":"    bool System.Collections.ICollection.IsSynchronized => false;\n\n    object System.Collections.ICollection.SyncRoot => this;\n\n    #endregion\n\n    #region GenericListHelpers\n\n    /// <summary>\n    /// Checks the <paramref name=\"index\"/> argument to see if it refers to a valid insertion point in a source of a given length.\n    /// </summary>\n    /// <param name=\"sourceLength\">The length of the source. This parameter is not checked for validity.</param>\n    /// <param name=\"index\">The index into the source.</param>\n    /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"index\"/> is not a valid index to an insertion point for the source.</exception>\n    private static void CheckNewIndexArgument(int sourceLength, int index)\n    {\n        if (index < 0 || index > sourceLength)\n        {\n            throw new ArgumentOutOfRangeException(nameof(index), \"Invalid new index \" + index + \" for source length \" + sourceLength);\n        }\n    }\n\n    /// <summary>\n    /// Checks the <paramref name=\"index\"/> argument to see if it refers to an existing element in a source of a given length.\n    /// </summary>\n    /// <param name=\"sourceLength\">The length of the source. This parameter is not checked for validity.</param>\n    /// <param name=\"index\">The index into the source.</param>\n    /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"index\"/> is not a valid index to an existing element for the source.</exception>\n    private static void CheckExistingIndexArgument(int sourceLength, int index)\n    {\n        if (index < 0 || index >= sourceLength)\n        {\n            throw new ArgumentOutOfRangeException(nameof(index), \"Invalid existing index \" + index + \" for source length \" + sourceLength);\n        }\n    }\n\n    /// <summary>","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/Dequeue.cs#L373-L409","documentation":"Thrown by the deque's private CheckNewIndexArgument helper when an insertion index is outside the valid insertion range [0, sourceLength]. Insertion positions may equal Count (insert at end) but never exceed it or go negative. It is raised from Insert and InsertRange as ArgumentOutOfRangeException naming the 'index' parameter.","triggerScenarios":"Calling deque.Insert(i, item) with i < 0 or i > Count, or deque.InsertRange(i, collection) with an out-of-range i.","commonSituations":"Off-by-one when inserting after the last element (using Count+1 instead of Count); computing the index from an empty deque (Count=0) where only index 0 is valid; passing an index from a differently-sized collection.","solutions":["Clamp or validate the insertion index to 0..deque.Count before calling Insert/InsertRange","Check the deque is not empty when reusing a stored index (an index valid before a RemoveAt may now exceed Count)","Use AddToAddBack/AddToFront if you actually meant append/prepend, which need no index"],"exampleFix":"// before\ndeque.Insert(deque.Count + 1, item); // throws\n// after\ndeque.Insert(deque.Count, item); // insert at end is legal","handlingStrategy":"validation","validationCode":"if (index < 0 || index > deque.Count) throw new ArgumentOutOfRangeException(nameof(index), $\"Insert index {index} out of range for Count {deque.Count}\");\ndeque.Insert(index, item);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat valid insertion indices as 0..Count inclusive","Re-read Count right before indexed mutations","Prefer AddToAddBack/AddToFront when an index isn't required"],"tags":["collections","deque","argument-out-of-range","insert"],"backgroundTag":"argument-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}