diff --git a/src/json/path.rs b/src/json/path.rs index 9e0dcea31..8270371b2 100644 --- a/src/json/path.rs +++ b/src/json/path.rs @@ -74,7 +74,7 @@ fn get_local_path_and_level(paths: &[PathSeg]) -> Option<(usize, String)> { paths.get(0).and_then(|seg| { if seg == &PathSeg::Ruled(Rule::path_local) { let mut level = 0; - while paths[level + 1] == PathSeg::Ruled(Rule::path_up) { + while paths.get(level + 1)? == &PathSeg::Ruled(Rule::path_up) { level += 1; } if let Some(PathSeg::Named(name)) = paths.get(level + 1) { diff --git a/tests/subexpression.rs b/tests/subexpression.rs index 2ac271f61..57754c35d 100644 --- a/tests/subexpression.rs +++ b/tests/subexpression.rs @@ -53,3 +53,17 @@ fn test_subexpression() { "Success" ); } + +#[test] +fn invalid_json_path() { + // The data here is not important + let data = &Vec::<()>::new(); + + let hbs = Handlebars::new(); + + let error = hbs.render_template("{{x[]@this}}", &data).unwrap_err(); + + let expected = "Error rendering \"Unnamed template\" line 1, col 1: Helper not defined: \"x\""; + + assert_eq!(format!("{}", error), expected); +}