Do not use `AllocateUninitializedArray` in private array pools. (dotnet/coreclr#26338)
authorVladimir Sadov <vsadov@microsoft.com>
Mon, 26 Aug 2019 02:14:06 +0000 (19:14 -0700)
committerStephen Toub <stoub@microsoft.com>
Mon, 26 Aug 2019 02:14:06 +0000 (22:14 -0400)
User may have extra expectations for the privatly owned array pools.
For example there could be an expectation that array never contains negative numbers (since user never puts them there). Uninitialized allocations can break such expectations.

Commit migrated from https://github.com/dotnet/coreclr/commit/c89fd6f88744bfee034e792906700c5a17e1b953

src/libraries/System.Private.CoreLib/src/System/Buffers/ConfigurableArrayPool.cs

index f7c2829..f24ddf4 100644 (file)
@@ -100,13 +100,13 @@ namespace System.Buffers
 
                 // The pool was exhausted for this buffer size.  Allocate a new buffer with a size corresponding
                 // to the appropriate bucket.
-                buffer = GC.AllocateUninitializedArray<T>(_buckets[index]._bufferLength);
+                buffer = new T[_buckets[index]._bufferLength];
             }
             else
             {
                 // The request was for a size too large for the pool.  Allocate an array of exactly the requested length.
                 // When it's returned to the pool, we'll simply throw it away.
-                buffer = GC.AllocateUninitializedArray<T>(minimumLength);
+                buffer = new T[minimumLength];
             }
 
             if (log.IsEnabled())
@@ -215,7 +215,7 @@ namespace System.Buffers
                 // for that slot, in which case we should do so now.
                 if (allocateBuffer)
                 {
-                    buffer = GC.AllocateUninitializedArray<T>(_bufferLength);
+                    buffer = new T[_bufferLength];
 
                     ArrayPoolEventSource log = ArrayPoolEventSource.Log;
                     if (log.IsEnabled())