{"record":{"id":"9954bc139e45903a","repo":"JamesNK/Newtonsoft.Json","slug":"arrayindex-is-less-than-0","errorCode":null,"errorMessage":"arrayIndex is less than 0.","messagePattern":"arrayIndex is less than 0\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":597,"sourceCode":"\n            int index = IndexOfItem(existing);\n            SetItem(index, replacement);\n        }\n\n        internal virtual bool ContainsItem(JToken? item)\n        {\n            return (IndexOfItem(item) != -1);\n        }\n\n        internal virtual void CopyItemsTo(Array array, int arrayIndex)\n        {\n            if (array == null)\n            {\n                throw new ArgumentNullException(nameof(array));\n            }\n            if (arrayIndex < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(arrayIndex), \"arrayIndex is less than 0.\");\n            }\n            if (arrayIndex >= array.Length && arrayIndex != 0)\n            {\n                throw new ArgumentException(\"arrayIndex is equal to or greater than the length of array.\");\n            }\n            if (Count > array.Length - arrayIndex)\n            {\n                throw new ArgumentException(\"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.\");\n            }\n\n            int index = 0;\n            foreach (JToken token in ChildrenTokens)\n            {\n                array.SetValue(token, arrayIndex + index);\n                index++;\n            }\n        }\n","sourceCodeStart":579,"sourceCodeEnd":615,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L579-L615","documentation":"CopyItemsTo throws ArgumentOutOfRangeException when arrayIndex is negative. The destination offset must be a valid non-negative position in the target array.","triggerScenarios":"jArray.CopyTo(arr, -1); a computed offset that underflows below zero.","commonSituations":"Off-by-one arithmetic on the offset; passing an index derived from an unfound position.","solutions":["Validate arrayIndex >= 0 before calling CopyTo.","Use 0 when you want to copy from the start.","Clamp computed offsets to a minimum of 0."],"exampleFix":"// before\narr.CopyTo(dest, offset - 1);\n// after\narr.CopyTo(dest, Math.Max(0, offset - 1));","handlingStrategy":"validation","validationCode":"if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));\ncontainer.CopyTo(dest, arrayIndex);","typeGuard":null,"tryCatchPattern":"try { container.CopyTo(dest, arrayIndex); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"arrayIndex\") {\n    container.CopyTo(dest, 0);\n}","preventionTips":["Validate arrayIndex >= 0 before CopyTo.","Use 0 to copy from the start.","Clamp computed offsets to at least 0."],"tags":["jcontainer","copyto","bounds","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}