{"record":{"id":"9a9ccc038e395769","repo":"stride3d/stride","slug":"invalid-existing-index-index-for-source-length-sourcelength","errorCode":null,"errorMessage":"Invalid existing index {index} for source length {sourceLength}","messagePattern":"Invalid existing index (.+?) for source length (.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/Dequeue.cs","lineNumber":405,"sourceCode":"    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>\n    /// Checks the <paramref name=\"offset\"/> and <paramref name=\"count\"/> arguments for validity when applied to a source of a given length. Allows 0-element ranges, including a 0-element range at the end of the source.\n    /// </summary>\n    /// <param name=\"sourceLength\">The length of the source. This parameter is not checked for validity.</param>\n    /// <param name=\"offset\">The index into source at which the range begins.</param>\n    /// <param name=\"count\">The number of elements in the range.</param>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Either <paramref name=\"offset\"/> or <paramref name=\"count\"/> is less than 0.</exception>\n    /// <exception cref=\"ArgumentException\">The range [offset, offset + count) is not within the range [0, sourceLength).</exception>\n    private static void CheckRangeArguments(int sourceLength, int offset, int count)\n    {\n        if (offset < 0)\n        {\n            throw new ArgumentOutOfRangeException(nameof(offset), \"Invalid offset \" + offset);\n        }\n","sourceCodeStart":387,"sourceCodeEnd":423,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/Dequeue.cs#L387-L423","documentation":"Thrown by CheckExistingIndexArgument when an index meant to address an EXISTING element falls outside [0, sourceLength-1]. Unlike insertion indices, an existing-element index may not equal Count. Raised from RemoveAt (and validated on construction) as ArgumentOutOfRangeException naming 'index'.","triggerScenarios":"Calling deque.RemoveAt(i) where i < 0 or i >= Count.","commonSituations":"Looping with i <= Count instead of i < Count; removing from an empty deque (any index is invalid); a stale index captured before other removals shrank the deque.","solutions":["Validate 0 <= i < deque.Count before RemoveAt","Re-fetch Count immediately before each removal inside loops","Guard against an empty deque before removing by index"],"exampleFix":"// before\nfor (int i = 0; i <= deque.Count; i++) deque.RemoveAt(i); // throws at i == Count\n// after\nfor (int i = deque.Count - 1; i >= 0; i--) deque.RemoveAt(i);","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= deque.Count) throw new ArgumentOutOfRangeException(nameof(index), $\"Index {index} invalid for Count {deque.Count}\");\ndeque.RemoveAt(index);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember existing-element indices max out at Count-1","Re-fetch Count each iteration of removal loops","Never reuse indices captured before mutations"],"tags":["collections","deque","argument-out-of-range","remove"],"backgroundTag":"index-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"}