Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

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

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix an ArrayPool Buffer Returned Still Dirty

- 02.12.26 - ErcanOPAK

🔧 C#: Fix an ArrayPool Buffer Returned Still Dirty

ArrayPool<T>.Shared.Return() does not clear the buffer’s contents by default — the clearArray parameter defaults to false specifically because zeroing on every return has a real performance cost, and the pool’s whole purpose is avoiding allocation overhead in hot paths. That means a rented buffer handed back without explicitly requesting a clear still contains whatever data the previous caller wrote into it, and the very next caller to rent a buffer of the same size has a real chance of receiving that exact same backing array — including every byte of data the previous owner never bothered to clear, which is fine for throwaway scratch space, but quietly dangerous when the buffer ever held something like authentication tokens, passwords, or other sensitive data that genuinely shouldn’t outlive its intended use.

🔎 The Problem

byte[] buffer = ArrayPool<byte>.Shared.Rent(256);
try
{
    int written = PopulateWithSensitiveToken(buffer); // writes a secret
    SendToken(buffer, written);
}
finally
{
    ArrayPool<byte>.Shared.Return(buffer);
    // clearArray defaults to false - the secret token's bytes are
    // still sitting in this array when it goes back into the pool
}

// Elsewhere, a completely unrelated piece of code rents a
// same-sized buffer for something totally unrelated and non-sensitive:
byte[] scratch = ArrayPool<byte>.Shared.Rent(256);
// there's a real chance this IS the same backing array, still
// holding the previous caller's token bytes until scratch overwrites them

✅ Fix: Explicitly Clear the Buffer When It Ever Held Sensitive Data

  • Pass clearArray: true to ArrayPool<T>.Shared.Return(buffer, clearArray: true) for any buffer that held sensitive data at any point during its use — the small, explicit cost of zeroing is the correct tradeoff for exactly the cases where leaving stale data behind in a shared pool is actually a problem.
  • Default to NOT pooling (use a freshly allocated, short-lived array instead) for genuinely small, infrequent allocations of sensitive data, and reserve ArrayPool specifically for high-frequency, non-sensitive scratch buffers where its performance benefit is actually worth the bookkeeping required to use it safely.
  • Consider CryptographicOperations.ZeroMemory() (or an explicit clearing loop) immediately after the buffer’s sensitive use is finished, rather than relying solely on the return call — this clears the data the moment it’s no longer needed, instead of leaving it sitting in memory for however long the buffer remains checked out before it’s eventually returned.

🚨 Why This Is Dangerous

  • A pooled buffer is just a reused chunk of process memory, and unlike disposing an object there’s no garbage collector eventually reclaiming and zeroing it — the leftover sensitive bytes simply stay there, readable by any other code in the same process that happens to rent a same-sized buffer next, for as long as the pool continues reusing that particular array.

ArrayPool gives you back the same memory, not a clean copy of it — and anything you wrote there before returning it is still there for whoever the pool hands that buffer to next, unless you asked it to clear first.

— C# Best Practice

Related posts:

Add blank item at top of dropdownlist

Add blank item at top of dropdownlist

How to set Tooltip for a particular Devexpress Gridview Column

How to set Tooltip for a particular Devexpress Gridview Column

C#: Why an Event Handler Keeps a Disposed Object Alive and Leaks Memory

C#: Why an Event Handler Keeps a Disposed Object Alive and Leaks Memory

Views: 0
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

The AI Prompt That Sorts Board Game Boxes by Play Time
Windows 11: Fix Phone Link Dropping Notifications Post-Update

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

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

Social

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