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

Fix out of bounds index when parsing JSON path #430

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}