{"record":{"id":"460a9485628ebacf","repo":"JamesNK/Newtonsoft.Json","slug":"arrayindex-is-less-than-0-460a94","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/JObject.cs","lineNumber":675,"sourceCode":"        {\n            JProperty? property = Property(item.Key, StringComparison.Ordinal);\n            if (property == null)\n            {\n                return false;\n            }\n\n            return (property.Value == item.Value);\n        }\n\n        void ICollection<KeyValuePair<string, JToken?>>.CopyTo(KeyValuePair<string, JToken?>[] 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 (JProperty property in _properties)\n            {\n                array[arrayIndex + index] = new KeyValuePair<string, JToken?>(property.Name, property.Value);\n                index++;\n            }\n        }\n","sourceCodeStart":657,"sourceCodeEnd":693,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JObject.cs#L657-L693","documentation":"ArgumentOutOfRangeException thrown by JObject's ICollection<KeyValuePair<string,JToken?>>.CopyTo when arrayIndex is negative. CopyTo requires a zero-based start position in the destination array; a negative index is rejected before copying.","triggerScenarios":"Calling CopyTo(array, -1) directly, or with an arrayIndex computed from a subtraction/subtracting-length expression that goes negative (e.g. arrayIndex = idx - offset where offset exceeds idx).","commonSituations":"Off-by-one buffer math; indexing arithmetic that underflows to negative; reused copy helpers that pass a caller-supplied index without bounds checking.","solutions":["Validate arrayIndex >= 0 before calling CopyTo.","Compute the offset defensively (Math.Max(0, offset)) only if a negative result is meaningless in your logic — otherwise treat it as an error.","Use a freshly allocated array starting at index 0 when in doubt."],"exampleFix":"// before\n((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, offset - 1); // can be < 0\n\n// after\nif (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));\n((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, arrayIndex);","handlingStrategy":"validation","validationCode":"if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));\n((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(array, arrayIndex);","typeGuard":null,"tryCatchPattern":"try { ((ICollection<KeyValuePair<string,JToken?>>)jObject).CopyTo(arr, idx); }\ncatch (ArgumentOutOfRangeException) { ((ICollection<KeyValuePair<string,JToken?>>)jObject).CopyTo(arr, 0); }","preventionTips":["Validate arrayIndex >= 0 before CopyTo.","Guard arithmetic that computes offsets from subtractions.","Default to index 0 when a computed offset is meaningless."],"tags":["newtonsoft-json","linq-to-json","jobject","copyto","argument-validation"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}