DATAREDIS-472 - Add guards to JedisConnection before casting long to int.

This commit is contained in:
Christoph Strobl
2016-03-04 11:35:15 +01:00
parent b630744564
commit 5a85ac47a3
2 changed files with 81 additions and 22 deletions

View File

@@ -1037,6 +1037,10 @@ public class JedisConnection extends AbstractRedisConnection {
}
public void restore(byte[] key, long ttlInMillis, byte[] serializedValue) {
if (ttlInMillis > Integer.MAX_VALUE) {
throw new IllegalArgumentException("TtlInMillis must be less than Integer.MAX_VALUE for restore in Jedis.");
}
try {
if (isPipelined()) {
pipeline(new JedisStatusResult(pipeline.restore(key, (int) ttlInMillis, serializedValue)));
@@ -1293,6 +1297,11 @@ public class JedisConnection extends AbstractRedisConnection {
}
public void setEx(byte[] key, long time, byte[] value) {
if (time > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Time must be less than Integer.MAX_VALUE for setEx in Jedis.");
}
try {
if (isPipelined()) {
pipeline(new JedisStatusResult(pipeline.setex(key, (int) time, value)));
@@ -1347,6 +1356,11 @@ public class JedisConnection extends AbstractRedisConnection {
}
public byte[] getRange(byte[] key, long start, long end) {
if (start > Integer.MAX_VALUE || end > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Start and end must be less than Integer.MAX_VALUE for getRange in Jedis.");
}
try {
if (isPipelined()) {
pipeline(new JedisResult(pipeline.substr(key, (int) start, (int) end), JedisConverters.stringToBytes()));
@@ -2022,6 +2036,11 @@ public class JedisConnection extends AbstractRedisConnection {
}
public List<byte[]> sRandMember(byte[] key, long count) {
if (count > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Count must be less than Integer.MAX_VALUE for sRandMember in Jedis.");
}
try {
if (isPipelined()) {
pipeline(new JedisResult(pipeline.srandmember(key, (int) count)));
@@ -3521,6 +3540,12 @@ public class JedisConnection extends AbstractRedisConnection {
@Override
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {
if (offset > Integer.MAX_VALUE || count > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"Offset and count must be less than Integer.MAX_VALUE for zRangeByScore in Jedis.");
}
try {
String keyStr = new String(key, "UTF-8");
if (isPipelined()) {

View File

@@ -20,9 +20,6 @@ import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -34,7 +31,6 @@ import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase;
import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption;
import org.springframework.data.redis.connection.jedis.JedisConnectionUnitTestSuite.JedisConnectionPipelineUnitTests;
import org.springframework.data.redis.connection.jedis.JedisConnectionUnitTestSuite.JedisConnectionUnitTests;
import org.springframework.data.redis.core.TimeoutUtils;
import redis.clients.jedis.Client;
import redis.clients.jedis.Jedis;
@@ -97,24 +93,6 @@ public class JedisConnectionUnitTestSuite {
assertThat(captor.getValue(), equalTo("return redis.call('SHUTDOWN','SAVE')".getBytes()));
}
/**
* @see DATAREDIS-286
*/
@Test
public void pExpireHavingIntOverflowShouldUseRedisServerTimeAsReferenceForPExpireAt() {
long msec = Long.valueOf((long) Integer.MAX_VALUE + 1);
long expected = msec + TimeoutUtils.toMillis(1, TimeUnit.SECONDS);
/* redis time as list containing [0] = seconds, [1] = microseconds
* @see http://redis.io/commands/time
*/
when(jedisSpy.time()).thenReturn(Arrays.asList("1", "0"));
connection.pExpire("foo".getBytes(), msec);
verifyNativeConnectionInvocation().pexpireAt(any(byte[].class), eq(expected));
}
/**
* @see DATAREDIS-267
*/
@@ -171,6 +149,62 @@ public class JedisConnectionUnitTestSuite {
connection.getSentinelConnection();
}
/**
* @see DATAREDIS-472
*/
@Test(expected = IllegalArgumentException.class)
public void restoreShouldThrowExceptionWhenTtlInMillisExceedsIntegerRange() {
connection.restore("foo".getBytes(), new Long(Integer.MAX_VALUE) + 1L, "bar".getBytes());
}
/**
* @see DATAREDIS-472
*/
@Test(expected = IllegalArgumentException.class)
public void setExShouldThrowExceptionWhenTimeExceedsIntegerRange() {
connection.setEx("foo".getBytes(), new Long(Integer.MAX_VALUE) + 1L, "bar".getBytes());
}
/**
* @see DATAREDIS-472
*/
@Test(expected = IllegalArgumentException.class)
public void getRangeShouldThrowExceptionWhenStartExceedsIntegerRange() {
connection.getRange("foo".getBytes(), new Long(Integer.MAX_VALUE) + 1L, Integer.MAX_VALUE);
}
/**
* @see DATAREDIS-472
*/
@Test(expected = IllegalArgumentException.class)
public void getRangeShouldThrowExceptionWhenEndExceedsIntegerRange() {
connection.getRange("foo".getBytes(), Integer.MAX_VALUE, new Long(Integer.MAX_VALUE) + 1L);
}
/**
* @see DATAREDIS-472
*/
@Test(expected = IllegalArgumentException.class)
public void sRandMemberShouldThrowExceptionWhenCountExceedsIntegerRange() {
connection.sRandMember("foo".getBytes(), new Long(Integer.MAX_VALUE) + 1L);
}
/**
* @see DATAREDIS-472
*/
@Test(expected = IllegalArgumentException.class)
public void zRangeByScoreShouldThrowExceptionWhenOffsetExceedsIntegerRange() {
connection.zRangeByScore("foo".getBytes(), "foo", "bar", new Long(Integer.MAX_VALUE) + 1L, Integer.MAX_VALUE);
}
/**
* @see DATAREDIS-472
*/
@Test(expected = IllegalArgumentException.class)
public void zRangeByScoreShouldThrowExceptionWhenCountExceedsIntegerRange() {
connection.zRangeByScore("foo".getBytes(), "foo", "bar", Integer.MAX_VALUE, new Long(Integer.MAX_VALUE) + 1L);
}
}
public static class JedisConnectionPipelineUnitTests extends JedisConnectionUnitTests {