Skip to content

Commit dde2450

Browse files
authored
Avoid crash in ARRTRIM (fix issue RedisJSON#485) (RedisJSON#499)
1 parent 861bfce commit dde2450

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/manager.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,15 @@ impl<'a> WriteHolder<Value, Value> for KeyHolderWrite<'a> {
439439
if let Some(array) = v.as_array() {
440440
let len = array.len() as i64;
441441
let stop = stop.normalize(len);
442-
443-
let range = if start > len || start > stop as i64 || len == 0 {
442+
let start = if start < 0 || start < len {
443+
start.normalize(len)
444+
} else {
445+
stop + 1 // start >=0 && start >= len
446+
};
447+
let range = if start > stop || len == 0 {
444448
0..0 // Return an empty array
445449
} else {
446-
start.normalize(len)..(stop + 1)
450+
start..(stop + 1)
447451
};
448452

449453
let mut new_value = v.take();

tests/pytest/test.py

+8
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ def testArrTrimCommand(env):
674674

675675
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'test', '.arr', -1, 0), 0)
676676

677+
r.assertOk(r.execute_command('JSON.SET', 'test',
678+
'.', '{ "arr": [0, 1, 2, 3, 2, 1, 0] }'))
679+
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'test', '.arr', -1, 0), 0)
680+
681+
r.assertOk(r.execute_command('JSON.SET', 'test',
682+
'.', '{ "arr": [0, 1, 2, 3, 2, 1, 0] }'))
683+
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'test', '.arr', -4, 1), 0)
684+
677685

678686
def testArrPopCommand(env):
679687
"""Test JSON.ARRPOP command"""

0 commit comments

Comments
 (0)