Skip to content

Commit fce10b2

Browse files
Resolve xoshiro256** out of range exn
Fixed an OutOfRangeException thrown by the xoshiro256** implementation of DoSampleBytes when passed certain length buffers
1 parent e989ab7 commit fce10b2

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/Numerics.Tests/Random/RandomTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ public void ThreadSafeSample()
137137
t2.Join();
138138
}
139139

140+
/// <summary>
141+
/// NextBytes fills buffers of different sizes without throwing an exception (e.g. OutOfRangeException)
142+
/// </remarks>
143+
[Test]
144+
public void NextBytesDoesNotThrow()
145+
{
146+
var rng = (System.Random)Activator.CreateInstance(_randomType, new object[] { false });
147+
148+
for (int i = 1; i < 30; i++)
149+
{
150+
Assert.DoesNotThrow(() => rng.NextBytes(new byte[i]));
151+
}
152+
}
153+
140154
/// <summary>
141155
/// Test runner function.
142156
/// </summary>

src/Numerics/Random/Xoshiro256StarStar.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ protected override void DoSampleBytes(byte[] buffer)
158158
int i = 0;
159159

160160
// Fill up the bulk of the buffer in chunks of 8 bytes at a time.
161-
for (int bound = buffer.Length - 3; i < bound;)
161+
int bound = buffer.Length - (buffer.Length % 8);
162+
while (i < bound)
162163
{
163164
// Generate 64 random bits.
164165
ulong x = RotateLeft(s1 * 5, 7) * 9;

0 commit comments

Comments
 (0)