Merge pull request #4672 from Shredder121/randomvalue-overflow
* pr/4672: Protect against Math.abs() with Long.MIN_VALUE
This commit is contained in:
commit
ecf9ce46e7
|
|
@ -110,11 +110,11 @@ public class RandomValuePropertySource extends PropertySource<Random> {
|
|||
private long getNextLongInRange(String range) {
|
||||
String[] tokens = StringUtils.commaDelimitedListToStringArray(range);
|
||||
if (tokens.length == 1) {
|
||||
return Math.abs(getSource().nextLong()) % Long.parseLong(tokens[0]);
|
||||
return Math.abs(getSource().nextLong() % Long.parseLong(tokens[0]));
|
||||
}
|
||||
long lowerBound = Long.parseLong(tokens[0]);
|
||||
long upperBound = Long.parseLong(tokens[1]) - lowerBound;
|
||||
return lowerBound + Math.abs(getSource().nextLong()) % upperBound;
|
||||
return lowerBound + Math.abs(getSource().nextLong() % upperBound);
|
||||
}
|
||||
|
||||
private Object getRandomBytes() {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,10 @@
|
|||
|
||||
package org.springframework.boot.context.config;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
|
@ -83,4 +86,27 @@ public class RandomValuePropertySourceTests {
|
|||
assertNotNull(value);
|
||||
assertTrue(value < 10L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void longOverflow() {
|
||||
RandomValuePropertySource source = Mockito.spy(this.source);
|
||||
Mockito.when(source.getSource()).thenReturn(new Random() {
|
||||
|
||||
@Override
|
||||
public long nextLong() {
|
||||
// constant that used to become -8, now becomes 8
|
||||
return Long.MIN_VALUE;
|
||||
}
|
||||
|
||||
});
|
||||
Long value = (Long) source.getProperty("random.long(10)");
|
||||
assertNotNull(value);
|
||||
assertTrue(value + " is less than 0", value >= 0L);
|
||||
assertTrue(value + " is more than 10", value < 10L);
|
||||
|
||||
value = (Long) source.getProperty("random.long[4,10]");
|
||||
assertNotNull(value);
|
||||
assertTrue(value + " is less than 4", value >= 4L);
|
||||
assertTrue(value + " is more than 10", value < 10L);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue