From 4b4d2c25b473eb09db661215941ff88bcb49589c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 May 2020 10:36:17 +0200 Subject: Add OOB-error checking in array case Signed-off-by: Matthias Beyer --- src/resolver.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/resolver.rs b/src/resolver.rs index acbaeb6..6c61020 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -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 { .. })); -- cgit v1.2.3