Skip to content

Commit

Permalink
Merge pull request #430 from 5225225/json-path-fix
Browse files Browse the repository at this point in the history
Fix out of bounds index when parsing JSON path
  • Loading branch information
sunng87 authored May 3, 2021
2 parents 880486b + 1e98496 commit 472ffa5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/json/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions tests/subexpression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 472ffa5

Please # to comment.