🔧 The Zero-Copy Parser That Was Pointing at Memory That Moved
A Span<T> is a lightweight view directly over existing memory rather than a copy of it, which is exactly what makes it fast – but that same property means it has no idea if the array it’s viewing gets resized, reallocated, or replaced while the span is still in scope, and List<T>’s internal array growing (via Add() triggering a resize once capacity is exceeded) while a Span<T> still references the OLD backing array leaves that span pointing at memory that’s now stale, producing garbage or genuinely wrong values with no exception thrown anywhere.
🔎 The Problem
var buffer = new List<byte>(capacity: 4);
buffer.AddRange(new byte[] { 1, 2, 3, 4 });
Span<byte> view = System.Runtime.InteropServices.CollectionsMarshal
.AsSpan(buffer); // valid span over buffer's CURRENT backing array
buffer.Add(5); // exceeds capacity 4 -> List<T> allocates a NEW,
// larger backing array and copies existing elements
// into it, then abandons the old array entirely
// `view` still points at the OLD, now-abandoned backing array.
// Reading from it doesn't throw - it just silently returns
// whatever that old array's contents happen to be, completely
// disconnected from the List's actual current state.
✅ Fix: Re-Acquire the Span After Any Operation That Could Resize
- Treat a Span<T> obtained from a List<T>’s backing array as valid only up until the next mutation that could change capacity – re-call CollectionsMarshal.AsSpan() (or whatever produced the span originally) immediately before each use rather than caching it across any Add(), Insert(), or RemoveAt() call.
- Where the exact final size is known ahead of time, pre-size the List<T> with the correct capacity in its constructor so no resize happens while any span over it is in use – a List<T> that never needs to grow during the span’s lifetime never swaps out its backing array underneath it.
- For code structured around repeated parsing where a span’s lifetime genuinely needs to survive multiple collection mutations, use a fixed-size array or a Memory<T>-backed buffer you control directly instead of a List<T>’s internal array, which was never designed to have its storage identity relied upon across a resize.
🚨 Why This Is Dangerous
- There is no exception, no warning, and no compiler diagnostic for this – a Span<T> genuinely cannot know the array it was built from has been abandoned, so reading through it after a resize just returns plausible-looking but wrong data, the exact kind of corruption that’s hardest to trace back to its actual cause.
- This is specifically timing-dependent on the List<T>’s current capacity versus its count at the moment the span was taken – code that works correctly through months of testing can start corrupting data the first time a particular code path happens to push the list just past whatever capacity threshold triggers a resize.
A Span<T> is a promise about where memory currently lives, not a promise that it will keep living there – the moment the collection underneath it decides to move, the span has no way to know, and neither will you unless you’ve accounted for it.
