Skip to content

Commit d9fa9cf

Browse files
Daniel HuangPaul Hohensee
authored andcommitted
8294509: The sign extension bug applies to 'public static int[] convertSeedBytesToInts(byte[] seed, int n, int z)' in RandomSupport
Backport-of: 5a9cd33632862aa2249794902d4168a7fe143054
1 parent 78c4c3a commit d9fa9cf

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/java.base/share/classes/jdk/internal/util/random/RandomSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public static int[] convertSeedBytesToInts(byte[] seed, int n, int z) {
308308
final int m = Math.min(seed.length, n << 2);
309309
// Distribute seed bytes into the words to be formed.
310310
for (int j = 0; j < m; j++) {
311-
result[j>>2] = (result[j>>2] << 8) | seed[j];
311+
result[j>>2] = (result[j>>2] << 8) | (seed[j] & 0xFF);
312312
}
313313
// If there aren't enough seed bytes for all the words we need,
314314
// use a SplitMix-style PRNG to fill in the rest.

test/jdk/java/util/Random/T8282144.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* @test
2929
* @summary RandomSupport.convertSeedBytesToLongs sign extension overwrites previous bytes.
30-
* @bug 8282144
30+
* @bug 8282144 8294509
3131
* @modules java.base/jdk.internal.util.random
3232
* @run main T8282144
3333
* @key randomness
@@ -36,6 +36,11 @@
3636

3737
public class T8282144 {
3838
public static void main(String[] args) {
39+
testLongs();
40+
testInts();
41+
}
42+
43+
private static void testLongs() {
3944
RandomGenerator rng = RandomGeneratorFactory.of("L64X128MixRandom").create(42);
4045

4146
for (int i = 1; i < 8; i++) {
@@ -56,6 +61,27 @@ public static void main(String[] args) {
5661
}
5762
}
5863

64+
private static void testInts() {
65+
RandomGenerator rng = RandomGeneratorFactory.of("L64X128MixRandom").create(42);
66+
67+
for (int i = 1; i < 8; i++) {
68+
byte[] seed = new byte[i];
69+
70+
for (int j = 0; j < 10; j++) {
71+
rng.nextBytes(seed);
72+
73+
int[] existing = RandomSupport.convertSeedBytesToInts(seed, 1, 1);
74+
int[] testing = convertSeedBytesToIntsFixed(seed, 1, 1);
75+
76+
for (int k = 0; k < existing.length; k++) {
77+
if (existing[k] != testing[k]) {
78+
throw new RuntimeException("convertSeedBytesToInts incorrect");
79+
}
80+
}
81+
}
82+
}
83+
}
84+
5985

6086
public static long[] convertSeedBytesToLongsFixed(byte[] seed, int n, int z) {
6187
final long[] result = new long[n];
@@ -68,4 +94,17 @@ public static long[] convertSeedBytesToLongsFixed(byte[] seed, int n, int z) {
6894

6995
return result;
7096
}
97+
98+
public static int[] convertSeedBytesToIntsFixed(byte[] seed, int n, int z) {
99+
final int[] result = new int[n];
100+
final int m = Math.min(seed.length, n << 2);
101+
102+
// Distribute seed bytes into the words to be formed.
103+
for (int j = 0; j < m; j++) {
104+
result[j >> 2] = (result[j >> 2] << 8) | (seed[j] & 0xff);
105+
}
106+
107+
return result;
108+
}
109+
71110
}

0 commit comments

Comments
 (0)