Skip to content

Commit 5a85ac4

Browse files
DATAREDIS-472 - Add guards to JedisConnection before casting long to int.
1 parent b630744 commit 5a85ac4

File tree

2 files changed

+81
-22
lines changed

2 files changed

+81
-22
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,10 @@ public byte[] dump(byte[] key) {
10371037
}
10381038

10391039
public void restore(byte[] key, long ttlInMillis, byte[] serializedValue) {
1040+
1041+
if (ttlInMillis > Integer.MAX_VALUE) {
1042+
throw new IllegalArgumentException("TtlInMillis must be less than Integer.MAX_VALUE for restore in Jedis.");
1043+
}
10401044
try {
10411045
if (isPipelined()) {
10421046
pipeline(new JedisStatusResult(pipeline.restore(key, (int) ttlInMillis, serializedValue)));
@@ -1293,6 +1297,11 @@ public Boolean mSetNX(Map<byte[], byte[]> tuples) {
12931297
}
12941298

12951299
public void setEx(byte[] key, long time, byte[] value) {
1300+
1301+
if (time > Integer.MAX_VALUE) {
1302+
throw new IllegalArgumentException("Time must be less than Integer.MAX_VALUE for setEx in Jedis.");
1303+
}
1304+
12961305
try {
12971306
if (isPipelined()) {
12981307
pipeline(new JedisStatusResult(pipeline.setex(key, (int) time, value)));
@@ -1347,6 +1356,11 @@ public Boolean setNX(byte[] key, byte[] value) {
13471356
}
13481357

13491358
public byte[] getRange(byte[] key, long start, long end) {
1359+
1360+
if (start > Integer.MAX_VALUE || end > Integer.MAX_VALUE) {
1361+
throw new IllegalArgumentException("Start and end must be less than Integer.MAX_VALUE for getRange in Jedis.");
1362+
}
1363+
13501364
try {
13511365
if (isPipelined()) {
13521366
pipeline(new JedisResult(pipeline.substr(key, (int) start, (int) end), JedisConverters.stringToBytes()));
@@ -2022,6 +2036,11 @@ public byte[] sRandMember(byte[] key) {
20222036
}
20232037

20242038
public List<byte[]> sRandMember(byte[] key, long count) {
2039+
2040+
if (count > Integer.MAX_VALUE) {
2041+
throw new IllegalArgumentException("Count must be less than Integer.MAX_VALUE for sRandMember in Jedis.");
2042+
}
2043+
20252044
try {
20262045
if (isPipelined()) {
20272046
pipeline(new JedisResult(pipeline.srandmember(key, (int) count)));
@@ -3521,6 +3540,12 @@ public Set<byte[]> zRangeByScore(byte[] key, String min, String max) {
35213540
@Override
35223541
public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) {
35233542

3543+
if (offset > Integer.MAX_VALUE || count > Integer.MAX_VALUE) {
3544+
3545+
throw new IllegalArgumentException(
3546+
"Offset and count must be less than Integer.MAX_VALUE for zRangeByScore in Jedis.");
3547+
}
3548+
35243549
try {
35253550
String keyStr = new String(key, "UTF-8");
35263551
if (isPipelined()) {

src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionUnitTestSuite.java

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
import static org.mockito.Matchers.*;
2121
import static org.mockito.Mockito.*;
2222

23-
import java.util.Arrays;
24-
import java.util.concurrent.TimeUnit;
25-
2623
import org.junit.Before;
2724
import org.junit.Test;
2825
import org.junit.runner.RunWith;
@@ -34,7 +31,6 @@
3431
import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption;
3532
import org.springframework.data.redis.connection.jedis.JedisConnectionUnitTestSuite.JedisConnectionPipelineUnitTests;
3633
import org.springframework.data.redis.connection.jedis.JedisConnectionUnitTestSuite.JedisConnectionUnitTests;
37-
import org.springframework.data.redis.core.TimeoutUtils;
3834

3935
import redis.clients.jedis.Client;
4036
import redis.clients.jedis.Jedis;
@@ -97,24 +93,6 @@ public void shutdownSaveShouldBeSentCorrectlyUsingLuaScript() {
9793
assertThat(captor.getValue(), equalTo("return redis.call('SHUTDOWN','SAVE')".getBytes()));
9894
}
9995

100-
/**
101-
* @see DATAREDIS-286
102-
*/
103-
@Test
104-
public void pExpireHavingIntOverflowShouldUseRedisServerTimeAsReferenceForPExpireAt() {
105-
106-
long msec = Long.valueOf((long) Integer.MAX_VALUE + 1);
107-
long expected = msec + TimeoutUtils.toMillis(1, TimeUnit.SECONDS);
108-
109-
/* redis time as list containing [0] = seconds, [1] = microseconds
110-
* @see http://redis.io/commands/time
111-
*/
112-
when(jedisSpy.time()).thenReturn(Arrays.asList("1", "0"));
113-
114-
connection.pExpire("foo".getBytes(), msec);
115-
verifyNativeConnectionInvocation().pexpireAt(any(byte[].class), eq(expected));
116-
}
117-
11896
/**
11997
* @see DATAREDIS-267
12098
*/
@@ -171,6 +149,62 @@ public void shouldThrowExceptionWhenAccessingRedisSentinelsCommandsWhenNoSentine
171149
connection.getSentinelConnection();
172150
}
173151

152+
/**
153+
* @see DATAREDIS-472
154+
*/
155+
@Test(expected = IllegalArgumentException.class)
156+
public void restoreShouldThrowExceptionWhenTtlInMillisExceedsIntegerRange() {
157+
connection.restore("foo".getBytes(), new Long(Integer.MAX_VALUE) + 1L, "bar".getBytes());
158+
}
159+
160+
/**
161+
* @see DATAREDIS-472
162+
*/
163+
@Test(expected = IllegalArgumentException.class)
164+
public void setExShouldThrowExceptionWhenTimeExceedsIntegerRange() {
165+
connection.setEx("foo".getBytes(), new Long(Integer.MAX_VALUE) + 1L, "bar".getBytes());
166+
}
167+
168+
/**
169+
* @see DATAREDIS-472
170+
*/
171+
@Test(expected = IllegalArgumentException.class)
172+
public void getRangeShouldThrowExceptionWhenStartExceedsIntegerRange() {
173+
connection.getRange("foo".getBytes(), new Long(Integer.MAX_VALUE) + 1L, Integer.MAX_VALUE);
174+
}
175+
176+
/**
177+
* @see DATAREDIS-472
178+
*/
179+
@Test(expected = IllegalArgumentException.class)
180+
public void getRangeShouldThrowExceptionWhenEndExceedsIntegerRange() {
181+
connection.getRange("foo".getBytes(), Integer.MAX_VALUE, new Long(Integer.MAX_VALUE) + 1L);
182+
}
183+
184+
/**
185+
* @see DATAREDIS-472
186+
*/
187+
@Test(expected = IllegalArgumentException.class)
188+
public void sRandMemberShouldThrowExceptionWhenCountExceedsIntegerRange() {
189+
connection.sRandMember("foo".getBytes(), new Long(Integer.MAX_VALUE) + 1L);
190+
}
191+
192+
/**
193+
* @see DATAREDIS-472
194+
*/
195+
@Test(expected = IllegalArgumentException.class)
196+
public void zRangeByScoreShouldThrowExceptionWhenOffsetExceedsIntegerRange() {
197+
connection.zRangeByScore("foo".getBytes(), "foo", "bar", new Long(Integer.MAX_VALUE) + 1L, Integer.MAX_VALUE);
198+
}
199+
200+
/**
201+
* @see DATAREDIS-472
202+
*/
203+
@Test(expected = IllegalArgumentException.class)
204+
public void zRangeByScoreShouldThrowExceptionWhenCountExceedsIntegerRange() {
205+
connection.zRangeByScore("foo".getBytes(), "foo", "bar", Integer.MAX_VALUE, new Long(Integer.MAX_VALUE) + 1L);
206+
}
207+
174208
}
175209

176210
public static class JedisConnectionPipelineUnitTests extends JedisConnectionUnitTests {

0 commit comments

Comments
 (0)