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

Possible null reference exception in compiled renderer #106

Closed
natehitze opened this issue Aug 13, 2020 · 1 comment · Fixed by #133
Closed

Possible null reference exception in compiled renderer #106

natehitze opened this issue Aug 13, 2020 · 1 comment · Fixed by #133
Milestone

Comments

@natehitze
Copy link

It's possible to get the compiled renderer to throw a null reference exception by adding a null item to a list in the model and then iterating over the list and accessing a property in the template.

Model:

public class ExampleClass
{
    public string Bar { get; } = "Test"
}

var input = new 
{
    Examples = new List<ExampleClass>() { null }
}

Template:

{{#Examples}} {{Bar}} {{/Examples}}

If you execute a compiled renderer function for that template with the example input you will get a NullReferenceException. I ran into this on a simple template & input model, but it could be a nightmare to debug with a complex model & template since there's no debug information other than the exception type.

I think a null check in the expression returned by CompilerContext.Lookup would be helpful? In my case I would prefer an exception to be thrown that has some information such as the name of the variable that failed and location in the template or context path that led to the variable.

@Romanx
Copy link
Contributor

Romanx commented Aug 29, 2022

Hey @natehitze,

Thanks for reporting this and submitting a PR. This was entirely an oversight in the compilation renderer design. I've approached the solution in a slightly different way which makes all access safe.

The feature is now on nuget a v1.10.8

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

Successfully merging a pull request may close this issue.

2 participants