Stubble icon indicating copy to clipboard operation
Stubble copied to clipboard

Possible null reference exception in compiled renderer

Open natehitze opened this issue 5 years ago • 0 comments

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.

natehitze avatar Aug 13 '20 19:08 natehitze