{"record":{"id":"880f60a4251e0e7a","repo":"JamesNK/Newtonsoft.Json","slug":"index-must-be-within-the-bounds-of-the-list","errorCode":null,"errorMessage":"Index must be within the bounds of the List.","messagePattern":"Index must be within the bounds of the List\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":368,"sourceCode":"                JsonCloneSettings? settings = copyAnnotations\n                    ? null\n                    : JsonCloneSettings.SkipCopyAnnotations;\n\n                item = item.CloneToken(settings);\n            }\n\n            return item;\n        }\n\n        internal abstract int IndexOfItem(JToken? item);\n\n        internal virtual bool InsertItem(int index, JToken? item, bool skipParentCheck, bool copyAnnotations)\n        {\n            IList<JToken> children = ChildrenTokens;\n\n            if (index > children.Count)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), \"Index must be within the bounds of the List.\");\n            }\n\n            CheckReentrancy();\n\n            item = EnsureParentToken(item, skipParentCheck, copyAnnotations);\n\n            JToken? previous = (index == 0) ? null : children[index - 1];\n            // haven't inserted new token yet so next token is still at the inserting index\n            JToken? next = (index == children.Count) ? null : children[index];\n\n            ValidateToken(item, null);\n\n            item.Parent = this;\n\n            item.Previous = previous;\n            if (previous != null)\n            {\n                previous.Next = item;","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L350-L386","documentation":"InsertItem throws ArgumentOutOfRangeException when the requested insertion index is strictly greater than ChildrenTokens.Count (index == Count is allowed for append; anything above is not). This guards the underlying List insert.","triggerScenarios":"jArray.Insert(index, item) where index > Count; Insert via IList<JToken>.Insert with a stale count; off-by-one after a concurrent removal.","commonSituations":"Off-by-one when inserting at 'the end' using count+1; using a cached count that is now stale; concurrent mutation.","solutions":["To append, call Add(item) instead of Insert(Count, item) — or pass exactly Count.","Clamp index to Math.Min(index, container.Count) before inserting.","Recompute Count immediately before the insert when the collection may have changed."],"exampleFix":"// before\narr.Insert(arr.Count + 1, item);\n// after\narr.Add(item);","handlingStrategy":"validation","validationCode":"int idx = Math.Min(requestedIndex, arr.Count);\narr.Insert(idx, item); // or arr.Add(item) to append","typeGuard":null,"tryCatchPattern":"try { arr.Insert(index, item); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\") {\n    arr.Add(item); // append as a safe fallback\n}","preventionTips":["Use Add for append instead of Insert(Count, item).","Clamp index to [0, Count].","Recompute Count right before Insert when the collection may change."],"tags":["jcontainer","insert","bounds","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}