RFR: JDK-8282144 RandomSupport.convertSeedBytesToLongs sign extension overwrites previous bytes

Jim Laskey jlaskey at openjdk.java.net
Sun Feb 27 22:35:48 UTC 2022


On Fri, 25 Feb 2022 19:58:13 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Class: ./java.base/share/classes/jdk/internal/util/random/RandomSupport.java 
>> Method: public static long[] convertSeedBytesToLongs(byte[] seed, int n, int z) 
>> 
>> The method attempts to create an array of longs by consuming the input bytes most significant bit first. New bytes are appended to the existing long using the OR operator on the signed byte. Due to sign extension this will overwrite all the existing bits from 63 to 8 if the next byte is negative.
>
> test/jdk/java/util/Random/T8282144.java line 39:
> 
>> 37: public class T8282144 {
>> 38:     public static void main(String[] args) {
>> 39:         RandomGenerator rng = RandomGeneratorFactory.of("L64X128MixRandom").create(42);
> 
> Does `rng` always produce the same sequence? If so, then perhaps the seed, `42`, should be a random value that is printed.

42 was chosen because its is known to produce negative byte values, other random values might not.

> test/jdk/java/util/Random/T8282144.java line 52:
> 
>> 50:                 for (int k = 0; k < existing.length; k++) {
>> 51:                     if (existing[k] != testing[k]) {
>> 52:                         throw new RuntimeException("convertSeedBytesToLongs incorrect");
> 
> Should `i`, `j`, and `k` be included in the exception message?

Correctness is binary - either it works or it doesn't. The values of i, j, k would not assist in isolating issues.

-------------

PR: https://git.openjdk.java.net/jdk/pull/7614


More information about the core-libs-dev mailing list