Repository navigation
Description
127.0.0.1:6379> json.set a . [{}]
OK
127.0.0.1:6379> json.set a [0] {}
OK
127.0.0.1:6379> json.set a [0] {} NX
(error) ERR array index out of range
This is impacting recovery logic in my application that scaffolds out missing parts of the tree. This also happens when you are NX'ing to an element that is neither at the beginning nor end of the array.
Fresh build of the latest code in master (2e6cbe4, to be exact), Redis 6.2.6, macOS Monterey 12.1 on an M1.
Relevant code is at
Lines 240 to 317 in 2e6cbe4
| fn find_add_paths(&mut self, path: &str) -> Result<Vec<UpdateInfo>, Error> { | |
| let mut parsed_static_path = StaticPathParser::check(path)?; | |
| if parsed_static_path.valid != VisitStatus::Valid { | |
| return Err("Err: wrong static path".into()); | |
| } | |
| if parsed_static_path.static_path_elements.len() < 2 { | |
| return Err("Err: path must end with object key to set".into()); | |
| } | |
| let last = parsed_static_path.static_path_elements.pop().unwrap(); | |
| if let StaticPathElement::ObjectKey(key) = last { | |
| if let StaticPathElement::Root = parsed_static_path.static_path_elements.last().unwrap() | |
| { | |
| // Adding to the root | |
| Ok(vec![UpdateInfo::AUI(AddUpdateInfo { | |
| path: Vec::new(), | |
| key, | |
| })]) | |
| } else { | |
| // Adding somewhere in existing object, use jsonpath_lib::replace_with | |
| let p = parsed_static_path | |
| .static_path_elements | |
| .iter() | |
| .map(|e| e.to_string()) | |
| .collect::<Vec<_>>() | |
| .join(""); | |
| let mut selector = Selector::default(); | |
| if let Err(e) = selector.str_path(&p) { | |
| return Err(e.into()); | |
| } | |
| selector.value(self.val); | |
| let mut res = selector.select_with_paths(|_| true)?; | |
| Ok(res | |
| .drain(..) | |
| .map(|v| { | |
| UpdateInfo::AUI(AddUpdateInfo { | |
| path: v, | |
| key: key.to_string(), | |
| }) | |
| }) | |
| .collect()) | |
| } | |
| } else if let StaticPathElement::ArrayIndex(_) = last { | |
| // if we reach here with array path we must be out of range | |
| // otherwise the path would be valid to be set and we would not | |
| // have reached here!! | |
| Err("ERR array index out of range".into()) | |
| } else { | |
| Err("ERR path not an object or array".into()) | |
| } | |
| } | |
| pub fn find_paths( | |
| &mut self, | |
| path: &str, | |
| option: &SetOptions, | |
| ) -> Result<Vec<UpdateInfo>, Error> { | |
| if SetOptions::NotExists != *option { | |
| let mut selector = Selector::default(); | |
| let mut res = selector | |
| .str_path(path)? | |
| .value(self.val) | |
| .select_with_paths(|_| true)?; | |
| if !res.is_empty() { | |
| return Ok(res | |
| .drain(..) | |
| .map(|v| UpdateInfo::SUI(SetUpdateInfo { path: v })) | |
| .collect()); | |
| } | |
| } | |
| if SetOptions::AlreadyExists != *option { | |
| self.find_add_paths(path) | |
| } else { | |
| Ok(Vec::new()) // empty vector means no updates | |
| } | |
| } |
find_add_paths is called, which will indiscriminately return the above error when that code path is hit. I'm trying to work on a fix, but I'm having a hard time figuring out the best way to work this in.
The best solution I can think off is just adding a check to see if the given index is valid before returning an Err.
I'm wondering if I'm abusing NX slightly, and that this may be expected behavior, as it'd be inappropriate to expect this module to grow the array when NX is present. However, I still do think this error should not be returned given a valid element is present - if there was no element and NX was supplied, the error would probably be right to surface. Either that or NX for array elements becomes outright disallowed.
Activity
oshadmi commentedon Jan 9, 2022
Fixed by #593