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

Triggering bug! panic with StrVals passed to sections #45

Closed
ibabushkin opened this issue Dec 28, 2016 · 4 comments
Closed

Triggering bug! panic with StrVals passed to sections #45

ibabushkin opened this issue Dec 28, 2016 · 4 comments

Comments

@ibabushkin
Copy link
Contributor

Given a template such as this one:

{{# some_var }}
some text {{{ some_var }}}
{{/ some_var }}

and using

let mut data = HashMap::new();
data.insert("some_var", "some value");

as the data passed to the compiled template, I am receiving the following panic:

thread 'main' panicked at 'bug: unexpected value StrVal(some value). Please report this issue on GitHub if you find an input that triggers this case.'

A code search in the repo yields the following lines in src/template.rs as the culprit:

match self.find(path, stack) {
    None => {}
    Some(value) => {
        match *value {
            Bool(true) => {
                try!(self.render(wr, stack, children));
            }
            Bool(false) => {}
            VecVal(ref vs) => {
                for v in vs.iter() {
                    stack.push(v);
                    try!(self.render(wr, stack, children));
                    stack.pop();
                }
            }
            Map(_) => {
                stack.push(value);
                try!(self.render(wr, stack, children));
                stack.pop();
            }
            Fun(ref fcell) => {
                let f = &mut *fcell.borrow_mut();
                let tokens = try!(self.render_fun(src, otag, ctag, f));
                try!(self.render(wr, stack, &tokens));
            }
            OptVal(ref val) => {
                if let Some(ref val) = *val {
                    stack.push(val);
                    try!(self.render(wr, stack, children));
                    stack.pop();
                }
            }
            _ => bug!("unexpected value {:?}", value),
        }
    }
};

Now, the spec doesn't quite specify a clear behaviour on whether string values are to be interpreted as "thruthy", "falsey" or whether this decision is up to the host language's interpretation of this question. Now, in either case, either some filtering at template-compile-time should be done, or strings should be explicitly handled.

I can provide a fix for this (I suggest handling strings as true), but I'd like to know how the authors/maintainers see this issue.

@therealbstern
Copy link
Contributor

Empty strings should be handled as "false" as they are a falsy value (see janl/mustache.js#186 for a rationale).

@therealbstern
Copy link
Contributor

FYI, the code referenced above seems to be at line 278.

@ibabushkin
Copy link
Contributor Author

Does this commit ibabushkin@1bd7ebf satisfy your requirements? If so, I'd open a pull-request.

@jolhoeft
Copy link
Member

Yes please, we'd like a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants