JamesNK/Newtonsoft.Json · error · ArgumentOutOfRangeException

arrayIndex is less than 0.

Error message

arrayIndex is less than 0.

What it means

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.

Source

Thrown at Src/Newtonsoft.Json/Linq/JObject.cs:675

        {
            JProperty? property = Property(item.Key, StringComparison.Ordinal);
            if (property == null)
            {
                return false;
            }

            return (property.Value == item.Value);
        }

        void ICollection<KeyValuePair<string, JToken?>>.CopyTo(KeyValuePair<string, JToken?>[] array, int arrayIndex)
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }
            if (arrayIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0.");
            }
            if (arrayIndex >= array.Length && arrayIndex != 0)
            {
                throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
            }
            if (Count > array.Length - arrayIndex)
            {
                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.");
            }

            int index = 0;
            foreach (JProperty property in _properties)
            {
                array[arrayIndex + index] = new KeyValuePair<string, JToken?>(property.Name, property.Value);
                index++;
            }
        }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Validate arrayIndex >= 0 before calling CopyTo.
  2. Compute the offset defensively (Math.Max(0, offset)) only if a negative result is meaningless in your logic — otherwise treat it as an error.
  3. Use a freshly allocated array starting at index 0 when in doubt.

Example fix

// before
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, offset - 1); // can be < 0

// after
if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, arrayIndex);
Defensive patterns

Strategy: validation

Validate before calling

if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(array, arrayIndex);

Try / catch

try { ((ICollection<KeyValuePair<string,JToken?>>)jObject).CopyTo(arr, idx); }
catch (ArgumentOutOfRangeException) { ((ICollection<KeyValuePair<string,JToken?>>)jObject).CopyTo(arr, 0); }

Prevention

When it happens

Trigger: 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).

Common situations: Off-by-one buffer math; indexing arithmetic that underflows to negative; reused copy helpers that pass a caller-supplied index without bounds checking.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/460a9485628ebacf. Report an issue: GitHub.