~matthiasbeyer/serde-select

4b4d2c25b473eb09db661215941ff88bcb49589c — Matthias Beyer 4 years ago 2362aa3
Add OOB-error checking in array case

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
1 files changed, 9 insertions(+), 1 deletions(-)

M src/resolver.rs
M src/resolver.rs => src/resolver.rs +9 -1
@@ 51,6 51,14 @@ pub fn resolve<'doc, O>(obj: &'doc O, tokens: &Token, error_if_not_found: bool)
            match tokens {
                Token::Index { idx, .. } => {
                    trace!("Checking object at index {}", idx);
                    if !obj.has_index(*idx) {
                        if let Some(len) = obj.array_len() {
                            return Err(Error::IndexOutOfBounds(*idx, len));
                        } else {
                            return Err(Error::IndexOutOfBounds(*idx, 0));
                        }
                    }

                    match tokens.next() {
                        Some(next) => {
                            trace!("There is another token...");


@@ 661,7 669,7 @@ mod test {
        .unwrap();
        let result = do_resolve!(toml => "example.foo.[1]");

        assert!(result.is_err());
        assert!(result.is_err(), format!("Error expected but having: {:?}", result));
        let result = result.unwrap_err();

        assert!(is_match!(result, Error::IndexOutOfBounds { .. }));