Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Fix a Span-Based Parser That Silently Corrupts Data Because the Underlying Array Was Resized Mid-Operation

- 22.10.26 - ErcanOPAK

🔧 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.

— C# Best Practice

Related posts:

IEnumerable Executes More Than Once

IEnumerable Executes More Than Once

List.ForEach Hides Exceptions

List.ForEach Hides Exceptions

C#: Use CallerArgumentExpression for Better Argument Validation

C#: Use CallerArgumentExpression for Better Argument Validation

Views: 4

Post navigation

The AI Prompt That Reviews Your Background Job Queue for Tasks That Silently Fail Without Ever Being Retried
CSS: Fix pointer-events: none on a Parent Silently Disabling Clicks on Its Children Too Because the Value Was Never Reset Back to auto

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com