Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try_replace panics with index out of bounds when deleting value #4

Closed
stefanpieck opened this issue Feb 7, 2022 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@stefanpieck
Copy link

Test to reproduce:

    #[test]
    fn test_delete_in_try_replace() -> Result<(), ParseError> {
        let input = json!({"list": ["BLUE", "ORANGE", "GREEN", "RED"]});
        let output = JsonPath::compile("$.list[*]")?.try_replace(&input, |_| None);
        assert_eq!(output, json!({"list": []}));
        Ok(())
    }

leads to error:

thread 'using_jsonpath_plus::tests::test_delete_in_try_replace' panicked at 'index out of bounds: the len is 2 but the index is 2', /Users/sp/.cargo/registry/src/github.com-1ecc6299db9ec823/jsonpath-plus-0.1.3/src/utils.rs:60:30
stack backtrace:
   0: rust_begin_unwind
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/panicking.rs:107:14
   2: core::panicking::panic_bounds_check
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/panicking.rs:75:5
   3: <usize as core::slice::index::SliceIndex<[T]>>::index
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/slice/index.rs:184:10
   4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/slice/index.rs:15:9
   5: <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/alloc/src/vec/mod.rs:2528:9
   6: jsonpath_plus::utils::try_replace_paths
             at /Users/sp/.cargo/registry/src/github.com-1ecc6299db9ec823/jsonpath-plus-0.1.3/src/utils.rs:60:30
   7: jsonpath_plus::<impl jsonpath_plus::ast::Path>::try_replace
             at /Users/sp/.cargo/registry/src/github.com-1ecc6299db9ec823/jsonpath-plus-0.1.3/src/lib.rs:145:9
   8: jsonpath_filter::using_jsonpath_plus::tests::test_delete_in_try_replace
             at ./src/using_jsonpath_plus.rs:216:22
   9: jsonpath_filter::using_jsonpath_plus::tests::test_delete_in_try_replace::{{closure}}
             at ./src/using_jsonpath_plus.rs:214:5
  10: core::ops::function::FnOnce::call_once
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/ops/function.rs:227:5
  11: core::ops::function::FnOnce::call_once
             at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/ops/function.rs:227:5
@CraftSpider
Copy link
Owner

Darn, I remember briefly realizing this may be an issue while working on this earlier, then I forgot to implement a fix. This happens because a deletion of an earlier element may change the index of later elements. I believe this can be fixed fairly simply by improving the ordering of paths to always work backwards along arrays. I'll publish a fix ASAP

@CraftSpider CraftSpider added the bug Something isn't working label Feb 7, 2022
@CraftSpider
Copy link
Owner

I appreciate the effort to provide pre-written test cases, thank you for the bug reports

@CraftSpider
Copy link
Owner

Fix published in version 0.1.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants