Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Components/Components/src/NavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ protected async ValueTask<bool> NotifyLocationChangingAsync(string uri, string?
}
finally
{
locationChangingHandlersCopy.AsSpan(0, handlerCount).Clear();
ArrayPool<Func<LocationChangingContext, ValueTask>>.Shared.Return(locationChangingHandlersCopy);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Runtime.CompilerServices;

namespace Microsoft.AspNetCore.Components.Endpoints.FormMapping;

Expand All @@ -17,7 +18,14 @@ public static PooledBuffer Add(ref PooledBuffer buffer, TElement element)
{
var newBuffer = ArrayPool<TElement>.Shared.Rent(buffer.Data.Length * 2);
Array.Copy(buffer.Data, newBuffer, buffer.Data.Length);

if (RuntimeHelpers.IsReferenceOrContainsReferences<TElement>())
{
buffer.Data.AsSpan(0, buffer.Count).Clear();
}

ArrayPool<TElement>.Shared.Return(buffer.Data);

buffer.Data = newBuffer;
}

Expand All @@ -28,7 +36,14 @@ public static PooledBuffer Add(ref PooledBuffer buffer, TElement element)
public static TCollection ToResult(PooledBuffer buffer)
{
var result = TCollectionFactory.ToResultCore(buffer.Data, buffer.Count);

if (RuntimeHelpers.IsReferenceOrContainsReferences<TElement>())
{
buffer.Data.AsSpan(0, buffer.Count).Clear();
}

ArrayPool<TElement>.Shared.Return(buffer.Data);

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void Dispose()
{
if (_sortedKeys != null)
{
_sortedKeys.AsSpan(0, _length).Clear();
ArrayPool<FormKey>.Shared.Return(_sortedKeys);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static async ValueTask StoreAsync(string key, OutputCacheEntry value, Has
await bufferStore.SetAsync(key, new(buffer.GetCommittedMemory()), CopyToLeasedMemory(tags, out var lease), duration, cancellationToken);
if (lease is not null)
{
if (tags is not null)
{
lease.AsSpan(0, tags.Count).Clear();
}

ArrayPool<string>.Shared.Return(lease);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Middleware/OutputCaching/src/RecyclableArrayBufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Microsoft.AspNetCore.OutputCaching;

Expand Down Expand Up @@ -32,10 +33,16 @@ public RecyclableArrayBufferWriter()
public void Dispose()
{
var tmp = _buffer;
var count = _index;
_index = 0;
_buffer = Array.Empty<T>();
if (tmp.Length != 0)
{
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
tmp.AsSpan(0, count).Clear();
}

ArrayPool<T>.Shared.Return(tmp);
}
}
Expand Down Expand Up @@ -120,6 +127,11 @@ private void CheckAndResizeBuffer(int sizeHint)
oldArray.AsSpan(0, _index).CopyTo(_buffer);
if (oldArray.Length != 0)
{
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
oldArray.AsSpan(0, _index).Clear();
}

ArrayPool<T>.Shared.Return(oldArray);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Mvc/Mvc.NewtonsoftJson/src/JsonArrayPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using Newtonsoft.Json;
using System.Runtime.CompilerServices;

namespace Microsoft.AspNetCore.Mvc.NewtonsoftJson;

Expand All @@ -26,6 +27,11 @@ public void Return(T[]? array)
{
ArgumentNullException.ThrowIfNull(array);

if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
array.AsSpan().Clear();
}

_inner.Return(array);
}
}
11 changes: 11 additions & 0 deletions src/Shared/ValueStringBuilder/ValueListBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public void Dispose()
if (toReturn != null)
{
_arrayFromPool = null;

if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
toReturn.AsSpan(0, _pos).Clear();
}

ArrayPool<T>.Shared.Return(toReturn);
}
}
Expand Down Expand Up @@ -95,6 +101,11 @@ private void Grow(int additionalCapacityRequired = 1)
_span = _arrayFromPool = array;
if (toReturn != null)
{
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
toReturn.AsSpan(0, _pos).Clear();
}

ArrayPool<T>.Shared.Return(toReturn);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/SignalR/common/Shared/JsonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Buffers;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -216,6 +217,15 @@ public void Return(T[]? array)
return;
}

#if NET
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
#else
if (!typeof(T).IsPrimitive)
#endif
{
array.AsSpan().Clear();
}

_inner.Return(array);
}
}
Expand Down
Loading