programming
error-handling
array-index-out-of-bounds
debugging
software-development

Source array was not long enough. Check srcIndex and length, and the array's lower bounds

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

This .NET error means a copy operation tried to read past the available part of the source array. The usual cause is a mismatch between srcIndex, length, and the actual source array size. In practice, it often appears in calls such as Array.Copy, Buffer.BlockCopy, or framework code that internally performs array copying.

What the Error Really Means

A typical copy operation needs all of these to be valid:

  • 'srcIndex must be within the source array'
  • 'length must be non-negative'
  • 'srcIndex + length must not exceed the source array bounds'
  • the destination side must also have enough space

If any of those relationships fail, the runtime throws an error instead of reading invalid memory.

A direct example:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        int[] source = { 10, 20, 30 };
8        int[] destination = new int[2];
9
10        Array.Copy(source, 2, destination, 0, 2);
11    }
12}

This fails because starting at source index 2, only one element remains, but the code asks to copy two.

Fix the Index Math First

The fastest way to debug this class of problem is to print or inspect the values that control the copy.

csharp
Console.WriteLine($"source length = {source.Length}");
Console.WriteLine($"srcIndex = {srcIndex}");
Console.WriteLine($"length = {length}");

Then check the relationship explicitly.

A guarded copy pattern:

csharp
1using System;
2
3class Program
4{
5    static void SafeCopy(int[] source, int srcIndex, int[] destination, int dstIndex, int length)
6    {
7        if (srcIndex < 0 || dstIndex < 0 || length < 0)
8            throw new ArgumentOutOfRangeException();
9
10        if (srcIndex + length > source.Length)
11            throw new ArgumentException("Source range exceeds source array length.");
12
13        if (dstIndex + length > destination.Length)
14            throw new ArgumentException("Destination range exceeds destination array length.");
15
16        Array.Copy(source, srcIndex, destination, dstIndex, length);
17    }
18
19    static void Main()
20    {
21        int[] source = { 10, 20, 30 };
22        int[] destination = new int[2];
23        SafeCopy(source, 1, destination, 0, 2);
24        Console.WriteLine(string.Join(", ", destination));
25    }
26}

This makes the bug easier to catch where the invalid range is created rather than letting it surface later in a less obvious place.

Watch for Off-by-One Errors

Many of these failures are simple off-by-one mistakes.

For example, if the last valid source index is source.Length - 1, then copying 2 items from that index is impossible because there is only one item left.

A common faulty pattern is:

  • loop until i <= array.Length
  • then use i as a valid index

That crosses the valid boundary by one element.

Array.Copy Versus Buffer.BlockCopy

The error message often appears around Array.Copy, but similar logic applies to Buffer.BlockCopy. The important difference is that Buffer.BlockCopy uses byte counts, not element counts.

csharp
byte[] source = { 1, 2, 3, 4 };
byte[] destination = new byte[2];
Buffer.BlockCopy(source, 0, destination, 0, 2);

That copies two bytes, not two higher-level objects. When developers forget that distinction and pass an element count where a byte count is required, range errors follow quickly.

The Error May Come from Framework Code

Sometimes your own code never calls Array.Copy directly, but a serializer, collection helper, or parser does. In that case, the same debugging rule applies: find the array, index, and requested length that the framework code received.

That often means the real bug is earlier, such as:

  • wrong slice length computed from a packet header
  • malformed input data
  • incorrect assumption about fixed-size records
  • stale cached array size after a resize

The thrown exception is just where the bad range finally becomes visible.

Common Pitfalls

A common mistake is checking only the destination length and forgetting that the source side can fail first.

Another mistake is confusing "last valid index" with "number of remaining items". Those are not the same quantity.

People also often misuse Buffer.BlockCopy by passing element counts instead of byte counts.

Finally, if a framework throws the exception, do not stop at the stack frame that copies the array. Trace back to where the invalid index and length were calculated.

Summary

  • This error means a copy operation tried to read beyond the available portion of the source array
  • Validate srcIndex, length, and srcIndex + length before copying
  • Check the destination range too, but do not forget the source side
  • Off-by-one mistakes are one of the most common causes
  • 'Buffer.BlockCopy uses byte counts, which is easy to misuse'
  • When the error comes from framework code, the real bug is often earlier in your own range calculation logic

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms