Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions PolyShim.Tests/NetCore21/RandomNumberGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Security.Cryptography;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.NetCore21;

public class RandomNumberGeneratorTests
{
[Fact]
public void Fill_Span_Test()
{
// Arrange
Span<byte> buffer = stackalloc byte[16];

// Act
RandomNumberGenerator.Fill(buffer);

// Assert
// Since cryptographic random data is non-deterministic,
// we can only verify that the buffer contains some non-zero bytes
buffer.ToArray().Should().Contain(b => b != 0);
Comment on lines +14 to +22
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test uses stackalloc byte[16], which is not guaranteed to be zero-initialized; the assertion can pass even if RandomNumberGenerator.Fill(...) is a no-op because the stack buffer may already contain non-zero bytes. Initialize the span (e.g., call buffer.Clear() or use a new byte[16]) before invoking Fill so the test actually validates that bytes were written.

Copilot uses AI. Check for mistakes.
}
}
25 changes: 25 additions & 0 deletions PolyShim/NetCore21/RandomNumberGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if (NETCOREAPP && !NETCOREAPP2_1_OR_GREATER) || (NETFRAMEWORK && NET45_OR_GREATER)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing net standard boundaries (up to 2.1)

Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compilation guard here is inconsistent with the rest of the NetCore21/* polyfills (which generally include NETSTANDARD && !NETSTANDARD2_1_OR_GREATER and don’t special-case NETFRAMEWORK && NET45_OR_GREATER). As written, this polyfill won’t be included for netstandard2.0 consumers, and it’s unclear why it should be excluded for net35/net40 but not other NetCore21 polyfills. Consider aligning the #if condition with the pattern used in PolyShim/NetCore21/DateTime.cs / Random.cs so the API is polyfilled for the intended TFMs.

Copilot uses AI. Check for mistakes.
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;
using System.Security.Cryptography;

internal static partial class PolyfillExtensions
{
extension(RandomNumberGenerator)
{
// https://learn.microsoft.com/dotnet/api/system.security.cryptography.randomnumbergenerator.fill
public static void Fill(Span<byte> data)
{
var buffer = new byte[data.Length];
using var rng = RandomNumberGenerator.Create();
rng.GetBytes(buffer);
buffer.CopyTo(data);
Comment on lines +16 to +21
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fill(Span<byte>) always allocates a managed array and creates/disposes a RandomNumberGenerator, even when data.Length == 0. Adding a fast-path return for empty spans would avoid unnecessary allocations/crypto provider creation while preserving behavior.

Copilot uses AI. Check for mistakes.
}
}
}
#endif
Loading